Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
k3s
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jacklull
k3s
Commits
c1096211
Commit
c1096211
authored
Oct 14, 2016
by
mbohlool
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Support optional tag in OpenAPI generator
parent
25afcc55
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
8 additions
and
26 deletions
+8
-26
types.go
.../go2idl/client-gen/test_apis/testgroup.k8s.io/v1/types.go
+1
-1
openapi.go
cmd/libs/go2idl/openapi-gen/generators/openapi.go
+7
-25
No files found.
cmd/libs/go2idl/client-gen/test_apis/testgroup.k8s.io/v1/types.go
View file @
c1096211
...
...
@@ -32,7 +32,7 @@ type TestType struct {
// +optional
api
.
ObjectMeta
`json:"metadata,omitempty"`
// +optional
Status
TestTypeStatus
`json:"status,omitempty"`
Status
TestTypeStatus
`json:"status,omitempty"`
}
type
TestTypeList
struct
{
...
...
cmd/libs/go2idl/openapi-gen/generators/openapi.go
View file @
c1096211
...
...
@@ -36,6 +36,7 @@ import (
// This is the comment tag that carries parameters for open API generation.
const
tagName
=
"k8s:openapi-gen"
const
tagOptional
=
"optional"
// Known values for the tag.
const
(
...
...
@@ -56,6 +57,11 @@ func hasOpenAPITagValue(comments []string, value string) bool {
return
false
}
func
hasOptionalTag
(
comments
[]
string
)
bool
{
tagValues
:=
types
.
ExtractCommentTags
(
"+"
,
comments
)[
tagOptional
]
return
tagValues
!=
nil
}
// NameSystems returns the name system used by the generators in this package.
func
NameSystems
()
namer
.
NameSystems
{
return
namer
.
NameSystems
{
...
...
@@ -225,30 +231,6 @@ func getReferableName(m *types.Member) string {
}
}
func
optionIndex
(
s
,
optionName
string
)
int
{
ret
:=
0
for
s
!=
""
{
var
next
string
i
:=
strings
.
Index
(
s
,
","
)
if
i
>=
0
{
s
,
next
=
s
[
:
i
],
s
[
i
+
1
:
]
}
if
s
==
optionName
{
return
ret
}
s
=
next
ret
++
}
return
-
1
}
func
isPropertyRequired
(
m
*
types
.
Member
)
bool
{
// A property is required if it does not have omitempty value in its json tag (documentation and implementation
// of json package requires omitempty to be at location 1 or higher.
// TODO: Move optional field definition from tags to comments.
return
optionIndex
(
reflect
.
StructTag
(
m
.
Tags
)
.
Get
(
"json"
),
"omitempty"
)
<
1
}
type
openAPITypeWriter
struct
{
*
generator
.
SnippetWriter
refTypes
map
[
string
]
*
types
.
Type
...
...
@@ -306,7 +288,7 @@ func (g openAPITypeWriter) generate(t *types.Type) error {
if
name
==
""
{
continue
}
if
isPropertyRequired
(
&
m
)
{
if
!
hasOptionalTag
(
m
.
CommentLines
)
{
required
=
append
(
required
,
name
)
}
if
err
:=
g
.
generateProperty
(
&
m
);
err
!=
nil
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment