Skip to content

Commit

Permalink
Use field_name?: field_type for optional fields, instead of OneOf ext…
Browse files Browse the repository at this point in the history
…ension, see #5 (#13)
  • Loading branch information
dpup authored Apr 27, 2024
1 parent 729197b commit 8206ee7
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
2 changes: 2 additions & 0 deletions data/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ type Message struct {
Fields []*Field
// NonOneOfFields contains a subset of fields that are not in the one-of groups
NonOneOfFields []*Field
// OptionalFields contains a subset of fields that are optional
OptionalFields []*Field
// Message is the nested messages defined inside the message
Messages []*Message
// OneOfFieldsGroups is the grouped list of one of fields with same index. so that renderer can render the clearing of other fields on set.
Expand Down
8 changes: 6 additions & 2 deletions generator/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ type Base{{.Name}} = {
{{- range .NonOneOfFields}}
{{tsTypeKey .}}: {{tsTypeDef .}}
{{- end}}
{{- range .OptionalFields}}
{{tsTypeKey .}}: {{tsTypeDef .}}
{{- end}}
}
export type {{.Name}} = Base{{.Name}}
Expand Down Expand Up @@ -541,9 +544,10 @@ func include(t *template.Template) func(name string, data interface{}) (string,
func tsTypeKey(r *registry.Registry) func(field *data.Field) string {
return func(field *data.Field) string {
name := fieldName(r)(field.Name)
if !r.EmitUnpopulated {
if !r.EmitUnpopulated || field.IsOptional {
// When EmitUnpopulated is false, the gateway will return undefined for
// any zero value, so all fields may be undefined.
// any zero value, so all fields may be undefined. Optional fields, may
// also be undefined if unset.
return name + "?"
}
// When it is false, only optional fields can be undefined, however they are
Expand Down
6 changes: 5 additions & 1 deletion registry/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,18 @@ func (r *Registry) analyseField(fileData *data.File, msgData *data.Message, pack
if !fieldData.IsOneOfField {
msgData.NonOneOfFields = append(msgData.NonOneOfFields, fieldData)
}
if fieldData.IsOptional {
msgData.OptionalFields = append(msgData.OptionalFields, fieldData)
}

// if it's an external dependencies. store in the file data so that they can be collected when every file's finished
if isExternal {
fileData.ExternalDependingTypes = append(fileData.ExternalDependingTypes, fqTypeName)
}

// if it's a one of field. register the field data in the group of the same one of index.
if fieldData.IsOneOfField { // one of field
// internally, optional fields are modeled as OneOf, however, we don't want to include them here.
if fieldData.IsOneOfField && !fieldData.IsOptional {
index := f.GetOneofIndex()
fieldData.OneOfIndex = index
_, ok := msgData.OneOfFieldsGroups[index]
Expand Down

0 comments on commit 8206ee7

Please sign in to comment.