Skip to content

Commit

Permalink
Small refactoring on custom extensions usage: (go-swagger#1440)
Browse files Browse the repository at this point in the history
- declare supported extensions as constants in types.go
- propagate extensions in resolverType
  • Loading branch information
fredbi authored and casualjim committed Mar 5, 2018
1 parent 2fed97b commit 6bb0956
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 31 deletions.
6 changes: 3 additions & 3 deletions generator/discriminators.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func discriminatorInfo(doc *analysis.Spec) *discInfo {
baseTypes := make(map[string]discor)
for _, sch := range doc.AllDefinitions() {
if sch.Schema.Discriminator != "" {
tpe, _ := sch.Schema.Extensions.GetString("x-go-name")
tpe, _ := sch.Schema.Extensions.GetString(xGoName)
if tpe == "" {
tpe = swag.ToGoName(sch.Name)
}
Expand All @@ -48,11 +48,11 @@ func discriminatorInfo(doc *analysis.Spec) *discInfo {
for _, ao := range sch.Schema.AllOf {
if ao.Ref.String() != "" {
if bt, ok := baseTypes[ao.Ref.String()]; ok {
name, _ := sch.Schema.Extensions.GetString("x-class")
name, _ := sch.Schema.Extensions.GetString(xClass)
if name == "" {
name = sch.Name
}
tpe, _ := sch.Schema.Extensions.GetString("x-go-name")
tpe, _ := sch.Schema.Extensions.GetString(xGoName)
if tpe == "" {
tpe = swag.ToGoName(sch.Name)
}
Expand Down
8 changes: 4 additions & 4 deletions generator/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func makeGenDefinition(name, pkg string, schema spec.Schema, specDoc *loads.Docu

func makeGenDefinitionHierarchy(name, pkg, container string, schema spec.Schema, specDoc *loads.Document, opts *GenOpts) (*GenDefinition, error) {

_, ok := schema.Extensions["x-go-type"]
_, ok := schema.Extensions[xGoType]
if ok {
return nil, nil
}
Expand Down Expand Up @@ -717,7 +717,7 @@ func (sg *schemaGenContext) buildProperties() error {
}
var nm = filepath.Base(emprop.Schema.Ref.GetURL().Fragment)
var tn string
if gn, ok := emprop.Schema.Extensions["x-go-name"]; ok {
if gn, ok := emprop.Schema.Extensions[xGoName]; ok {
tn = gn.(string)
} else {
tn = swag.ToGoName(nm)
Expand Down Expand Up @@ -748,7 +748,7 @@ func (sg *schemaGenContext) buildProperties() error {
}
sg.MergeResult(emprop, false)

if customTag, found := emprop.Schema.Extensions["x-go-custom-tag"]; found {
if customTag, found := emprop.Schema.Extensions[xGoCustomTag]; found {
emprop.GenSchema.CustomTag = customTag.(string)
}
if emprop.GenSchema.HasDiscriminator {
Expand Down Expand Up @@ -1319,7 +1319,7 @@ func (sg *schemaGenContext) GoName() string {
}

func goName(sch *spec.Schema, orig string) string {
name, _ := sch.Extensions.GetString("x-go-name")
name, _ := sch.Extensions.GetString(xGoName)
if name != "" {
return name
}
Expand Down
6 changes: 3 additions & 3 deletions generator/operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ func (b *codeGenOpBuilder) MakeOperation() (GenOperation, error) {
var successResponses []GenResponse
if operation.Responses != nil {
for _, v := range srs {
name, ok := v.Response.Extensions.GetString("x-go-name")
name, ok := v.Response.Extensions.GetString(xGoName)
if !ok {
name = runtime.Statuses[v.Code]
}
Expand Down Expand Up @@ -431,11 +431,11 @@ func (b *codeGenOpBuilder) MakeOperation() (GenOperation, error) {

swsp := resolver.Doc.Spec()
var extraSchemes []string
if ess, ok := operation.Extensions.GetStringSlice("x-schemes"); ok {
if ess, ok := operation.Extensions.GetStringSlice(xSchemes); ok {
extraSchemes = append(extraSchemes, ess...)
}

if ess1, ok := swsp.Extensions.GetStringSlice("x-schemes"); ok {
if ess1, ok := swsp.Extensions.GetStringSlice(xSchemes); ok {
extraSchemes = concatUnique(ess1, extraSchemes)
}
sort.Strings(extraSchemes)
Expand Down
4 changes: 2 additions & 2 deletions generator/typeresolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ func basicTaskListResolver(t testing.TB) (*loads.Document, *typeResolver, error)
}
swsp := tlb.Spec()
uc := swsp.Definitions["UserCard"]
uc.AddExtension("x-go-name", "UserItem")
uc.AddExtension(xGoName, "UserItem")
swsp.Definitions["UserCard"] = uc
resolver := &typeResolver{
Doc: tlb,
Expand All @@ -427,7 +427,7 @@ func basicTaskListResolver(t testing.TB) (*loads.Document, *typeResolver, error)
resolver.KnownDefs = make(map[string]struct{})
for k, sch := range swsp.Definitions {
resolver.KnownDefs[k] = struct{}{}
if nm, ok := sch.Extensions["x-go-name"]; ok {
if nm, ok := sch.Extensions[xGoName]; ok {
resolver.KnownDefs[nm.(string)] = struct{}{}
}
}
Expand Down
49 changes: 30 additions & 19 deletions generator/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,29 @@ import (
)

const (
iface = "interface{}"
array = "array"
file = "file"
number = "number"
integer = "integer"
boolean = "boolean"
str = "string"
object = "object"
binary = "binary"
xNullable = "x-nullable"
xIsNullable = "x-isnullable"
xOmitEmpty = "x-omitempty"
sHTTP = "http"
body = "body"
iface = "interface{}"
array = "array"
file = "file"
number = "number"
integer = "integer"
boolean = "boolean"
str = "string"
object = "object"
binary = "binary"
sHTTP = "http"
body = "body"
)

// Extensions supported by go-swagger
const (
xClass = "x-class" // class name used by discriminator
xGoCustomTag = "x-go-custom-tag" // additional tag for serializers on struct fields
xGoName = "x-go-name" // name of the generated go variable
xGoType = "x-go-type" // reuse existing type (do not generate)
xIsNullable = "x-isnullable"
xNullable = "x-nullable" // turns the schema into a pointer
xOmitEmpty = "x-omitempty"
xSchemes = "x-schemes" // additional schemes supported for operations (server generation)
)

// swaggerTypeMapping contains a mapping from go type to swagger type or format
Expand Down Expand Up @@ -127,15 +136,15 @@ func debugLog(format string, args ...interface{}) {
func knownDefGoType(def string, schema spec.Schema, clear func(string) string) (string, string, string) {
debugLog("known def type: %q", def)
ext := schema.Extensions
if nm, ok := ext.GetString("x-go-name"); ok {
if nm, ok := ext.GetString(xGoName); ok {
if clear == nil {
debugLog("known def type x-go-name no clear: %q", nm)
debugLog("known def type %s no clear: %q", xGoName, nm)
return nm, "", ""
}
debugLog("known def type x-go-name clear: %q -> %q", nm, clear(nm))
debugLog("known def type %s clear: %q -> %q", xGoName, nm, clear(nm))
return clear(nm), "", ""
}
v, ok := ext["x-go-type"]
v, ok := ext[xGoType]
if !ok {
if clear == nil {
debugLog("known def type no clear: %q", def)
Expand All @@ -155,7 +164,7 @@ func knownDefGoType(def string, schema spec.Schema, clear func(string) string) (
} else {
alias = filepath.Base(pkg)
}
debugLog("known def type x-go-type no clear: %q", alias+"."+t, pkg, alias)
debugLog("known def type %s no clear: %q", xGoType, alias+"."+t, pkg, alias)
return alias + "." + t, pkg, alias
}

Expand Down Expand Up @@ -253,6 +262,8 @@ func (t *typeResolver) resolveFormat(schema *spec.Schema, isAnonymous bool, isRe
result.IsPrimitive = schFmt != binary
result.IsStream = schFmt == binary
_, result.IsCustomFormatter = customFormatters[tpe]
// propagate extensions in resolvedType
result.Extensions = schema.Extensions

switch result.SwaggerType {
case str:
Expand Down

0 comments on commit 6bb0956

Please sign in to comment.