From 30f8d1802b2b29292fc262f09b6ae4f2014fc0c1 Mon Sep 17 00:00:00 2001 From: Colleen Murphy Date: Sat, 1 May 2021 15:46:03 -0700 Subject: [PATCH] Add 'nullablestring' field type Currently, `*string` fields in Go structs are converted to `{type: string, nullable: true}` in the API schema, which is right, but then converted down to just `string` when converted to generated client structs. Without this patch, there is no way to express that `*string`s should stay as `*string`s when run through the generator, because the 'nullable' flag is ignored. Compare this to `*bool`s, which become `boolean` in the schema and then correctly converted back to *bool in the generator. This patch adds a backwards-compatible way to opt in to converting `*string`s in the original struct to *strings in the generated struct. --- generator/generator.go | 2 ++ parse/builder/builder.go | 2 ++ 2 files changed, 4 insertions(+) diff --git a/generator/generator.go b/generator/generator.go index 258ee7f7..44b7591c 100644 --- a/generator/generator.go +++ b/generator/generator.go @@ -81,6 +81,8 @@ func getTypeString(nullable bool, typeName string, schema *types.Schema, schemas return "string" case "hostname": return "string" + case "nullablestring": + return "*string" default: if schema != nil && schemas != nil { otherSchema := schemas.Schema(&schema.Version, typeName) diff --git a/parse/builder/builder.go b/parse/builder/builder.go index f27b09e3..dcc264d4 100644 --- a/parse/builder/builder.go +++ b/parse/builder/builder.go @@ -382,6 +382,8 @@ func ConvertSimple(fieldType string, value interface{}, op Operation) (interface return convert.ToString(value), nil case "reference": return convert.ToString(value), nil + case "nullablestring": + return convert.ToString(value), nil } return nil, ErrComplexType