Skip to content

Commit

Permalink
Add a yaml test schema
Browse files Browse the repository at this point in the history
  • Loading branch information
Zaid-Ajaj committed Dec 14, 2022
1 parent 6d642f4 commit 5e474de
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 11 deletions.
18 changes: 9 additions & 9 deletions pkg/codegen/schema/schema.go
Expand Up @@ -1579,9 +1579,9 @@ func (returnTypeSpec *ReturnTypeSpec) UnmarshalJSON(inputJSON []byte) error {
return nil
}

func (returnTypeSpec *ReturnTypeSpec) UnmarshalYAML(node *yaml.Node) error {
func (returnTypeSpec *ReturnTypeSpec) UnmarshalYAML(inputData []byte) error {
var data map[string]interface{}
if err := node.Decode(&data); err != nil {
if err := yaml.Unmarshal(inputData, &data); err != nil {
return err
}

Expand All @@ -1592,11 +1592,11 @@ func (returnTypeSpec *ReturnTypeSpec) UnmarshalYAML(node *yaml.Node) error {
var objectSpec *ObjectTypeSpec
var typeSpec *TypeSpec
if _, hasProperties := data["properties"]; hasProperties {
if err := node.Decode(&objectSpec); err != nil {
if err := yaml.Unmarshal(inputData, &objectSpec); err != nil {
return err
}
} else {
if err := node.Decode(&typeSpec); err != nil {
if err := yaml.Unmarshal(inputData, &typeSpec); err != nil {
return err
}
}
Expand Down Expand Up @@ -1642,7 +1642,7 @@ func emptyObject(data RawMessage) (bool, error) {
return len(*objectData) == 0, nil
}

func UnmarshalFunctionSpec(funcSpec *FunctionSpec, data map[string]RawMessage) error {
func unmarshalFunctionSpec(funcSpec *FunctionSpec, data map[string]RawMessage) error {
if description, ok := data["description"]; ok {
if err := json.Unmarshal(description, &funcSpec.Description); err != nil {
return err
Expand Down Expand Up @@ -1709,17 +1709,17 @@ func (funcSpec *FunctionSpec) UnmarshalJSON(inputJSON []byte) error {
if err := json.Unmarshal(inputJSON, &data); err != nil {
return err
}
return UnmarshalFunctionSpec(funcSpec, data)
return unmarshalFunctionSpec(funcSpec, data)
}

// UnmarshalYAML is custom unmarshalling logic for FunctionSpec so that we can derive Outputs from ReturnType
// which otherwise isn't possible when both are retrieved from the same JSON field
func (funcSpec *FunctionSpec) UnmarshalYAML(node *yaml.Node) error {
func (funcSpec *FunctionSpec) UnmarshalYAML(input []byte) error {
var data map[string]RawMessage
if err := node.Decode(&data); err != nil {
if err := yaml.Unmarshal(input, &data); err != nil {
return err
}
return UnmarshalFunctionSpec(funcSpec, data)
return unmarshalFunctionSpec(funcSpec, data)
}

func (funcSpec FunctionSpec) MarshalFunctionSpec() map[string]interface{} {
Expand Down
22 changes: 20 additions & 2 deletions pkg/codegen/schema/schema_test.go
Expand Up @@ -17,6 +17,7 @@ package schema

import (
"encoding/json"
"fmt"
"io/ioutil"
"net/url"
"path/filepath"
Expand All @@ -27,6 +28,7 @@ import (

"github.com/blang/semver"
"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v2"
)

func readSchemaFile(file string) (pkgSpec PackageSpec) {
Expand All @@ -36,8 +38,16 @@ func readSchemaFile(file string) (pkgSpec PackageSpec) {
panic(err)
}

if err = json.Unmarshal(schemaBytes, &pkgSpec); err != nil {
panic(err)
if strings.HasSuffix(file, ".json") {
if err = json.Unmarshal(schemaBytes, &pkgSpec); err != nil {
panic(err)
}
} else if strings.HasSuffix(file, ".yaml") || strings.HasSuffix(file, ".yml") {
if err = yaml.Unmarshal(schemaBytes, &pkgSpec); err != nil {
panic(err)
}
} else {
panic(fmt.Sprintf("unknown schema file extension while parsing %s", file))
}

return pkgSpec
Expand Down Expand Up @@ -402,6 +412,14 @@ func TestMethods(t *testing.T) {
assert.Equal(t, pkg.Functions[0].ReturnType, NumberType)
},
},
{
filename: "good-simplified-methods.yml",
validator: func(pkg *Package) {
assert.Len(t, pkg.Functions, 1)
assert.NotNil(t, pkg.Functions[0].ReturnType, "There should be a return type")
assert.Equal(t, pkg.Functions[0].ReturnType, NumberType)
},
},
{
filename: "bad-methods-1.json",
expectedError: "unknown function xyz:index:Foo/bar",
Expand Down
@@ -0,0 +1,14 @@
name: xyz
version: 0.0.1
functions:
xyz:index:basic:
multiArgumentInputs:
- value
inputs:
properties:
value:
type: number
required:
- value
outputs:
type: number

0 comments on commit 5e474de

Please sign in to comment.