Skip to content

Commit

Permalink
add Date decoder
Browse files Browse the repository at this point in the history
  • Loading branch information
hgiasac committed Oct 13, 2024
1 parent 3eedd56 commit 7c81ae6
Show file tree
Hide file tree
Showing 35 changed files with 1,171 additions and 852 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ toolchain go1.23.1

require (
github.com/hasura/ndc-rest/ndc-rest-schema v0.2.5
github.com/hasura/ndc-sdk-go v1.5.1
github.com/hasura/ndc-sdk-go v1.5.2-0.20241013153211-ca0e28914f0c
github.com/lmittmann/tint v1.0.5
go.opentelemetry.io/otel v1.31.0
go.opentelemetry.io/otel/trace v1.31.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.22.0 h1:asbCHRVmodnJTuQ3qamDwqVOIjwqUPTYmYuemVOx+Ys=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.22.0/go.mod h1:ggCgvZ2r7uOoQjOyu2Y1NhHmEPPzzuhWgcza5M1Ji1I=
github.com/hasura/ndc-sdk-go v1.5.1 h1:RYcT/PpKcfeuugG575tFv1wogEMI040LoC5Q/8ANY9o=
github.com/hasura/ndc-sdk-go v1.5.1/go.mod h1:oik0JrwuN5iZwZjZJzIRMw9uO2xDJbCXwhS1GgaRejk=
github.com/hasura/ndc-sdk-go v1.5.2-0.20241013153211-ca0e28914f0c h1:C+2uejHoZbaU7AuVGP3xCaT+l0HPA6Mgvcu1E5eqbLA=
github.com/hasura/ndc-sdk-go v1.5.2-0.20241013153211-ca0e28914f0c/go.mod h1:oik0JrwuN5iZwZjZJzIRMw9uO2xDJbCXwhS1GgaRejk=
github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM=
github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSow5/V2vxeg=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
Expand Down
2 changes: 1 addition & 1 deletion go.work
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
go 1.23.1
go 1.23.0

use (
.
Expand Down
52 changes: 8 additions & 44 deletions ndc-rest-schema/command/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ import (
"io"
"log/slog"
"os"
"reflect"
"strings"
"testing"

"github.com/hasura/ndc-rest/ndc-rest-schema/schema"
"gotest.tools/v3/assert"
)

var nopLogger = slog.New(slog.NewTextHandler(io.Discard, &slog.HandlerOptions{}))
Expand Down Expand Up @@ -118,11 +117,11 @@ func TestConvertToNDCSchema(t *testing.T) {
err := CommandConvertToNDCSchema(args, nopLogger)

if tc.errorMsg != "" {
assertError(t, err, tc.errorMsg)
assert.ErrorContains(t, err, tc.errorMsg)
return
}

assertNoError(t, err)
assert.NilError(t, err)
if tc.noOutput {
return
}
Expand Down Expand Up @@ -150,46 +149,11 @@ func TestConvertToNDCSchema(t *testing.T) {
t.Errorf("cannot decode the output file json at %s", tc.expected)
t.FailNow()
}
assertDeepEqual(t, expectedSchema.Settings, output.Settings)
assertDeepEqual(t, expectedSchema.Functions, output.Functions)
assertDeepEqual(t, expectedSchema.Procedures, output.Procedures)
assertDeepEqual(t, expectedSchema.ScalarTypes, output.ScalarTypes)
assertDeepEqual(t, expectedSchema.ObjectTypes, output.ObjectTypes)
assert.DeepEqual(t, expectedSchema.Settings, output.Settings)
assert.DeepEqual(t, expectedSchema.Functions, output.Functions)
assert.DeepEqual(t, expectedSchema.Procedures, output.Procedures)
assert.DeepEqual(t, expectedSchema.ScalarTypes, output.ScalarTypes)
assert.DeepEqual(t, expectedSchema.ObjectTypes, output.ObjectTypes)
})
}
}

func assertNoError(t *testing.T, err error) {
if err != nil {
t.Errorf("expected no error, got: %s", err)
panic(err)
}
}

