Skip to content

Commit

Permalink
feat: schema changes to support SumType (#1322)
Browse files Browse the repository at this point in the history
  • Loading branch information
deniseli authored Apr 24, 2024
1 parent 5c53193 commit c214d92
Show file tree
Hide file tree
Showing 16 changed files with 825 additions and 439 deletions.
2 changes: 1 addition & 1 deletion backend/controller/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func (c *ConsoleService) GetModules(ctx context.Context, req *connect.Request[pb
Config: c,
})

case *schema.Database, *schema.Enum:
case *schema.Database, *schema.Enum, *schema.SumType:
}
}

Expand Down
12 changes: 12 additions & 0 deletions backend/controller/ingress/ingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,18 @@ func validateValue(fieldType schema.Type, path path, value any, sch *schema.Sche
return fmt.Errorf("%s is not a valid variant of enum %s", value, fieldType)
}

case *schema.SumType:
for _, v := range d.Variants {
err := validateValue(v, path, value, sch)
if err == nil {
typeMatches = true
break
}
}
if !typeMatches {
return fmt.Errorf("%v is not a valid variant of sumtype %s", value, fieldType)
}

case *schema.Config, *schema.Database, *schema.Secret:

}
Expand Down
49 changes: 49 additions & 0 deletions backend/controller/ingress/ingress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -365,3 +365,52 @@ func TestEnumValidation(t *testing.T) {
}
}
}

func TestSumTypeValidation(t *testing.T) {
sch := &schema.Schema{
Modules: []*schema.Module{
{Name: "test", Decls: []schema.Decl{
&schema.SumType{
Name: "IntOrBool",
Variants: []schema.Type{&schema.Int{}, &schema.Bool{}},
},
&schema.Data{
Name: "SumTypeRequest",
Fields: []*schema.Field{
{Name: "message", Type: &schema.Ref{Name: "IntOrBool", Module: "test"}},
},
},
&schema.Data{
Name: "OptionalSumTypeRequest",
Fields: []*schema.Field{
{Name: "message", Type: &schema.Optional{
Type: &schema.Ref{Name: "IntOrBool", Module: "test"},
}},
},
},
}},
},
}

tests := []struct {
validateRoot *schema.Ref
req obj
err string
}{
{&schema.Ref{Name: "SumTypeRequest", Module: "test"}, obj{"message": true}, ""},
{&schema.Ref{Name: "SumTypeRequest", Module: "test"}, obj{"message": 10.0}, ""},
{&schema.Ref{Name: "OptionalSumTypeRequest", Module: "test"}, obj{}, ""},
{&schema.Ref{Name: "OptionalSumTypeRequest", Module: "test"}, obj{"message": 10.0}, ""},
{&schema.Ref{Name: "SumTypeRequest", Module: "test"}, obj{"message": "akxznc"},
"akxznc is not a valid variant of sumtype test.IntOrBool"},
}

for _, test := range tests {
err := validateValue(test.validateRoot, []string{test.validateRoot.String()}, test.req, sch)
if test.err == "" {
assert.NoError(t, err)
} else {
assert.Contains(t, err.Error(), test.err)
}
}
}
Loading

0 comments on commit c214d92

Please sign in to comment.