Skip to content

Commit

Permalink
Allow diff/breaking for non existing schema specifcation
Browse files Browse the repository at this point in the history
  • Loading branch information
stefannegele committed Oct 2, 2023
1 parent 9d4c0a3 commit ff829de
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 30 deletions.
27 changes: 13 additions & 14 deletions schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -564,19 +564,16 @@ func (field Field) findEquivalent(otherFields []Field) (result *Field) {
return result
}

func GetSchemaSpecification(dataContract DataContract, pathToType []string, pathToSpecification []string) (dataset *Dataset, err error) {
func GetSchemaSpecification(dataContract DataContract, pathToType []string, pathToSpecification []string) (*Dataset, error) {
schemaType, localSchemaSpecification, err := extractSchemaSpecification(dataContract, pathToType, pathToSpecification)
if err != nil {
return nil, fmt.Errorf("failed extracting schema specification: %w", err)
}

schemaSpecificationBytes := schemaSpecificationAsString(localSchemaSpecification)
dataset, err = parseDataset(schemaType, schemaSpecificationBytes)
if err != nil {
return nil, fmt.Errorf("failed parsing dataset: %w", err)
}
parsedDataset := parseDataset(schemaType, schemaSpecificationBytes)

return dataset, err
return &parsedDataset, err
}

func schemaSpecificationAsString(schemaSpecification interface{}) []byte {
Expand Down Expand Up @@ -610,12 +607,13 @@ func extractSchemaSpecification(
func getSchemaType(contract DataContract, path []string) (schemaType string, err error) {
schemaTypeUntyped, err := GetValue(contract, path)
if err != nil {
return "", fmt.Errorf("can't get value of schema type: %w for path %v", err, path)
fmt.Println(fmt.Errorf("can't get value of schema type for path %v: %w", path, err))
return "", nil
}

schemaType, ok := schemaTypeUntyped.(string)
if !ok {
return "", fmt.Errorf("schema not of type string")
return "", fmt.Errorf("schema type not a string")
}

return schemaType, nil
Expand All @@ -624,18 +622,19 @@ func getSchemaType(contract DataContract, path []string) (schemaType string, err
func getSpecification(contract DataContract, path []string) (specification interface{}, err error) {
specification, err = GetValue(contract, path)
if err != nil {
return "", fmt.Errorf("can't get value of schema type: %w for path %v", err, path)
fmt.Println(fmt.Errorf("can't get value of schema specification for path %v: %w", path, err))
return "", nil
}

return specification, nil
}

func parseDataset(schemaType string, specification []byte) (*Dataset, error) {
func parseDataset(schemaType string, specification []byte) Dataset {
switch schemaType {
case "dbt":
return parseDbtDataset(specification), nil
return parseDbtDataset(specification)
default:
return nil, fmt.Errorf("unknown schema type %v", schemaType)
return Dataset{}
}
}

Expand Down Expand Up @@ -670,13 +669,13 @@ type dbtColumn struct {
Constraints []dbtConstraint
}

func parseDbtDataset(specification []byte) *Dataset {
func parseDbtDataset(specification []byte) Dataset {
var res dbtSpecification

yaml.Unmarshal(specification, &res)
models := modelsFromDbtSpecification(res)

return &Dataset{SchemaType: "dbt", Models: models}
return Dataset{SchemaType: "dbt", Models: models}
}

func modelsFromDbtSpecification(res dbtSpecification) (models []Model) {
Expand Down
26 changes: 10 additions & 16 deletions schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -428,15 +428,13 @@ func TestParseDataset(t *testing.T) {
}

tests := []struct {
name string
args args
want *Dataset
wantErr bool
name string
args args
want Dataset
}{
{
name: "unkown",
args: args{"unkown", []byte{}},
wantErr: true,
name: "unkown",
args: args{"unkown", []byte{}},
},
{
name: "dbt",
Expand All @@ -450,7 +448,7 @@ models:
- name: %v
data_type: %v
description: "%v"`, modelName, modelDescription, modelType, fieldName, fieldType, fieldDescription))},
want: &Dataset{Models: []Model{
want: Dataset{Models: []Model{
{
Name: modelName,
Type: &modelType,
Expand Down Expand Up @@ -485,7 +483,7 @@ models:
- name: %v
data_type: %v
description: "%v"`, modelName, modelDescription, modelType, fieldName, fieldName, fieldName, fieldName, fieldType, fieldDescription))},
want: &Dataset{Models: []Model{
want: Dataset{Models: []Model{
{
Name: modelName,
Type: &modelType,
Expand Down Expand Up @@ -521,7 +519,7 @@ models:
- type: check
expression: "id > 0"
`, modelName, modelDescription, modelType, fieldName, fieldType, fieldDescription))},
want: &Dataset{Models: []Model{
want: Dataset{Models: []Model{
{
Name: modelName,
Type: &modelType,
Expand All @@ -543,13 +541,9 @@ models:

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := parseDataset(tt.args.schemaType, tt.args.specification)
got := parseDataset(tt.args.schemaType, tt.args.specification)

if (err != nil) != tt.wantErr {
t.Errorf("parseDataset() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != nil && !got.equals(*tt.want) {
if !got.equals(tt.want) {
t.Errorf("parseDataset() got = %v, want %v", got, tt.want)
}
})
Expand Down

0 comments on commit ff829de

Please sign in to comment.