github.com/rzurga/go-swagger@v0.28.1-0.20211109195225-5d1f453ffa3a/generator/client.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 generator
    16  
    17  import (
    18  	"errors"
    19  
    20  	"github.com/go-openapi/swag"
    21  )
    22  
    23  // GenerateClient generates a client library for a swagger spec document.
    24  func GenerateClient(name string, modelNames, operationIDs []string, opts *GenOpts) error {
    25  	if err := opts.CheckOpts(); err != nil {
    26  		return err
    27  	}
    28  
    29  	if err := opts.setTemplates(); err != nil {
    30  		return err
    31  	}
    32  
    33  	specDoc, analyzed, err := opts.analyzeSpec()
    34  	if err != nil {
    35  		return err
    36  	}
    37  
    38  	models, err := gatherModels(specDoc, modelNames)
    39  	if err != nil {
    40  		return err
    41  	}
    42  
    43  	operations := gatherOperations(analyzed, operationIDs)
    44  	if len(operations) == 0 {
    45  		return errors.New("no operations were selected")
    46  	}
    47  
    48  	generator := appGenerator{
    49  		Name:              appNameOrDefault(specDoc, name, defaultClientName),
    50  		SpecDoc:           specDoc,
    51  		Analyzed:          analyzed,
    52  		Models:            models,
    53  		Operations:        operations,
    54  		Target:            opts.Target,
    55  		DumpData:          opts.DumpData,
    56  		Package:           opts.LanguageOpts.ManglePackageName(opts.ClientPackage, defaultClientTarget),
    57  		APIPackage:        opts.LanguageOpts.ManglePackagePath(opts.APIPackage, defaultOperationsTarget),
    58  		ModelsPackage:     opts.LanguageOpts.ManglePackagePath(opts.ModelPackage, defaultModelsTarget),
    59  		ServerPackage:     opts.LanguageOpts.ManglePackagePath(opts.ServerPackage, defaultServerTarget),
    60  		ClientPackage:     opts.LanguageOpts.ManglePackagePath(opts.ClientPackage, defaultClientTarget),
    61  		OperationsPackage: opts.LanguageOpts.ManglePackagePath(opts.ClientPackage, defaultClientTarget),
    62  		Principal:         opts.PrincipalAlias(),
    63  		DefaultScheme:     opts.DefaultScheme,
    64  		DefaultProduces:   opts.DefaultProduces,
    65  		DefaultConsumes:   opts.DefaultConsumes,
    66  		GenOpts:           opts,
    67  	}
    68  	generator.Receiver = "o"
    69  	return (&clientGenerator{generator}).Generate()
    70  }
    71  
    72  type clientGenerator struct {
    73  	appGenerator
    74  }
    75  
    76  func (c *clientGenerator) Generate() error {
    77  	app, err := c.makeCodegenApp()
    78  	if err != nil {
    79  		return err
    80  	}
    81  
    82  	if c.DumpData {
    83  		return dumpData(swag.ToDynamicJSON(app))
    84  	}
    85  
    86  	if c.GenOpts.IncludeModel {
    87  		for _, m := range app.Models {
    88  			if m.IsStream {
    89  				continue
    90  			}
    91  			mod := m
    92  			if err := c.GenOpts.renderDefinition(&mod); err != nil {
    93  				return err
    94  			}
    95  		}
    96  	}
    97  
    98  	if c.GenOpts.IncludeHandler {
    99  		for _, g := range app.OperationGroups {
   100  			opg := g
   101  			for _, o := range opg.Operations {
   102  				op := o
   103  				if err := c.GenOpts.renderOperation(&op); err != nil {
   104  					return err
   105  				}
   106  			}
   107  			if err := c.GenOpts.renderOperationGroup(&opg); err != nil {
   108  				return err
   109  			}
   110  		}
   111  	}
   112  
   113  	if c.GenOpts.IncludeSupport {
   114  		if err := c.GenOpts.renderApplication(&app); err != nil {
   115  			return err
   116  		}
   117  	}
   118  
   119  	return nil
   120  }