-
Notifications
You must be signed in to change notification settings - Fork 87
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
WIP: capability to generate non Opt* struct for parameters with default value #1200
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -65,7 +65,7 @@ | |
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 @@ | |
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 | ||
} | ||
Comment on lines
+103
to
+106
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is where the main logic resides, if the feature is turned on and the call is made for the server side (and the param has default value), do not add The reason behind adding a flag rather than making it ogen default behavior is: https://t.me/ogen_dev/9482 |
||
|
||
return t.GenericVariant.Name() + t.name | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. moved the only anomaly that happens for |
||
} | ||
|
||
return t.name | ||
} | ||
|
||
// GoDoc returns type godoc. | ||
func (t Type) GoDoc() []string { | ||
s := t.Schema | ||
|
@@ -129,7 +146,7 @@ | |
var b strings.Builder | ||
b.WriteString(string(t.Kind)) | ||
b.WriteRune('(') | ||
b.WriteString(t.Go()) | ||
Check failure on line 149 in gen/ir/type.go GitHub Actions / test
Check failure on line 149 in gen/ir/type.go GitHub Actions / test (386, ubuntu-latest)
Check failure on line 149 in gen/ir/type.go GitHub Actions / test (amd64, ubuntu-latest)
Check failure on line 149 in gen/ir/type.go GitHub Actions / test (amd64, macos-latest)
Check failure on line 149 in gen/ir/type.go GitHub Actions / test (amd64, ubuntu-latest, -race)
Check failure on line 149 in gen/ir/type.go GitHub Actions / Analyze (go)
Check failure on line 149 in gen/ir/type.go GitHub Actions / golangci-lint
|
||
b.WriteRune(')') | ||
if s := t.Schema; s != nil { | ||
if ref := s.Ref.String(); ref != "" { | ||
|
@@ -165,19 +182,37 @@ | |
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 | ||
) | ||
Comment on lines
+189
to
+196
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. adding |
||
|
||
// CallerContext holds information about the caller side context of the template method being invoked | ||
type CallerContext struct { | ||
ServerClient | ||
} | ||
Comment on lines
+198
to
+201
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Defining a struct to hold the caller context, as the same mechanism might be necessary when implementing readonly/writeonly to store the information of if the method is called for write (POST, PUT etc) or read (GET etc) operation |
||
|
||
// Go returns valid Go type for this Type. | ||
func (t *Type) Go() string { | ||
func (t *Type) Go(opt GoOpt) string { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This strategy will require us to basically rewrite many template methods to take an argument and pass that down the call stack. |
||
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)) | ||
} | ||
|
@@ -243,7 +278,7 @@ | |
case KindPointer: | ||
return t.PointerTo.NamePostfix() + "Pointer" | ||
case KindStruct, KindMap, KindAlias, KindInterface, KindGeneric, KindEnum, KindSum, KindStream: | ||
return t.Name | ||
Check failure on line 281 in gen/ir/type.go GitHub Actions / test
Check failure on line 281 in gen/ir/type.go GitHub Actions / test (386, ubuntu-latest)
Check failure on line 281 in gen/ir/type.go GitHub Actions / test (amd64, ubuntu-latest)
Check failure on line 281 in gen/ir/type.go GitHub Actions / test (amd64, macos-latest)
Check failure on line 281 in gen/ir/type.go GitHub Actions / test (amd64, ubuntu-latest, -race)
Check failure on line 281 in gen/ir/type.go GitHub Actions / Analyze (go)
Check failure on line 281 in gen/ir/type.go GitHub Actions / golangci-lint
|
||
default: | ||
panic(fmt.Sprintf("unexpected kind: %s", t.Kind)) | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
make the field private and add an exported method
Name
instead