10BC0 api/types: generate with latest go-swagger · moby/moby@2783f80 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2783f80

Browse files
committed
api/types: generate with latest go-swagger
Override some of the templates to suppress emitting unwanted validation and marshal/unmarshal code. Signed-off-by: Cory Snider <csnider@mirantis.com>
1 parent 5f121ce commit 2783f80

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+439
-35
lines changed

Dockerfile

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -81,26 +81,14 @@ FROM distribution/distribution:$REGISTRY_VERSION AS registry
8181
RUN mkdir /build && mv /bin/registry /build/registry
8282

8383
# go-swagger
84-
FROM base AS swagger-src
85-
WORKDIR /usr/src/swagger
86-
# Currently uses a fork from https://github.com/kolyshkin/go-swagger/tree/golang-1.13-fix
87-
# TODO: move to under moby/ or fix upstream go-swagger to work for us.
88-
RUN git init . && git remote add origin "https://github.com/kolyshkin/go-swagger.git"
89-
# GO_SWAGGER_COMMIT specifies the version of the go-swagger binary to build and
90-
# install. Go-swagger is used in CI for validating swagger.yaml in hack/validate/swagger-gen
91-
ARG GO_SWAGGER_COMMIT=c56166c036004ba7a3a321e5951ba472b9ae298c
92-
RUN git fetch -q --depth 1 origin "${GO_SWAGGER_COMMIT}" && git checkout -q FETCH_HEAD
93-
9484
FROM base AS swagger
9585
WORKDIR /go/src/github.com/go-swagger/go-swagger
9686
ARG TARGETPLATFORM
97-
ENV GO111MODULE=off
98-
RUN --mount=from=swagger-src,src=/usr/src/swagger,rw \
99-
--mount=type=cache,target=/root/.cache/go-build,id=swagger-build-$TARGETPLATFORM \
87+
RUN --mount=type=cache,target=/root/.cache/go-build,id=swagger-build-$TARGETPLATFORM \
10088
--mount=type=cache,target=/go/pkg/mod \
10189
--mount=type=tmpfs,target=/go/src/ <<EOT
10290
set -e
103-
xx-go build -o /build/swagger ./cmd/swagger
91+
GOBIN=/build xx-go install github.com/go-swagger/go-swagger/cmd/swagger@v0.32.3
10492
xx-verify /build/swagger
10593
EOT
10694

