-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: schema changes to support SumType #1322
Changes from 12 commits
f1f0356
a789639
b0e670c
f43186c
99269ff
2712fc6
088eee8
feb2aab
8f5339e
2aa1d81
de64b8a
ab9f8ac
4e6b788
090ef5e
ceb45dc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,7 +12,7 @@ import ( | |
) | ||
|
||
var ( | ||
declUnion = []Decl{&Data{}, &Verb{}, &Database{}, &Enum{}, &Config{}, &Secret{}} | ||
declUnion = []Decl{&Data{}, &Verb{}, &Database{}, &Enum{}, &Config{}, &Secret{}, &SumType{}} | ||
nonOptionalTypeUnion = []Type{ | ||
&Int{}, &Float{}, &String{}, &Bytes{}, &Bool{}, &Time{}, &Array{}, | ||
&Map{}, &Any{}, &Unit{}, | ||
|
@@ -41,6 +41,7 @@ var ( | |
{Name: "String", Pattern: `"(?:\\.|[^"])*"`}, | ||
{Name: "Number", Pattern: `[0-9]+(?:\.[0-9]+)?`}, | ||
{Name: "Punct", Pattern: `[%/\-\_:[\]{}<>()*+?.,\\^$|#~!\'@]`}, | ||
{Name: "Equals", Pattern: `=`}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just add this to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done! |
||
}) | ||
|
||
commonParserOptions = []participle.Option{ | ||
|
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,70 @@ | ||||||||||
package schema | ||||||||||
|
||||||||||
import ( | ||||||||||
"fmt" | ||||||||||
"strings" | ||||||||||
|
||||||||||
schemapb "github.com/TBD54566975/ftl/backend/protos/xyz/block/ftl/v1/schema" | ||||||||||
"google.golang.org/protobuf/proto" | ||||||||||
) | ||||||||||
|
||||||||||
type SumType struct { | ||||||||||
Pos Position `parser:"" protobuf:"1,optional"` | ||||||||||
|
||||||||||
Comments []string `parser:"@Comment*" protobuf:"2"` | ||||||||||
Name string `parser:"'sumtype' @Ident Equals" protobuf:"3"` | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be just There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oo lala, thank you! |
||||||||||
Variants []Type `parser:"@@ ('|' @@)*" protobuf:"4"` | ||||||||||
} | ||||||||||
|
||||||||||
var _ Decl = (*SumType)(nil) | ||||||||||
var _ Symbol = (*SumType)(nil) | ||||||||||
|
||||||||||
func (s *SumType) Position() Position { return s.Pos } | ||||||||||
|
||||||||||
func (s *SumType) String() string { | ||||||||||
w := &strings.Builder{} | ||||||||||
fmt.Fprint(w, encodeComments(s.Comments)) | ||||||||||
typeNames := make([]string, len(s.Variants)) | ||||||||||
for i, v := range s.Variants { | ||||||||||
typeNames[i] = v.String() | ||||||||||
} | ||||||||||
fmt.Fprintf(w, "sumtype %s = %s", s.Name, strings.Join(typeNames, " | ")) | ||||||||||
return w.String() | ||||||||||
} | ||||||||||
|
||||||||||
func (s *SumType) schemaDecl() {} | ||||||||||
func (*SumType) schemaSymbol() {} | ||||||||||
func (s *SumType) schemaChildren() []Node { | ||||||||||
children := make([]Node, len(s.Variants)) | ||||||||||
for i, v := range s.Variants { | ||||||||||
children[i] = v | ||||||||||
} | ||||||||||
return children | ||||||||||
} | ||||||||||
func (s *SumType) ToProto() proto.Message { | ||||||||||
variants := make([]*schemapb.Type, len(s.Variants)) | ||||||||||
for i, v := range s.Variants { | ||||||||||
variants[i] = typeToProto(v) | ||||||||||
} | ||||||||||
return &schemapb.SumType{ | ||||||||||
Pos: posToProto(s.Pos), | ||||||||||
Comments: s.Comments, | ||||||||||
Name: s.Name, | ||||||||||
Variants: variants, | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we have a helper for this, so you don't have to construct your own list!
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oops, actually:
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done :D |
||||||||||
} | ||||||||||
} | ||||||||||
|
||||||||||
func (s *SumType) GetName() string { return s.Name } | ||||||||||
|
||||||||||
func SumTypeFromProto(s *schemapb.SumType) *SumType { | ||||||||||
variants := make([]Type, len(s.Variants)) | ||||||||||
for i, v := range s.Variants { | ||||||||||
variants[i] = typeToSchema(v) | ||||||||||
} | ||||||||||
return &SumType{ | ||||||||||
Pos: posFromProto(s.Pos), | ||||||||||
Name: s.Name, | ||||||||||
Comments: s.Comments, | ||||||||||
Variants: variants, | ||||||||||
} | ||||||||||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice!
Though, this should be using
{"kind": xyz, "value": ...}
, or is that in a followup?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fyi - from feedback during the design sync we actually lifted the original struct out of
value
, so it'll just be an additional field named@_kind
(prefixed with@_
to avoid conflicts) at the top level nowThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mmm,
@_kind
can still conflict though if the sum type variant is a map:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah good point, i misunderstood your prior comment from the design sync. we can go with the kind/value approach then