func assertError(t *testing.T, err error, message string) {
if err == nil {
t.Error("expected error, got nil")
t.FailNow()
} else if !strings.Contains(err.Error(), message) {
t.Errorf("expected error with content: %s, got: %s", err.Error(), message)
t.FailNow()
}
}

func assertDeepEqual(t *testing.T, expected any, reality any, msgs ...string) {
if reflect.DeepEqual(expected, reality) {
return
}

expectedJson, _ := json.Marshal(expected)
realityJson, _ := json.Marshal(reality)

var expected1, reality1 any
assertNoError(t, json.Unmarshal(expectedJson, &expected1))
assertNoError(t, json.Unmarshal(realityJson, &reality1))

if !reflect.DeepEqual(expected1, reality1) {
t.Errorf("%s: not equal.\nexpected: %s\ngot : %s", strings.Join(msgs, " "), string(expectedJson), string(realityJson))
t.FailNow()
}
}
5 changes: 3 additions & 2 deletions ndc-rest-schema/command/json2yaml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"testing"

"gopkg.in/yaml.v3"
"gotest.tools/v3/assert"
)

func TestJson2Yaml(t *testing.T) {
Expand Down Expand Up @@ -46,11 +47,11 @@ func TestJson2Yaml(t *testing.T) {
}, nopLogger)

if tc.errorMsg != "" {
assertError(t, err, tc.errorMsg)
assert.ErrorContains(t, err, tc.errorMsg)
return
}

assertNoError(t, err)
assert.NilError(t, err)

