Add "generic" option to the "optional" configuration for handling nullable types (#252)
This is an implementation for #251, it adds a new `"generic"` option for the `"optional"` configuration, and a companion type `"optional_generic_type"` which is a fully qualified type with a placeholder `%` for the generic parameter. Co-authored-by: Dylan R. Johnston <[email protected]> Co-authored-by: Ben Kraft <[email protected]>
This commit is contained in:
co-authored by
Dylan R. Johnston
Ben Kraft
parent
94b6a71403
commit
c61d7acaa5
@@ -55,6 +55,7 @@ var (
|
||||
_ goType = (*goEnumType)(nil)
|
||||
_ goType = (*goStructType)(nil)
|
||||
_ goType = (*goInterfaceType)(nil)
|
||||
_ goType = (*goGenericType)(nil)
|
||||
)
|
||||
|
||||
type (
|
||||
@@ -80,6 +81,12 @@ type (
|
||||
// user (perhaps to handle nulls explicitly, or to avoid copying large
|
||||
// structures).
|
||||
goPointerType struct{ Elem goType }
|
||||
// goGenericType represent the Go type GoGenericRef[Elem], used when requested by the
|
||||
// user to box nullable data without using pointers or sentinel values
|
||||
goGenericType struct {
|
||||
GoGenericRef string
|
||||
Elem goType
|
||||
}
|
||||
)
|
||||
|
||||
// Opaque types are defined by the user; pointers and slices need no definition
|
||||
@@ -91,21 +98,27 @@ func (typ *goTypenameForBuiltinType) WriteDefinition(w io.Writer, g *generator)
|
||||
}
|
||||
func (typ *goSliceType) WriteDefinition(io.Writer, *generator) error { return nil }
|
||||
func (typ *goPointerType) WriteDefinition(io.Writer, *generator) error { return nil }
|
||||
func (typ *goGenericType) WriteDefinition(io.Writer, *generator) error { return nil }
|
||||
|
||||
func (typ *goOpaqueType) Reference() string { return typ.GoRef }
|
||||
func (typ *goTypenameForBuiltinType) Reference() string { return typ.GoTypeName }
|
||||
func (typ *goSliceType) Reference() string { return "[]" + typ.Elem.Reference() }
|
||||
func (typ *goPointerType) Reference() string { return "*" + typ.Elem.Reference() }
|
||||
func (typ *goGenericType) Reference() string {
|
||||
return fmt.Sprintf("%s[%s]", typ.GoGenericRef, typ.Elem.Reference())
|
||||
}
|
||||
|
||||
func (typ *goOpaqueType) SelectionSet() ast.SelectionSet { return nil }
|
||||
func (typ *goTypenameForBuiltinType) SelectionSet() ast.SelectionSet { return nil }
|
||||
func (typ *goSliceType) SelectionSet() ast.SelectionSet { return typ.Elem.SelectionSet() }
|
||||
func (typ *goPointerType) SelectionSet() ast.SelectionSet { return typ.Elem.SelectionSet() }
|
||||
func (typ *goGenericType) SelectionSet() ast.SelectionSet { return typ.Elem.SelectionSet() }
|
||||
|
||||
func (typ *goOpaqueType) GraphQLTypeName() string { return typ.GraphQLName }
|
||||
func (typ *goTypenameForBuiltinType) GraphQLTypeName() string { return typ.GraphQLName }
|
||||
func (typ *goSliceType) GraphQLTypeName() string { return typ.Elem.GraphQLTypeName() }
|
||||
func (typ *goPointerType) GraphQLTypeName() string { return typ.Elem.GraphQLTypeName() }
|
||||
func (typ *goGenericType) GraphQLTypeName() string { return typ.Elem.GraphQLTypeName() }
|
||||
|
||||
// goEnumType represents a Go named-string type used to represent a GraphQL
|
||||
// enum. In this case, we generate both the type (`type T string`) and also a
|
||||
@@ -529,6 +542,7 @@ func (typ *goOpaqueType) Unwrap() goType { return typ }
|
||||
func (typ *goTypenameForBuiltinType) Unwrap() goType { return typ }
|
||||
func (typ *goSliceType) Unwrap() goType { return typ.Elem.Unwrap() }
|
||||
func (typ *goPointerType) Unwrap() goType { return typ.Elem.Unwrap() }
|
||||
func (typ *goGenericType) Unwrap() goType { return typ.Elem.Unwrap() }
|
||||
func (typ *goEnumType) Unwrap() goType { return typ }
|
||||
func (typ *goStructType) Unwrap() goType { return typ }
|
||||
func (typ *goInterfaceType) Unwrap() goType { return typ }
|
||||
@@ -537,6 +551,7 @@ func (typ *goOpaqueType) SliceDepth() int { return 0 }
|
||||
func (typ *goTypenameForBuiltinType) SliceDepth() int { return 0 }
|
||||
func (typ *goSliceType) SliceDepth() int { return typ.Elem.SliceDepth() + 1 }
|
||||
func (typ *goPointerType) SliceDepth() int { return 0 }
|
||||
func (typ *goGenericType) SliceDepth() int { return 0 }
|
||||
func (typ *goEnumType) SliceDepth() int { return 0 }
|
||||
func (typ *goStructType) SliceDepth() int { return 0 }
|
||||
func (typ *goInterfaceType) SliceDepth() int { return 0 }
|
||||
@@ -545,6 +560,7 @@ func (typ *goOpaqueType) IsPointer() bool { return false }
|
||||
func (typ *goTypenameForBuiltinType) IsPointer() bool { return false }
|
||||
func (typ *goSliceType) IsPointer() bool { return typ.Elem.IsPointer() }
|
||||
func (typ *goPointerType) IsPointer() bool { return true }
|
||||
func (typ *goGenericType) IsPointer() bool { return false }
|
||||
func (typ *goEnumType) IsPointer() bool { return false }
|
||||
func (typ *goStructType) IsPointer() bool { return false }
|
||||
func (typ *goInterfaceType) IsPointer() bool { return false }
|
||||
|
||||
Reference in New Issue
Block a user