Skip to content

Commit

Permalink
Added an option in genOpts
Browse files Browse the repository at this point in the history
  • Loading branch information
diego-fu-hs authored and casualjim committed Jan 8, 2018
1 parent 853676a commit d2aae81
Show file tree
Hide file tree
Showing 15 changed files with 69 additions and 62 deletions.
3 changes: 2 additions & 1 deletion cmd/swagger/commands/generate/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ func (c *Client) getOpts() (*generator.GenOpts, error) {
DumpData: c.DumpData,
ExistingModels: c.ExistingModels,
Copyright: copyrightstr,
IsClient: true,
}, nil
}

Expand All @@ -98,5 +99,5 @@ You can get these now with: go get -u -f %s/...

// Execute runs this command
func (c *Client) Execute(args []string) error {
return generate(c)
return createSwagger(c)
}
8 changes: 3 additions & 5 deletions cmd/swagger/commands/generate/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,9 @@ import (
// Model the generate model file command
type Model struct {
shared
Name []string `long:"name" short:"n" description:"the model to generate"`
NoValidator bool `long:"skip-validator" description:"when present will not generate a model validator"`
NoStruct bool `long:"skip-struct" description:"when present will not generate the model struct"`
DumpData bool `long:"dump-data" description:"when present dumps the json for the template generator instead of generating files"`
Name []string `long:"name" short:"n" description:"the model to generate"`
NoStruct bool `long:"skip-struct" description:"when present will not generate the model struct"`
DumpData bool `long:"dump-data" description:"when present dumps the json for the template generator instead of generating files"`
}

// Execute generates a model file
Expand All @@ -46,7 +45,6 @@ func (m *Model) Execute(args []string) error {
ExcludeSpec: true,
SkipSupport: true,
SkipOperations: true,
SkipValidation: m.NoValidator,
SkipModels: m.NoStruct,
}
return s.Execute(args)
Expand Down
1 change: 0 additions & 1 deletion cmd/swagger/commands/generate/model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ func TestGenerateModel(t *testing.T) {
flags.Parse(m)
m.Spec = flags.Filename(path)
m.Target = flags.Filename(generated)
m.NoValidator = true

if err := m.Execute([]string{}); err != nil {
t.Error(err)
Expand Down
4 changes: 1 addition & 3 deletions cmd/swagger/commands/generate/operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ type Operation struct {
NoHandler bool `long:"skip-handler" description:"when present will not generate an operation handler"`
NoStruct bool `long:"skip-parameters" description:"when present will not generate the parameter model struct"`
NoResponses bool `long:"skip-responses" description:"when present will not generate the response model struct"`
NoValidator bool `long:"skip-validator" description:"when present will not generate a model validator"`
NoURLBuilder bool `long:"skip-url-builder" description:"when present will not generate a URL builder"`
DumpData bool `long:"dump-data" description:"when present dumps the json for the template generator instead of generating files"`
SkipFlattening bool `long:"skip-flatten" description:"skips flattening of spec prior to generation"`
Expand All @@ -53,7 +52,6 @@ func (o *Operation) getOpts() (*generator.GenOpts, error) {
IncludeHandler: !o.NoHandler,
IncludeResponses: !o.NoResponses,
IncludeParameters: !o.NoStruct,
IncludeValidator: !o.NoValidator,
IncludeURLBuilder: !o.NoURLBuilder,
Tags: o.Tags,
FlattenSpec: !o.SkipFlattening,
Expand Down Expand Up @@ -83,5 +81,5 @@ func (o *Operation) Execute(args []string) error {
return errors.New("only 1 operation at a time is supported for dumping data")
}

return generate(o)
return createSwagger(o)
}
2 changes: 1 addition & 1 deletion cmd/swagger/commands/generate/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,5 +116,5 @@ You can get these now with: go get -u -f %s/...

// Execute runs this command
func (s *Server) Execute(args []string) error {
return generate(s)
return createSwagger(s)
}
4 changes: 2 additions & 2 deletions cmd/swagger/commands/generate/shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (s *shared) getAdditionalInitialisms() []string {
return s.AdditionalInitialisms
}

func generate(s sharedCommand) error {
func createSwagger(s sharedCommand) error {
cfg, err := readConfig(string(s.getConfigFile()))
if err != nil {
return err
Expand All @@ -54,7 +54,7 @@ func generate(s sharedCommand) error {
return err
}

if err := opts.EnsureDefaults(false); err != nil {
if err := opts.EnsureDefaults(); err != nil {
return err
}

Expand Down
10 changes: 7 additions & 3 deletions cmd/swagger/commands/generate/support.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,19 @@ func (s *Support) log(rp string) {

log.Printf(`Generation completed!
For this generation to compile you need to have some packages in your GOPATH:
For this generation to compile you need to have some packages in your vendor or GOPATH:
* github.com/go-openapi/runtime
* github.com/go-openapi/runtime
* github.com/asaskevich/govalidator
* github.com/tylerb/graceful
* github.com/jessevdk/go-flags
* golang.org/x/net/context/ctxhttp
You can get these now with: go get -u -f %s/...
`, rp)
}

// Execute generates the supporting files file
func (s *Support) Execute(args []string) error {
return generate(s)
return createSwagger(s)
}
2 changes: 0 additions & 2 deletions cmd/swagger/completion/swagger.zsh-completion
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ __generate_model() {
'(-T,--template-dir)'{-T,--template-dir}"[alternative template override directory]:template-dir" \
'(-C,--config-file)'{-C,--config-file}"[configuration file to use for overriding template options]:config-file" \
'(-n,--name)'{-n,--name}"[the model to generate]:name" \
"(--skip-validator)--skip-validator[when present will not generate a model validator]" \
"(--skip-struct)--skip-struct[when present will not generate the model struct]" \
"(--dump-data)--dump-data[when present dumps the json for the template generator instead of generating files]" && return 0
}
Expand All @@ -76,7 +75,6 @@ __generate_operation() {
"(--skip-handler)--skip-handler[when present will not generate an operation handler]" \
"(--skip-parameters)--skip-parameters[when present will not generate the parameter model struct]" \
"(--skip-responses)--skip-responses[when present will not generate the response model struct]" \
"(--skip-validator)--skip-validator[when present will not generate a model validator]" \
"(--skip-url-builder)--skip-url-builder[when present will not generate a URL builder]" \
"(--dump-data)--dump-data[when present dumps the json for the template generator instead of generating files]" && return 0
}
Expand Down
1 change: 0 additions & 1 deletion cmd/swagger/swagger.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,5 +125,4 @@ It aims to represent the contract of your API with a language agnostic descripti
if _, err := parser.Parse(); err != nil {
os.Exit(1)
}

}
42 changes: 21 additions & 21 deletions docs/generate/client.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,27 @@ Help Options:
-h, --help Show this help message
[client command options]
-f, --spec= the spec file to use (default swagger.{json,yml,yaml})
-a, --api-package= the package to save the operations (default: operations)
-m, --model-package= the package to save the models (default: models)
-s, --server-package= the package to save the server specific code (default: restapi)
-c, --client-package= the package to save the client specific code (default: client)
-t, --target= the base directory for generating the files (default: ./)
-T, --template-dir= alternative template override directory
-C, --config-file= configuration file to use for overriding template options
-A, --name= the name of the application, defaults to a mangled value of info.title
-O, --operation= specify an operation to include, repeat for multiple
--tags= the tags to include, if not specified defaults to all
-P, --principal= the model to use for the security principal
-M, --model= specify a model to include, repeat for multiple
--default-scheme= the default scheme for this client (default: http)
--default-produces= the default mime type that API operations produce (default: application/json)
--skip-models no models will be generated when this flag is specified
--skip-operations no operations will be generated when this flag is specified
--dump-data when present dumps the json for the template generator instead of generating files
--skip-validation skips validation of spec prior to generation
-r, --copyright-file= the file containing a copyright header for the generated source
--additional-initialism= additional consecutive capitals that should be considered as initialism, repeat for multiple
-f, --spec= the spec file to use (default swagger.{json,yml,yaml})
-a, --api-package= the package to save the operations (default: operations)
-m, --model-package= the package to save the models (default: models)
-s, --server-package= the package to save the server specific code (default: restapi)
-c, --client-package= the package to save the client specific code (default: client)
-t, --target= the base directory for generating the files (default: ./)
-T, --template-dir= alternative template override directory
-C, --config-file= configuration file to use for overriding template options
-A, --name= the name of the application, defaults to a mangled value of info.title
-O, --operation= specify an operation to include, repeat for multiple
--tags= the tags to include, if not specified defaults to all
-P, --principal= the model to use for the security principal
-M, --model= specify a model to include, repeat for multiple
--default-scheme= the default scheme for this client (default: http)
--default-produces= the default mime type that API operations produce (default: application/json)
--skip-models no models will be generated when this flag is specified
--skip-operations no operations will be generated when this flag is specified
--dump-data when present dumps the json for the template generator instead of generating files
--skip-validation skips validation of spec prior to generation
-r, --copyright-file= the file containing a copyright header for the generated source
--additional-initialism= additional consecutive capitals that should be considered as initialism, repeat for multiple
```

There is an example client in https://github.com/go-swagger/go-swagger/tree/master/examples/todo-list/client
Expand Down
2 changes: 1 addition & 1 deletion docs/generate/server.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ Help Options:
--compatibility-mode=[modern|intermediate] the compatibility mode for the tls server (default: modern)
--skip-validation skips validation of spec prior to generation
-r, --copyright-file= the file containing a copyright header for the generated source
--additional-initialism= additional consecutive capitals that should be considered as initialism, repeat for multiple
--additional-initialism= additional consecutive capitals that should be considered as initialism, repeat for multiple
```

The server application gets generated with all the handlers stubbed out with a not implemented handler. That means that you can start the API server immediately after generating it. It will respond to all valid requests with 501 Not Implemented. When a request is invalid it will most likely respond with an appropriate 4xx response.
Expand Down
2 changes: 1 addition & 1 deletion generator/discriminators_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func opts() *GenOpts {
var opts GenOpts
opts.IncludeValidator = true
opts.IncludeModel = true
if err := opts.EnsureDefaults(false); err != nil {
if err := opts.EnsureDefaults(); err != nil {
panic(err)
}
return &opts
Expand Down
35 changes: 22 additions & 13 deletions generator/operation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,8 @@ func TestDateFormat_Spec1(t *testing.T) {
buf := bytes.NewBuffer(nil)
opts := opts()
opts.defaultsEnsured = false
err = opts.EnsureDefaults(true)
opts.IsClient = true
err = opts.EnsureDefaults()
assert.NoError(t, err)
err = templates.MustGet("clientParameter").Execute(buf, op)
if assert.NoError(t, err) {
Expand All @@ -406,7 +407,8 @@ func TestDateFormat_Spec2(t *testing.T) {
buf := bytes.NewBuffer(nil)
opts := opts()
opts.defaultsEnsured = false
err = opts.EnsureDefaults(true)
opts.IsClient = true
err = opts.EnsureDefaults()
assert.NoError(t, err)
err = templates.MustGet("clientParameter").Execute(buf, op)
if assert.NoError(t, err) {
Expand Down Expand Up @@ -441,7 +443,7 @@ func TestBuilder_Issue287(t *testing.T) {
ClientPackage: "client",
Target: dr,
}
err := opts.EnsureDefaults(false)
err := opts.EnsureDefaults()
assert.NoError(t, err)
appGen, err := newAppGenerator("plainTexter", nil, nil, opts)
if assert.NoError(t, err) {
Expand Down Expand Up @@ -479,8 +481,9 @@ func TestBuilder_Issue465(t *testing.T) {
ServerPackage: "server",
ClientPackage: "client",
Target: dr,
IsClient: true,
}
err := opts.EnsureDefaults(true)
err := opts.EnsureDefaults()
assert.NoError(t, err)
appGen, err := newAppGenerator("plainTexter", nil, nil, opts)
if assert.NoError(t, err) {
Expand Down Expand Up @@ -519,7 +522,7 @@ func TestBuilder_Issue500(t *testing.T) {
ClientPackage: "client",
Target: dr,
}
err := opts.EnsureDefaults(false)
err := opts.EnsureDefaults()
assert.NoError(t, err)
appGen, err := newAppGenerator("multiTags", nil, nil, opts)
if assert.NoError(t, err) {
Expand Down Expand Up @@ -549,7 +552,8 @@ func TestGenClient_IllegalBOM(t *testing.T) {
buf := bytes.NewBuffer(nil)
opts := opts()
opts.defaultsEnsured = false
err = opts.EnsureDefaults(true)
opts.IsClient = true
err = opts.EnsureDefaults()
assert.NoError(t, err)
err = templates.MustGet("clientResponse").Execute(buf, op)
assert.NoError(t, err)
Expand All @@ -565,7 +569,8 @@ func TestGenClient_CustomFormatPath(t *testing.T) {
buf := bytes.NewBuffer(nil)
opts := opts()
opts.defaultsEnsured = false
err = opts.EnsureDefaults(true)
opts.IsClient = true
err = opts.EnsureDefaults()
assert.NoError(t, err)
err = templates.MustGet("clientParameter").Execute(buf, op)
if assert.NoError(t, err) {
Expand All @@ -583,7 +588,8 @@ func TestGenClient_Issue733(t *testing.T) {
buf := bytes.NewBuffer(nil)
opts := opts()
opts.defaultsEnsured = false
err = opts.EnsureDefaults(true)
opts.IsClient = true
err = opts.EnsureDefaults()
assert.NoError(t, err)
err = templates.MustGet("clientResponse").Execute(buf, op)
if assert.NoError(t, err) {
Expand Down Expand Up @@ -612,10 +618,11 @@ func TestGenServerIssue890_ValidationTrueFlatteningTrue(t *testing.T) {
ServerPackage: "server",
ClientPackage: "client",
Target: dr,
IsClient: true,
}

//Testing Server Generation
err := opts.EnsureDefaults(true)
err := opts.EnsureDefaults()
assert.NoError(t, err)
appGen, err := newAppGenerator("JsonRefOperation", nil, nil, opts)
if assert.NoError(t, err) {
Expand Down Expand Up @@ -664,17 +671,17 @@ func TestGenServerIssue890_ValidationFalseFlattenTrue(t *testing.T) {
IncludeParameters: true,
IncludeResponses: true,
IncludeMain: true,
ValidateSpec: false,
FlattenSpec: true,
APIPackage: "restapi",
ModelPackage: "model",
ServerPackage: "server",
ClientPackage: "client",
Target: dr,
IsClient: true,
}

//Testing Server Generation
err := opts.EnsureDefaults(true)
err := opts.EnsureDefaults()
assert.NoError(t, err)
appGen, err := newAppGenerator("JsonRefOperation", nil, nil, opts)
if assert.NoError(t, err) {
Expand Down Expand Up @@ -730,10 +737,11 @@ func TestGenServerIssue890_ValidationFalseFlattenFalse(t *testing.T) {
ServerPackage: "server",
ClientPackage: "client",
Target: dr,
IsClient: true,
}

//Testing Server Generation
err := opts.EnsureDefaults(true)
err := opts.EnsureDefaults()
assert.NoError(t, err)
_, err = newAppGenerator("JsonRefOperation", nil, nil, opts)
// if flatten is not set, expand takes over so this would resume normally
Expand Down Expand Up @@ -774,10 +782,11 @@ func TestGenServerIssue890_ValidationTrueFlattenFalse(t *testing.T) {
ServerPackage: "server",
ClientPackage: "client",
Target: dr,
IsClient: true,
}

//Testing Server Generation
err := opts.EnsureDefaults(true)
err := opts.EnsureDefaults()
assert.NoError(t, err)
_, err = newAppGenerator("JsonRefOperation", nil, nil, opts)
// now if flatten is false, expand takes over so server generation should resume normally
Expand Down
2 changes: 1 addition & 1 deletion generator/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func testGenOpts() (g GenOpts) {
g.TemplateDir = ""
g.WithContext = false
g.DumpData = false
_ = g.EnsureDefaults(false)
_ = g.EnsureDefaults()
return
}

Expand Down
Loading

0 comments on commit d2aae81

Please sign in to comment.