Skip to content

Commit

Permalink
Use edgedb.Optional in generated optional structs (#283)
Browse files Browse the repository at this point in the history
This fixes a bug in the generator that didn't make optional structs
optional.

Also, for generated code, this makes values in slices never be their
optional variant.
  • Loading branch information
fmoor authored Sep 22, 2023
1 parent 99c07ac commit 9d0e3ed
Show file tree
Hide file tree
Showing 11 changed files with 24 additions and 16 deletions.
16 changes: 8 additions & 8 deletions cmd/edgeql-go/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ func generateSlice(
) ([]goType, []string, error) {
types, imports, err := generateType(
desc.Fields[0].Desc,
desc.Fields[0].Required,
true,
path,
mixedCaps,
)
Expand All @@ -204,7 +204,7 @@ func generateSliceV2(
) ([]goType, []string, error) {
types, imports, err := generateTypeV2(
&desc.Fields[0].Desc,
desc.Fields[0].Required,
true,
path,
mixedCaps,
)
Expand All @@ -223,7 +223,7 @@ func generateObject(
mixedCaps bool,
) ([]goType, []string, error) {
var imports []string
typ := goStruct{Name: nameFromPath(path)}
typ := goStruct{Name: nameFromPath(path), Required: required}
types := []goType{&typ}

for _, field := range desc.Fields {
Expand Down Expand Up @@ -258,12 +258,12 @@ func generateObject(

func generateObjectV2(
desc *descriptor.V2,
_ bool,
required bool,
path []string,
mixedCaps bool,
) ([]goType, []string, error) {
var imports []string
typ := goStruct{Name: nameFromPath(path)}
typ := goStruct{Name: nameFromPath(path), Required: required}
types := []goType{&typ}

for _, field := range desc.Fields {
Expand Down Expand Up @@ -303,7 +303,7 @@ func generateTuple(
mixedCaps bool,
) ([]goType, []string, error) {
var imports []string
typ := &goStruct{Name: nameFromPath(path)}
typ := &goStruct{Name: nameFromPath(path), Required: required}
types := []goType{typ}

for _, field := range desc.Fields {
Expand Down Expand Up @@ -339,12 +339,12 @@ func generateTuple(

func generateTupleV2(
desc *descriptor.V2,
_ bool,
required bool,
path []string,
mixedCaps bool,
) ([]goType, []string, error) {
var imports []string
typ := &goStruct{Name: nameFromPath(path)}
typ := &goStruct{Name: nameFromPath(path), Required: required}
types := []goType{typ}

for _, field := range desc.Fields {
Expand Down
3 changes: 3 additions & 0 deletions cmd/edgeql-go/templates/struct.template
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,8 @@
// is part of the return type for
// {{.QueryFuncName}}()
type {{.Name}} struct {
{{- if not .Required}}
edgedb.Optional
{{- end}}
{{range .Fields}} {{.GoName}} {{.Type}} `{{.Tag}}`
{{end}}}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ var selectObjectCmd string
// is part of the return type for
// selectObject()
type selectObjectResult struct {
edgedb.Optional
Name string `edgedb:"Name"`
Language string `edgedb:"Language"`
Params []selectObjectResultParamsItem `edgedb:"Params"`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ var selectArrayCmd string
func selectArray(
ctx context.Context,
client *edgedb.Client,
) ([]edgedb.OptionalStr, error) {
var result []edgedb.OptionalStr
) ([]string, error) {
var result []string

err := client.QuerySingle(
ctx,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ var selectScalarsCmd string
func selectScalars(
ctx context.Context,
client *edgedb.Client,
) ([]edgedb.OptionalMemory, error) {
var result []edgedb.OptionalMemory
) ([]edgedb.Memory, error) {
var result []edgedb.Memory

err := client.Query(
ctx,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type selectTupleResult struct {
// is part of the return type for
// selectTuple()
type selectTupleResult2Item struct {
edgedb.Optional
Element0 edgedb.OptionalStr `edgedb:"0"`
Element1 edgedb.OptionalInt64 `edgedb:"1"`
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ var selectObjectCmd string
// is part of the return type for
// selectObject()
type selectObjectResult struct {
edgedb.Optional
Name string `edgedb:"Name"`
Language string `edgedb:"Language"`
Params []selectObjectResultParamsItem `edgedb:"Params"`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ var selectArrayCmd string
func selectArray(
ctx context.Context,
client *edgedb.Client,
) ([]edgedb.OptionalStr, error) {
var result []edgedb.OptionalStr
) ([]string, error) {
var result []string

err := client.QuerySingle(
ctx,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ var selectScalarsCmd string
func selectScalars(
ctx context.Context,
client *edgedb.Client,
) ([]edgedb.OptionalMemory, error) {
var result []edgedb.OptionalMemory
) ([]edgedb.Memory, error) {
var result []edgedb.Memory

err := client.Query(
ctx,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type selectTupleResult struct {
// is part of the return type for
// selectTuple()
type selectTupleResult2Item struct {
edgedb.Optional
Element0 edgedb.OptionalStr `edgedb:"0"`
Element1 edgedb.OptionalInt64 `edgedb:"1"`
}
Expand Down
1 change: 1 addition & 0 deletions cmd/edgeql-go/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ type goStruct struct {
Name string
QueryFuncName string
Fields []goStructField
Required bool
}

func (t *goStruct) Reference() string { return t.Name }

0 comments on commit 9d0e3ed

Please sign in to comment.