diff --git a/gen/ir/constructors.go b/gen/ir/constructors.go index 1ff970990..b4f845244 100644 --- a/gen/ir/constructors.go +++ b/gen/ir/constructors.go @@ -23,7 +23,7 @@ func Array(item *Type, sem NilSemantic, schema *jsonschema.Schema) *Type { func Alias(name string, to *Type) *Type { return &Type{ Kind: KindAlias, - Name: name, + name: name, AliasTo: to, Validators: to.Validators, Features: to.CloneFeatures(), @@ -32,7 +32,7 @@ func Alias(name string, to *Type) *Type { func Interface(name string) *Type { return &Type{ - Name: name, + name: name, Kind: KindInterface, InterfaceMethods: map[string]struct{}{}, Implementations: map[*Type]struct{}{}, @@ -49,9 +49,8 @@ func Pointer(to *Type, sem NilSemantic) *Type { } func Generic(name string, of *Type, v GenericVariant) *Type { - name = v.Name() + name return &Type{ - Name: name, + name: name, Kind: KindGeneric, GenericOf: of, GenericVariant: v, @@ -69,7 +68,7 @@ func Any(schema *jsonschema.Schema) *Type { func Stream(name string, schema *jsonschema.Schema) *Type { return &Type{ Kind: KindStream, - Name: name, + name: name, Schema: schema, } } diff --git a/gen/ir/type.go b/gen/ir/type.go index 159f45b5b..311860b81 100644 --- a/gen/ir/type.go +++ b/gen/ir/type.go @@ -65,7 +65,7 @@ func (s SumSpec) PickMappingEntryFor(t *Type) *SumSpecMap { type Type struct { Doc string // ogen documentation Kind Kind // kind - Name string // only for struct, alias, interface, enum, stream, generic, map, sum + name string // only for struct, alias, interface, enum, stream, generic, map, sum Primitive PrimitiveType // only for primitive, enum AliasTo *Type // only for alias PointerTo *Type // only for pointer @@ -94,6 +94,23 @@ type Type struct { Features []string } +type NameOpt struct { + CallerContext +} + +func (t Type) Name(o NameOpt) string { + if t.Is(KindGeneric) { + isFeatureTurnedOn := true // generate non Opt* for default valued params, or not + if isFeatureTurnedOn && o.ServerClient == ServerClientServer && t.Default().Set { + return t.name + } + + return t.GenericVariant.Name() + t.name + } + + return t.name +} + // GoDoc returns type godoc. func (t Type) GoDoc() []string { s := t.Schema @@ -165,19 +182,37 @@ func (t *Type) Is(vs ...Kind) bool { return false } +type GoOpt struct { + CallerContext +} + +// ServerClient tells if template method being called is for client or server side code generation +type ServerClient int + +const ( + ServerClientNone = iota + ServerClientClient + ServerClientServer +) + +// CallerContext holds information about the caller side context of the template method being invoked +type CallerContext struct { + ServerClient +} + // Go returns valid Go type for this Type. -func (t *Type) Go() string { +func (t *Type) Go(opt GoOpt) string { switch t.Kind { case KindPrimitive: return t.Primitive.String() case KindAny: return "jx.Raw" case KindArray: - return "[]" + t.Item.Go() + return "[]" + t.Item.Go(opt) case KindPointer: - return "*" + t.PointerTo.Go() + return "*" + t.PointerTo.Go(opt) case KindStruct, KindMap, KindAlias, KindInterface, KindGeneric, KindEnum, KindSum, KindStream: - return t.Name + return t.Name(NameOpt{CallerContext{opt.CallerContext.ServerClient}}) default: panic(fmt.Sprintf("unexpected kind: %s", t.Kind)) }