From 7223f5a4f9cd42c36282b1ef86dbae745a9bc92d Mon Sep 17 00:00:00 2001 From: Alec Thomas Date: Tue, 12 Mar 2024 16:28:32 +1100 Subject: [PATCH] fix: config/schema wasn't being decoded from proto --- backend/schema/config.go | 8 ++++++++ backend/schema/protobuf_dec.go | 4 ++++ backend/schema/protobuf_enc.go | 1 + backend/schema/schema_test.go | 21 ++++++++++++++++++++- backend/schema/secret.go | 8 ++++++++ 5 files changed, 41 insertions(+), 1 deletion(-) diff --git a/backend/schema/config.go b/backend/schema/config.go index a4be81b5f..be16bc494 100644 --- a/backend/schema/config.go +++ b/backend/schema/config.go @@ -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), + } +} diff --git a/backend/schema/protobuf_dec.go b/backend/schema/protobuf_dec.go index 7ae81589b..347e8518a 100644 --- a/backend/schema/protobuf_dec.go +++ b/backend/schema/protobuf_dec.go @@ -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 diff --git a/backend/schema/protobuf_enc.go b/backend/schema/protobuf_enc.go index 3bd44df5b..b5b06a588 100644 --- a/backend/schema/protobuf_enc.go +++ b/backend/schema/protobuf_enc.go @@ -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)}} } diff --git a/backend/schema/schema_test.go b/backend/schema/schema_test.go index bf1a2ce3e..72167de9a 100644 --- a/backend/schema/schema_test.go +++ b/backend/schema/schema_test.go @@ -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" } @@ -37,6 +39,8 @@ module todo { when Time } + secret secretValue String + verb create(todo.CreateRequest) todo.CreateResponse +calls todo.destroy @@ -89,6 +93,8 @@ func TestImports(t *testing.T) { func TestVisit(t *testing.T) { expected := ` Module + Config + String Data Field Optional @@ -109,6 +115,8 @@ Module String Field Time + Secret + String Verb DataRef DataRef @@ -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) { @@ -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" } @@ -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{ diff --git a/backend/schema/secret.go b/backend/schema/secret.go index bbf688244..583f95fa5 100644 --- a/backend/schema/secret.go +++ b/backend/schema/secret.go @@ -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), + } +}