Skip to content

Commit

Permalink
fix: config/schema wasn't being decoded from proto
Browse files Browse the repository at this point in the history
  • Loading branch information
alecthomas committed Mar 12, 2024
1 parent 11a63b1 commit 7223f5a
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 1 deletion.
8 changes: 8 additions & 0 deletions backend/schema/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,11 @@ func (s *Config) ToProto() protoreflect.ProtoMessage {
func (s *Config) schemaChildren() []Node { return []Node{s.Type} }

func (s *Config) schemaDecl() {}

func ConfigFromProto(p *schemapb.Config) *Config {
return &Config{
Pos: posFromProto(p.Pos),
Name: p.Name,
Type: typeToSchema(p.Type),
}
}
4 changes: 4 additions & 0 deletions backend/schema/protobuf_dec.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ func declListToSchema(s []*schemapb.Decl) []Decl {
out = append(out, DatabaseFromProto(n.Database))
case *schemapb.Decl_Enum:
out = append(out, EnumFromProto(n.Enum))
case *schemapb.Decl_Config:
out = append(out, ConfigFromProto(n.Config))
case *schemapb.Decl_Secret:
out = append(out, SecretFromProto(n.Secret))
}
}
return out
Expand Down
1 change: 1 addition & 0 deletions backend/schema/protobuf_enc.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ func valueToProto(v Value) *schemapb.Value {
switch t := v.(type) {
case *StringValue:
return &schemapb.Value{Value: &schemapb.Value_StringValue{StringValue: t.ToProto().(*schemapb.StringValue)}}

case *IntValue:
return &schemapb.Value{Value: &schemapb.Value_IntValue{IntValue: t.ToProto().(*schemapb.IntValue)}}
}
Expand Down
21 changes: 20 additions & 1 deletion backend/schema/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ func TestSchemaString(t *testing.T) {
expected := Builtins().String() + `
// A comment
module todo {
config configValue String
data CreateRequest {
name {String: String}? +alias json "rqn"
}
Expand All @@ -37,6 +39,8 @@ module todo {
when Time
}
secret secretValue String
verb create(todo.CreateRequest) todo.CreateResponse
+calls todo.destroy
Expand Down Expand Up @@ -89,6 +93,8 @@ func TestImports(t *testing.T) {
func TestVisit(t *testing.T) {
expected := `
Module
Config
String
Data
Field
Optional
Expand All @@ -109,6 +115,8 @@ Module
String
Field
Time
Secret
String
Verb
DataRef
DataRef
Expand Down Expand Up @@ -136,7 +144,7 @@ Module
return next()
})
assert.NoError(t, err)
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(actual.String()), "%s", actual.String())
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(actual.String()))
}

func TestParserRoundTrip(t *testing.T) {
Expand Down Expand Up @@ -355,6 +363,9 @@ func TestParseModule(t *testing.T) {
input := `
// A comment
module todo {
config configValue String
secret secretValue String
data CreateRequest {
name {String: String}? +alias json "rqn"
}
Expand Down Expand Up @@ -410,6 +421,14 @@ var testSchema = MustValidate(&Schema{
Name: "todo",
Comments: []string{"A comment"},
Decls: []Decl{
&Secret{
Name: "secretValue",
Type: &String{},
},
&Config{
Name: "configValue",
Type: &String{},
},
&Data{
Name: "CreateRequest",
Fields: []*Field{
Expand Down
8 changes: 8 additions & 0 deletions backend/schema/secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,11 @@ func (s *Secret) ToProto() protoreflect.ProtoMessage {
func (s *Secret) schemaChildren() []Node { return []Node{s.Type} }

func (s *Secret) schemaDecl() {}

func SecretFromProto(p *schemapb.Secret) *Secret {
return &Secret{
Pos: posFromProto(p.Pos),
Name: p.Name,
Type: typeToSchema(p.Type),
}
}

0 comments on commit 7223f5a

Please sign in to comment.