github.com/thetreep/go-swagger@v0.0.0-20240223100711-35af64f14f01/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/thetreep/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  	RootedErrorPath            bool     `long:"rooted-error-path" description:"extends validation errors with the type name instead of an empty path, in the case of arrays and maps"`
    33  }
    34  
    35  func (mo modelOptions) apply(opts *generator.GenOpts) {
    36  	opts.ModelPackage = mo.ModelPackage
    37  	opts.Models = mo.Models
    38  	opts.ExistingModels = mo.ExistingModels
    39  	opts.StrictAdditionalProperties = mo.StrictAdditionalProperties
    40  	opts.PropertiesSpecOrder = mo.KeepSpecOrder
    41  	opts.IgnoreOperations = mo.AllDefinitions
    42  	opts.StructTags = mo.StructTags
    43  	opts.WantsRootedErrorPath = mo.RootedErrorPath
    44  }
    45  
    46  // WithModels adds the model options group.
    47  //
    48  // This group is available to all commands that need some model generation.
    49  type WithModels struct {
    50  	Models modelOptions `group:"Options for model generation"`
    51  }
    52  
    53  // Model the generate model file command.
    54  //
    55  // Define the options that are specific to the "swagger generate model" command.
    56  type Model struct {
    57  	WithShared
    58  	WithModels
    59  
    60  	NoStruct              bool     `long:"skip-struct" description:"when present will not generate the model struct" hidden:"deprecated"`
    61  	Name                  []string `long:"name" short:"n" description:"the model to generate, repeat for multiple (defaults to all). Same as --models"`
    62  	AcceptDefinitionsOnly bool     `long:"accept-definitions-only" description:"accepts a partial swagger spec with only the definitions key"`
    63  }
    64  
    65  func (m Model) apply(opts *generator.GenOpts) {
    66  	m.Shared.apply(opts)
    67  	m.Models.apply(opts)
    68  
    69  	opts.IncludeModel = !m.NoStruct
    70  	opts.IncludeValidator = !m.NoStruct
    71  	opts.AcceptDefinitionsOnly = m.AcceptDefinitionsOnly
    72  }
    73  
    74  func (m Model) log(rp string) {
    75  	log.Println(
    76  		`Generation completed!
    77  
    78  For this generation to compile you need to have some packages in your go.mod:
    79  
    80  	* github.com/go-openapi/validate
    81  	* github.com/go-openapi/strfmt
    82  
    83  You can get these now with: go mod tidy`,
    84  	)
    85  }
    86  
    87  func (m *Model) generate(opts *generator.GenOpts) error {
    88  	return generator.GenerateModels(append(m.Name, m.Models.Models...), opts)
    89  }
    90  
    91  // Execute generates a model file
    92  func (m *Model) Execute(args []string) error {
    93  
    94  	if m.Shared.DumpData && len(append(m.Name, m.Models.Models...)) > 1 {
    95  		return errors.New("only 1 model at a time is supported for dumping data")
    96  	}
    97  
    98  	if m.Models.ExistingModels != "" {
    99  		log.Println("warning: Ignoring existing-models flag when generating models.")
   100  	}
   101  	return createSwagger(m)
   102  }