api/templates/schema.gotmpl

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
{{- if and .IsBaseType .IsExported (not .IsSuperAlias) }}
2+
{{- template "schemaPolymorphic" . }}
3+
{{- else if .IsSuperAlias }}
4+
type {{ pascalize .Name }} {{ template "typeSchemaType" . }}{{/* For types declared as $ref on some other type, just declare the type as a golang _aliased_ type, e.g. type A = B. No method shall be redeclared. */}}
5+
{{- if .IsBaseType }}
6+
{{ template "baseTypeSerializer" . }}{{/* When the alias redeclares a polymorphic type, define factory methods with this alias. */}}
7+
{{- end }}
8+
{{- else if .IsEmbedded }}
9+
{{- template "schemaEmbedded" . }}
10+
{{- else }}
11+
{{- if or .IsComplexObject .IsTuple .IsAdditionalProperties }}{{/* TODO(fred): handle case of subtype inheriting from base type with AdditionalProperties, issue #2220 */}}
12+
{{ if .Name }}type {{ if not .IsExported }}{{ .Name }}{{ else }}{{ pascalize .Name }}{{ end }}{{ end }} {{ template "schemaBody" . }}
13+
{{- range .Properties }}
14+
{{- if .IsBaseType }}
15+
// {{ pascalize .Name}} gets the {{ humanize .Name }} of this base type{{/* all properties which are of a base type propagate its interface */}}
16+
func ({{ $.ReceiverName}} *{{ pascalize $.Name}}) {{ pascalize .Name}}() {{ template "schemaType" . }}{
17+
{{- if eq $.DiscriminatorField .Name }}
18+
return {{ printf "%q" $.DiscriminatorValue }}
19+
{{- else }}
20+
return {{ $.ReceiverName }}.{{camelize .Name}}Field
21+
{{- end }}
22+
}
23+
24+
// Set{{ pascalize .Name}} sets the {{ humanize .Name }} of this base type
25+
func ({{ $.ReceiverName}} *{{ pascalize $.Name}}) Set{{ pascalize .Name}}(val {{ template "schemaType" . }}) {
26+
{{- if ne $.DiscriminatorField .Name }}
27+
{{ $.ReceiverName }}.{{camelize .Name}}Field = val
28+
{{- end }}
29+
}
30+
{{- end }}
31+
{{- end }}
32+
{{- if .Default }}{{/* TODO(fred) - issue #2189 */}}
33+
func ({{.ReceiverName}} *{{ pascalize .Name }}) UnmarshalJSON(b []byte) error {
34+
type {{ pascalize .Name }}Alias {{ pascalize .Name }}
35+
var t {{ pascalize .Name }}Alias
36+
if err := json.Unmarshal([]byte({{printf "%q" (json .Default)}}), &t); err != nil {
37+
return err
38+
}
39+
if err := json.Unmarshal(b, &t); err != nil {
40+
return err
41+
}
42+
*{{.ReceiverName}} = {{ pascalize .Name }}(t)
43+
return nil
44+
}
45+
{{- end }}
46+
{{- else }}
47+
type {{ pascalize .Name }} {{ template "typeSchemaType" . }}
48+
{{- end }}
49+
{{- if (and .IsPrimitive .IsAliased .IsCustomFormatter (not (stringContains .Zero "(\""))) }}
50+
{{ template "aliasedSerializer" . }}
51+
{{- end }}
52+
{{- if .IsSubType }}
53+
{{ range .AllOf }}
54+
{{ range .Properties }}
55+
{{- if .IsBaseType }}
56+
57+
// {{ pascalize .Name}} gets the {{ humanize .Name }} of this subtype
58+
func ({{$.ReceiverName}} *{{ pascalize $.Name}}) {{ pascalize .Name}}() {{ template "schemaType" . }}{
59+
{{- if eq $.DiscriminatorField .Name }}
60+
return {{ printf "%q" $.DiscriminatorValue }}
61+
{{- else }}
62+
return {{ $.ReceiverName }}.{{camelize .Name}}Field
63+
{{- end }}
64+
}
65+
66+
// Set{{ pascalize .Name}} sets the {{ humanize .Name }} of this subtype
67+
func ({{$.ReceiverName}} *{{ pascalize $.Name}}) Set{{ pascalize .Name}}(val {{ template "schemaType" . }}) {
68+
{{- if ne $.DiscriminatorField .Name }}
69+
{{ $.ReceiverName }}.{{camelize .Name}}Field = val
70+
{{- end }}
71+
}
72+
{{- end }}
73+
{{- end }}{{/* TODO(fred): handle AdditionalProperties in base type */}}
74+
{{- end }}
75+
{{ template "mapOrSliceGetter" . }}
76+
{{- end }}
77+
{{ template "schemaSerializer" . }}
78+
{{- end }}
79+
{{- if and .IncludeValidator (not .IsSuperAlias) (not .IsEmbedded) }}{{/* aliased types type A = B do not redefine methods */}}
80+
{{- if and (not (or .IsInterface .IsStream)) (or .Required .HasValidations .HasBaseType) }}
81+
{{- if (eq .SwaggerType "string") }}{{/* Enum factory for enums for which we generate const (atm, only strings)*/}}
82+
{{- if .Enum }}
83+
84+
func New{{ pascalize .Name }}(value {{ .GoType }}) *{{ .GoType }} {
85+
return &value
86+
}
87+
88+
// Pointer returns a pointer to a freshly-allocated {{ .GoType }}.
89+
func ({{ .ReceiverName }} {{ .GoType }}) Pointer() *{{ .GoType }} {
90+
return &{{ .ReceiverName }}
91+
}
92+
{{- end }}
93+
{{- end }}
94+
{{ if false }}{{ template "schemavalidator" . }}{{ end }}
95+
{{- else if and false not (or .IsInterface .IsStream) }}
96+
// Validate validates this {{ humanize .Name }}{{/* this schema implements the runtime.Validatable interface but has no validations to check */}}
97+
func ({{.ReceiverName}} {{ if or .IsTuple .IsComplexObject .IsAdditionalProperties }}*{{ end }}{{ if or (not .IsExported) .Discriminates }}{{ camelize .Name }}{{ else }}{{ pascalize .Name }}{{ end }}) Validate(formats strfmt.Registry) error {
98+
return nil
99+
}
100+
{{- else }}{{/* {{ .Name }} does not implement the runtime.Validatable interface: noop */}}
101+
{{- end }}
102+
{{- if false }}
103+
{{- if and (not (or .IsInterface .IsStream)) (or .HasContextValidations) }}
104+
{{ template "schemacontextvalidator" . }}
105+
{{- else if not (or .IsInterface .IsStream) }}
106+
// ContextValidate validates this {{ humanize .Name }} based on context it is used {{/* this schema implements the runtime.ContextValidatable interface but has no validations to check */}}
107+
func ({{.ReceiverName}} {{ if or .IsTuple .IsComplexObject .IsAdditionalProperties }}*{{ end }}{{ if or (not .IsExported) .Discriminates }}{{ camelize .Name }}{{ else }}{{ pascalize .Name }}{{ end }}) ContextValidate(ctx context.Context, formats strfmt.Registry) error {
108+
return nil
109+
}
110+
{{- else }}{{/* {{ .Name }} does not implement the runtime.Validatable interface: noop */}}
111+
{{- end }}
112+
{{- end }}
113+
{{- if .WantsMarshalBinary }}
114+
{{ template "marshalBinarySerializer" . }}
115+
{{- end }}
116+
{{- end }}
117+
{{- define "mapOrSliceGetter" }}{{/* signature for AdditionalProperties and AdditionalItems getter funcs */}}
118+
{{- if not .IsBaseType }}
119+
{{- if .HasAdditionalProperties }}
120+
{{- with .AdditionalProperties }}
121+
// {{- template "docstring" . }}{{- template "propertyValidationDocString" . }}
122+
{{ pascalize .Name }}() map[string]{{ template "schemaType" . }}
123+
{{- end }}
124+
{{- end }}
125+
{{- with .AdditionalItems }}
126+
// {{- template "docstring" . }}{{- template "propertyValidationDocString" . }}
127+
{{ pascalize .Name }}() []{{ template "schemaType" . }}
128+
{{- end }}
129+
{{- else }}
130+
// AdditionalProperties in base type shoud be handled just like regular properties{{/* TODO(fred): add full support for AdditionalProperties in base type */}}
131+
// At this moment, the base type property is pushed down to the subtype
132+
{{- end }}
133+
{{- end }}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{{ define "marshalBinarySerializer" }}{{ end }}

api/templates/server/operation.gotmpl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818

1919
{{ range .ExtraSchemas }}
2020
// {{ .Name }} {{ comment .Description }}
21+
//
2122
// swagger:model {{ .Name }}
22-
{{ template "schema" . }}
23-
{{ end }}
23+
{{- template "schema" . }}
24+
{{- end }}

api/types/common/id_response.go

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/types/container/change_type.go

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/types/container/create_response.go

Lines changed: 6 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/types/container/filesystem_change.go

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/types/container/port.go

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/types/container/top_response.go

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)
0