Skip to content

Commit

Permalink
TER-388 made changes to get the default input and output values for d…
Browse files Browse the repository at this point in the history
…ependency (#164)
  • Loading branch information
26christy authored Dec 13, 2023
1 parent 5dc63c5 commit 2ec56ff
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/pkg/jsonschema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/cldcvr/terrarium/src/pkg/pb/terrariumpb"
"github.com/rotisserie/eris"
"github.com/xeipuuv/gojsonschema"
"google.golang.org/protobuf/types/known/structpb"
)

type Node struct {
Expand Down Expand Up @@ -158,5 +159,30 @@ func (n *Node) ToProto() *terrariumpb.JSONSchema {
}
}

if n.Default != nil {
switch defaultValue := n.Default.(type) {
case float64:
protoSchema.Default = &structpb.Value{
Kind: &structpb.Value_NumberValue{
NumberValue: defaultValue,
},
}
case int:
protoSchema.Default = &structpb.Value{
Kind: &structpb.Value_NumberValue{
NumberValue: float64(defaultValue),
},
}
case string:
protoSchema.Default = &structpb.Value{
Kind: &structpb.Value_StringValue{
StringValue: defaultValue,
},
}
// Add more cases as needed for other types
default:
protoSchema.Default = nil
}
}
return protoSchema
}
116 changes: 116 additions & 0 deletions src/pkg/jsonschema/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/xeipuuv/gojsonschema"
"google.golang.org/protobuf/types/known/structpb"
)

func TestNode_Compile(t *testing.T) {
Expand Down Expand Up @@ -254,6 +255,121 @@ func TestNode_ToProto(t *testing.T) {
},
wantErr: false,
},
{
name: "JSONSchema with Default float",
jsn: &Node{
Title: "Test Title",
Description: "Test Description",
Type: "object",
Properties: map[string]*Node{
"property1": {
Title: "Property 1",
Type: "string",
},
"property2": {
Title: "Property 2",
Type: "integer",
},
},
Default: 80.0, // Adding a Default value of type float64
},
validator: func(t *testing.T, result *terrariumpb.JSONSchema) {
require.NotNil(t, result)
assert.Equal(t, "Test Title", result.Title)
assert.Equal(t, "Test Description", result.Description)
assert.Equal(t, "object", result.Type)
require.Len(t, result.Properties, 2)
assert.NotNil(t, result.Properties["property1"])
assert.NotNil(t, result.Properties["property2"])

// Asserting the Default field value
require.NotNil(t, result.Default)
assert.IsType(t, &structpb.Value_NumberValue{}, result.Default.Kind)
defaultValue := result.Default.GetNumberValue()
assert.Equal(t, float64(80.0), defaultValue)
},
wantErr: false,
},
{
name: "JSONSchema with Default int",
jsn: &Node{
Title: "Test Title",
Description: "Test Description",
Type: "object",
Properties: map[string]*Node{
"property1": {
Title: "Property 1",
Type: "string",
},
"property2": {
Title: "Property 2",
Type: "integer",
},
},
Default: 100, // Adding a Default value of type int
},
validator: func(t *testing.T, result *terrariumpb.JSONSchema) {
require.NotNil(t, result)
assert.NotNil(t, result.Default)

intValue, ok := result.Default.GetKind().(*structpb.Value_NumberValue)
require.True(t, ok, "Expected Default to be of type int")
assert.Equal(t, float64(100), intValue.NumberValue)
},
wantErr: false,
},
{
name: "JSONSchema with Default string",
jsn: &Node{
Title: "Test Title",
Description: "Test Description",
Type: "object",
Properties: map[string]*Node{
"property1": {
Title: "Property 1",
Type: "string",
},
"property2": {
Title: "Property 2",
Type: "integer",
},
},
Default: "hello", // Adding a Default value of type string
},
validator: func(t *testing.T, result *terrariumpb.JSONSchema) {
require.NotNil(t, result)
assert.NotNil(t, result.Default)

stringValue, ok := result.Default.GetKind().(*structpb.Value_StringValue)
require.True(t, ok, "Expected Default to be of type string")
assert.Equal(t, "hello", stringValue.StringValue)
},
wantErr: false,
},
{
name: "JSONSchema with nil Default",
jsn: &Node{
Title: "Test Title",
Description: "Test Description",
Type: "object",
Properties: map[string]*Node{
"property1": {
Title: "Property 1",
Type: "string",
},
"property2": {
Title: "Property 2",
Type: "integer",
},
},
Default: nil, // Setting Default as nil
},
validator: func(t *testing.T, result *terrariumpb.JSONSchema) {
require.NotNil(t, result)
assert.Nil(t, result.Default)
},
wantErr: false,
},
}

for _, tt := range tests {
Expand Down

2 comments on commit 2ec56ff

@26christy
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Job Name cldcvr/terrarium integration pipeline
Job Status failed
Total Steps 2
Progress 1
Message An unexpected error occurred: build has finished with status: Failure
View on Code Pipes

@26christy
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Job Name cldcvr/terrarium integration pipeline
Job Status failed
Total Steps 2
Progress 1
Message An unexpected error occurred: build has finished with status: Failure
View on Code Pipes

Please sign in to comment.