if tc.noOutput {
return
Expand Down
4 changes: 3 additions & 1 deletion ndc-rest-schema/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,20 @@ toolchain go1.23.1
require (
github.com/alecthomas/kong v1.2.1
github.com/evanphx/json-patch v0.5.2
github.com/hasura/ndc-sdk-go v1.5.1
github.com/hasura/ndc-sdk-go v1.5.2-0.20241013153211-ca0e28914f0c
github.com/invopop/jsonschema v0.12.0
github.com/lmittmann/tint v1.0.5
github.com/pb33f/libopenapi v0.18.3
github.com/wk8/go-ordered-map/v2 v2.1.9-0.20240815153524-6ea36470d1bd
gopkg.in/yaml.v3 v3.0.1
gotest.tools/v3 v3.5.1
)

require (
github.com/bahlo/generic-list-go v0.2.0 // indirect
github.com/buger/jsonparser v1.1.1 // indirect
github.com/dprotaso/go-yit v0.0.0-20240618133044-5a0af90af097 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/sergi/go-diff v1.3.1 // indirect
Expand Down
4 changes: 2 additions & 2 deletions ndc-rest-schema/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE=
github.com/hasura/ndc-sdk-go v1.5.1 h1:RYcT/PpKcfeuugG575tFv1wogEMI040LoC5Q/8ANY9o=
github.com/hasura/ndc-sdk-go v1.5.1/go.mod h1:oik0JrwuN5iZwZjZJzIRMw9uO2xDJbCXwhS1GgaRejk=
github.com/hasura/ndc-sdk-go v1.5.2-0.20241013153211-ca0e28914f0c h1:C+2uejHoZbaU7AuVGP3xCaT+l0HPA6Mgvcu1E5eqbLA=
github.com/hasura/ndc-sdk-go v1.5.2-0.20241013153211-ca0e28914f0c/go.mod h1:oik0JrwuN5iZwZjZJzIRMw9uO2xDJbCXwhS1GgaRejk=
github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM=
github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSow5/V2vxeg=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
Expand Down
27 changes: 14 additions & 13 deletions ndc-rest-schema/openapi/internal/oas2.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,10 @@ func (oc *OAS2Builder) getSchemaTypeFromProxy(schemaProxy *base.SchemaProxy, nul
if refName != "" && len(innerSchema.Type) > 0 && innerSchema.Type[0] == "object" {
refName = utils.ToPascalCase(refName)
ndcType = schema.NewNamedType(refName)
typeSchema = &rest.TypeSchema{Type: refName}
typeSchema = &rest.TypeSchema{
// TODO: remove type
// Type: refName,
}
} else {
if innerSchema.Title != "" && !strings.Contains(innerSchema.Title, " ") {
fieldPaths = []string{utils.ToPascalCase(innerSchema.Title)}
Expand All @@ -237,7 +240,6 @@ func (oc *OAS2Builder) getSchemaTypeFromProxy(schemaProxy *base.SchemaProxy, nul
}
}
if nullable {
typeSchema.Nullable = true
if !isNullableType(ndcType) {
ndcType = schema.NewNullableType(ndcType)
}
Expand All @@ -253,7 +255,7 @@ func (oc *OAS2Builder) getSchemaTypeFromParameter(param *v2.Parameter, apiPath s
return nil, errParameterSchemaEmpty(fieldPaths)
}
result = oc.buildScalarJSON()
} else if isPrimitiveScalar(param.Type) {
} else if isPrimitiveScalar([]string{param.Type}) {
scalarName := getScalarFromType(oc.schema, []string{param.Type}, param.Format, param.Enum, oc.trimPathPrefix(apiPath), fieldPaths)
result = schema.NewNamedType(scalarName)
} else {
Expand Down Expand Up @@ -293,7 +295,7 @@ func (oc *OAS2Builder) getSchemaType(typeSchema *base.Schema, apiPath string, fi
if _, ok := oc.schema.ScalarTypes[scalarName]; !ok {
oc.schema.ScalarTypes[scalarName] = *defaultScalarTypes[rest.ScalarJSON]
}
typeResult = createSchemaFromOpenAPISchema(typeSchema, scalarName)
typeResult = createSchemaFromOpenAPISchema(typeSchema)
return schema.NewNamedType(scalarName), typeResult, nil
}

Expand All @@ -303,16 +305,15 @@ func (oc *OAS2Builder) getSchemaType(typeSchema *base.Schema, apiPath string, fi
return nil, nil, errParameterSchemaEmpty(fieldPaths)
}
result = oc.buildScalarJSON()
typeResult = createSchemaFromOpenAPISchema(typeSchema, string(rest.ScalarJSON))
typeResult = createSchemaFromOpenAPISchema(typeSchema)
} else {
typeName := typeSchema.Type[0]
if isPrimitiveScalar(typeName) {
if isPrimitiveScalar(typeSchema.Type) {
scalarName := getScalarFromType(oc.schema, typeSchema.Type, typeSchema.Format, typeSchema.Enum, oc.trimPathPrefix(apiPath), fieldPaths)
result = schema.NewNamedType(scalarName)
typeResult = createSchemaFromOpenAPISchema(typeSchema, scalarName)
typeResult = createSchemaFromOpenAPISchema(typeSchema)
} else {
typeResult = createSchemaFromOpenAPISchema(typeSchema, "")
typeResult.Type = typeName
typeResult = createSchemaFromOpenAPISchema(typeSchema)
switch typeName {
case "object":
refName := utils.StringSliceToPascalCase(fieldPaths)
Expand All @@ -328,7 +329,6 @@ func (oc *OAS2Builder) getSchemaType(typeSchema *base.Schema, apiPath string, fi
object.Description = &typeSchema.Description
}

typeResult.Properties = make(map[string]rest.TypeSchema)
for prop := typeSchema.Properties.First(); prop != nil; prop = prop.Next() {
propName := prop.Key()
nullable := !slices.Contains(typeSchema.Required, propName)
Expand All @@ -337,13 +337,14 @@ func (oc *OAS2Builder) getSchemaType(typeSchema *base.Schema, apiPath string, fi
return nil, nil, err
}
objField := rest.ObjectField{
Type: propType.Encode(),
ObjectField: schema.ObjectField{
Type: propType.Encode(),
},
Rest: propApiSchema,
}
if propApiSchema.Description != "" {
objField.Description = &propApiSchema.Description
}
propApiSchema.Nullable = nullable
typeResult.Properties[propName] = *propApiSchema
object.Fields[propName] = objField

oc.typeUsageCounter.Add(getNamedType(propType, true, ""), 1)
Expand Down
Loading

0 comments on commit 7c81ae6

Please sign in to comment.