-
Notifications
You must be signed in to change notification settings - Fork 205
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
Support x-kubernetes-v2-spec extension #166
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 |
---|---|---|
|
@@ -242,6 +242,16 @@ func methodReturnsValue(mt *types.Type, pkg, name string) bool { | |
return r.Name.Name == name && r.Name.Package == pkg | ||
} | ||
|
||
func hasOpenAPIV3DefinitionMethod(t *types.Type) bool { | ||
for mn, mt := range t.Methods { | ||
if mn != "OpenAPIV3Definition" { | ||
continue | ||
} | ||
return methodReturnsValue(mt, openAPICommonPackagePath, "OpenAPIDefinition") | ||
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. OpenAPIDefinition or OpenAPIV3Definition? 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. it's the name of struct |
||
} | ||
return false | ||
} | ||
|
||
func hasOpenAPIDefinitionMethod(t *types.Type) bool { | ||
for mn, mt := range t.Methods { | ||
if mn != "OpenAPIDefinition" { | ||
|
@@ -304,9 +314,21 @@ func (g openAPITypeWriter) generateCall(t *types.Type) error { | |
case types.Struct: | ||
args := argsFromType(t) | ||
g.Do("\"$.$\": ", t.Name) | ||
if hasOpenAPIDefinitionMethod(t) { | ||
|
||
hasV2Definition := hasOpenAPIDefinitionMethod(t) | ||
hasV2DefinitionTypeAndFormat := hasOpenAPIDefinitionMethods(t) | ||
hasV3Definition := hasOpenAPIV3DefinitionMethod(t) | ||
|
||
switch { | ||
case hasV2DefinitionTypeAndFormat: | ||
g.Do(nameTmpl+"(ref),\n", args) | ||
case hasV2Definition && hasV3Definition: | ||
g.Do("common.EmbedOpenAPIDefinitionIntoV2Extension($.type|raw${}.OpenAPIV3Definition(), $.type|raw${}.OpenAPIDefinition()),\n", args) | ||
case hasV2Definition: | ||
g.Do("$.type|raw${}.OpenAPIDefinition(),\n", args) | ||
} else { | ||
case hasV3Definition: | ||
g.Do("$.type|raw${}.OpenAPIV3Definition(),\n", args) | ||
default: | ||
g.Do(nameTmpl+"(ref),\n", args) | ||
} | ||
} | ||
|
@@ -317,14 +339,30 @@ func (g openAPITypeWriter) generate(t *types.Type) error { | |
// Only generate for struct type and ignore the rest | ||
switch t.Kind { | ||
case types.Struct: | ||
if hasOpenAPIDefinitionMethod(t) { | ||
hasV2Definition := hasOpenAPIDefinitionMethod(t) | ||
hasV2DefinitionTypeAndFormat := hasOpenAPIDefinitionMethods(t) | ||
hasV3Definition := hasOpenAPIV3DefinitionMethod(t) | ||
|
||
if hasV2Definition || (hasV3Definition && !hasV2DefinitionTypeAndFormat) { | ||
// already invoked directly | ||
return nil | ||
} | ||
|
||
args := argsFromType(t) | ||
g.Do("func "+nameTmpl+"(ref $.ReferenceCallback|raw$) $.OpenAPIDefinition|raw$ {\n", args) | ||
if hasOpenAPIDefinitionMethods(t) { | ||
switch { | ||
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. no separate case for 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.
hasV3Definition case is short-circuited at L329, do you think about adding a case for hasV3Definition and commenting 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. no, we can leave it as it is now. |
||
case hasV2DefinitionTypeAndFormat && hasV3Definition: | ||
g.Do("return common.EmbedOpenAPIDefinitionIntoV2Extension($.type|raw${}.OpenAPIV3Definition(), $.OpenAPIDefinition|raw${\n"+ | ||
"Schema: spec.Schema{\n"+ | ||
"SchemaProps: spec.SchemaProps{\n", args) | ||
g.generateDescription(t.CommentLines) | ||
g.Do("Type:$.type|raw${}.OpenAPISchemaType(),\n"+ | ||
"Format:$.type|raw${}.OpenAPISchemaFormat(),\n"+ | ||
"},\n"+ | ||
"},\n"+ | ||
"})\n}\n\n", args) | ||
return nil | ||
case hasV2DefinitionTypeAndFormat: | ||
g.Do("return $.OpenAPIDefinition|raw${\n"+ | ||
"Schema: spec.Schema{\n"+ | ||
"SchemaProps: spec.SchemaProps{\n", args) | ||
|
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.
for now, i cant think of a better name for this