github.com/circl-dev/go-swagger@v0.31.0/cmd/swagger/commands/generate/model.go (about)

     1  // Copyright 2015 go-swagger maintainers
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //    http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package generate
    16  
    17  import (
    18  	"errors"
    19  	"log"
    20  
    21  	"github.com/circl-dev/go-swagger/generator"
    22  )
    23  
    24  type modelOptions struct {
    25  	ModelPackage               string   `long:"model-package" short:"m" description:"the package to save the models" default:"models"`
    26  	Models                     []string `long:"model" short:"M" description:"specify a model to include in generation, repeat for multiple (defaults to all)"`
    27  	ExistingModels             string   `long:"existing-models" description:"use pre-generated models e.g. github.com/foobar/model"`
    28  	StrictAdditionalProperties bool     `long:"strict-additional-properties" description:"disallow extra properties when additionalProperties is set to false"`
    29  	KeepSpecOrder              bool     `long:"keep-spec-order" description:"keep schema properties order identical to spec file"`
    30  	AllDefinitions             bool     `long:"all-definitions" description:"generate all model definitions regardless of usage in operations" hidden:"deprecated"`
    31  	StructTags                 []string `long:"struct-tags" description:"the struct tags to generate, repeat for multiple (defaults to json)"`
    32  }
    33  
    34  func (mo modelOptions) apply(opts *generator.GenOpts) {
    35  	opts.ModelPackage = mo.ModelPackage
    36  	opts.Models = mo.Models
    37  	opts.ExistingModels = mo.ExistingModels
    38  	opts.StrictAdditionalProperties = mo.StrictAdditionalProperties
    39  	opts.PropertiesSpecOrder = mo.KeepSpecOrder
    40  	opts.IgnoreOperations = mo.AllDefinitions
    41  	opts.StructTags = mo.StructTags
    42  }
    43  
    44  // WithModels adds the model options group.
    45  //
    46  // This group is available to all commands that need some model generation.
    47  type WithModels struct {
    48  	Models modelOptions `group:"Options for model generation"`
    49  }
    50  
    51  // Model the generate model file command.
    52  //
    53  // Define the options that are specific to the "swagger generate model" command.
    54  type Model struct {
    55  	WithShared
    56  	WithModels
    57  
    58  	NoStruct              bool     `long:"skip-struct" description:"when present will not generate the model struct" hidden:"deprecated"`
    59  	Name                  []string `long:"name" short:"n" description:"the model to generate, repeat for multiple (defaults to all). Same as --models"`
    60  	AcceptDefinitionsOnly bool     `long:"accept-definitions-only" description:"accepts a partial swagger spec wih only the definitions key"`
    61  }
    62  
    63  func (m Model) apply(opts *generator.GenOpts) {
    64  	m.Shared.apply(opts)
    65  	m.Models.apply(opts)
    66  
    67  	opts.IncludeModel = !m.NoStruct
    68  	opts.IncludeValidator = !m.NoStruct
    69  	opts.AcceptDefinitionsOnly = m.AcceptDefinitionsOnly
    70  }
    71  
    72  func (m Model) log(rp string) {
    73  	log.Printf(`Generation completed!
    74  
    75  For this generation to compile you need to have some packages in your GOPATH:
    76  
    77  	* github.com/circl-dev/validate
    78  	* github.com/go-openapi/strfmt
    79  
    80  You can get these now with: go get -u -f %s/...
    81  `, rp)
    82  }
    83  
    84  func (m *Model) generate(opts *generator.GenOpts) error {
    85  	return generator.GenerateModels(append(m.Name, m.Models.Models...), opts)
    86  }
    87  
    88  // Execute generates a model file
    89  func (m *Model) Execute(args []string) error {
    90  
    91  	if m.Shared.DumpData && len(append(m.Name, m.Models.Models...)) > 1 {
    92  		return errors.New("only 1 model at a time is supported for dumping data")
    93  	}
    94  
    95  	if m.Models.ExistingModels != "" {
    96  		log.Println("warning: Ignoring existing-models flag when generating models.")
    97  	}
    98  	return createSwagger(m)
    99  }