From ff829de4a708185b3a0552351d542233c6d19680 Mon Sep 17 00:00:00 2001 From: Stefan Negele Date: Mon, 2 Oct 2023 11:54:46 +0200 Subject: [PATCH] Allow diff/breaking for non existing schema specifcation --- schema.go | 27 +++++++++++++-------------- schema_test.go | 26 ++++++++++---------------- 2 files changed, 23 insertions(+), 30 deletions(-) diff --git a/schema.go b/schema.go index 5211c977..92a8a288 100644 --- a/schema.go +++ b/schema.go @@ -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 { @@ -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 @@ -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{} } } @@ -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) { diff --git a/schema_test.go b/schema_test.go index 2ef4bf7f..16faabf2 100644 --- a/schema_test.go +++ b/schema_test.go @@ -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", @@ -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, @@ -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, @@ -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, @@ -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) } })