Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: instance name func to doc file (#1116)
- Loading branch information
Showing
4 changed files
with
215 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package swag | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"strings" | ||
"text/template" | ||
) | ||
|
||
// Spec holds exported Swagger Info so clients can modify it. | ||
type Spec struct { | ||
Version string | ||
Host string | ||
BasePath string | ||
Schemes []string | ||
Title string | ||
Description string | ||
InfoInstanceName string | ||
SwaggerTemplate string | ||
} | ||
|
||
// ReadDoc parses SwaggerTemplate into swagger document. | ||
func (i *Spec) ReadDoc() string { | ||
i.Description = strings.Replace(i.Description, "\n", "\\n", -1) | ||
|
||
t, err := template.New("swagger_info").Funcs(template.FuncMap{ | ||
"marshal": func(v interface{}) string { | ||
a, _ := json.Marshal(v) | ||
return string(a) | ||
}, | ||
"escape": func(v interface{}) string { | ||
// escape tabs | ||
str := strings.Replace(v.(string), "\t", "\\t", -1) | ||
// replace " with \", and if that results in \\", replace that with \\\" | ||
str = strings.Replace(str, "\"", "\\\"", -1) | ||
return strings.Replace(str, "\\\\\"", "\\\\\\\"", -1) | ||
}, | ||
}).Parse(i.SwaggerTemplate) | ||
if err != nil { | ||
return i.SwaggerTemplate | ||
} | ||
|
||
var tpl bytes.Buffer | ||
if err = t.Execute(&tpl, i); err != nil { | ||
return i.SwaggerTemplate | ||
} | ||
|
||
return tpl.String() | ||
} | ||
|
||
// InstanceName returns Spec instance name. | ||
func (i *Spec) InstanceName() string { | ||
return i.InfoInstanceName | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
package swag | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func TestSpec_InstanceName(t *testing.T) { | ||
type fields struct { | ||
Version string | ||
Host string | ||
BasePath string | ||
Schemes []string | ||
Title string | ||
Description string | ||
InfoInstanceName string | ||
SwaggerTemplate string | ||
} | ||
tests := []struct { | ||
name string | ||
fields fields | ||
want string | ||
}{ | ||
{ | ||
name: "TestInstanceNameCorrect", | ||
fields: fields{ | ||
Version: "1.0", | ||
Host: "localhost:8080", | ||
BasePath: "/", | ||
InfoInstanceName: "TestInstanceName1", | ||
}, | ||
want: "TestInstanceName1", | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
i := &Spec{ | ||
Version: tt.fields.Version, | ||
Host: tt.fields.Host, | ||
BasePath: tt.fields.BasePath, | ||
Schemes: tt.fields.Schemes, | ||
Title: tt.fields.Title, | ||
Description: tt.fields.Description, | ||
InfoInstanceName: tt.fields.InfoInstanceName, | ||
SwaggerTemplate: tt.fields.SwaggerTemplate, | ||
} | ||
assert.Equal(t, tt.want, i.InstanceName()) | ||
}) | ||
} | ||
} | ||
|
||
func TestSpec_ReadDoc(t *testing.T) { | ||
type fields struct { | ||
Version string | ||
Host string | ||
BasePath string | ||
Schemes []string | ||
Title string | ||
Description string | ||
InfoInstanceName string | ||
SwaggerTemplate string | ||
} | ||
tests := []struct { | ||
name string | ||
fields fields | ||
want string | ||
}{ | ||
{ | ||
name: "TestReadDocCorrect", | ||
fields: fields{ | ||
Version: "1.0", | ||
Host: "localhost:8080", | ||
BasePath: "/", | ||
InfoInstanceName: "TestInstanceName", | ||
SwaggerTemplate: `{ | ||
"swagger": "2.0", | ||
"info": { | ||
"description": "{{escape .Description}}", | ||
"title": "{{.Title}}", | ||
"version": "{{.Version}}" | ||
}, | ||
"host": "{{.Host}}", | ||
"basePath": "{{.BasePath}}", | ||
}`, | ||
}, | ||
want: "{" + | ||
"\n\t\t\t\"swagger\": \"2.0\"," + | ||
"\n\t\t\t\"info\": {" + | ||
"\n\t\t\t\t\"description\": \"\",\n\t\t\t\t\"" + | ||
"title\": \"\"," + | ||
"\n\t\t\t\t\"version\": \"1.0\"" + | ||
"\n\t\t\t}," + | ||
"\n\t\t\t\"host\": \"localhost:8080\"," + | ||
"\n\t\t\t\"basePath\": \"/\"," + | ||
"\n\t\t}", | ||
}, | ||
{ | ||
name: "TestReadDocMarshalTrigger", | ||
fields: fields{ | ||
Version: "1.0", | ||
Host: "localhost:8080", | ||
BasePath: "/", | ||
InfoInstanceName: "TestInstanceName", | ||
SwaggerTemplate: "{{ marshal .Version }}", | ||
}, | ||
want: "\"1.0\"", | ||
}, | ||
{ | ||
name: "TestReadDocParseError", | ||
fields: fields{ | ||
Version: "1.0", | ||
Host: "localhost:8080", | ||
BasePath: "/", | ||
InfoInstanceName: "TestInstanceName", | ||
SwaggerTemplate: "{{ ..Version }}", | ||
}, | ||
want: "{{ ..Version }}", | ||
}, | ||
{ | ||
name: "TestReadDocExecuteError", | ||
fields: fields{ | ||
Version: "1.0", | ||
Host: "localhost:8080", | ||
BasePath: "/", | ||
InfoInstanceName: "TestInstanceName", | ||
SwaggerTemplate: "{{ .Schemesa }}", | ||
}, | ||
want: "{{ .Schemesa }}", | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
i := &Spec{ | ||
Version: tt.fields.Version, | ||
Host: tt.fields.Host, | ||
BasePath: tt.fields.BasePath, | ||
Schemes: tt.fields.Schemes, | ||
Title: tt.fields.Title, | ||
Description: tt.fields.Description, | ||
InfoInstanceName: tt.fields.InfoInstanceName, | ||
SwaggerTemplate: tt.fields.SwaggerTemplate, | ||
} | ||
assert.Equal(t, tt.want, i.ReadDoc()) | ||
}) | ||
} | ||
} |