github.com/go-swagger/go-swagger@v0.31.0/docs/reference/templates/template_layout.md (about)

     1  ---
     2  title: Code generation templates
     3  date: 2023-01-01T01:01:01-08:00
     4  draft: true
     5  ---
     6  # Custom generation
     7  
     8  To completely customize the templates that are being considered, their file names and paths, go-swagger allows you to pass in a configuration file.
     9  There are basically 4 types of items that are being generated:
    10  
    11    * [Models](https://godoc.org/github.com/go-swagger/go-swagger/generator#GenDefinition)
    12    * [Operations](https://godoc.org/github.com/go-swagger/go-swagger/generator#GenOperation)
    13    * [Operation groups](https://godoc.org/github.com/go-swagger/go-swagger/generator#GenOperationGroup) (tagged groups of operations)
    14    * [Application](https://godoc.org/github.com/go-swagger/go-swagger/generator#GenApp)
    15  
    16  You provide a configuration that describes the type of template, the source for where to find the template. For built-in templates the name should be prefixed with `asset:`.
    17  You also provide the target directory and the file name. Directory and file names are processed as templates too and allow for a number of filters.
    18  
    19  We use the viper library to read config values, this means you can provide the configuration file in whichever format you like: json, yaml, hcl or toml.
    20  
    21  ## Available filters in templates
    22  
    23  We use the [sprig](https://masterminds.github.io/sprig/) library to provide a large variety of template functions for creating custom templates.
    24  
    25  In addition to this there are a number of added filters you can use inside a template to manipulate values:
    26  
    27  Filter | Description
    28  skip_exists|boolean|Skip generating content for a file if the specified target file already exists. Use this for files the user needs to customise.
    29  skip_format|boolean|Skip formatting code from the template according to the standard golang rules. This may be useful if you have your own coding conventions that custom templates already adhere to, or if you are generating non-golang code.
    30  
    31  ## Server generation
    32  
    33  ```
    34  swagger generate server -A TodoList -f ./swagger.json -C default-server.yml
    35  ```
    36  
    37  For the default server generator this config file would have the following content:
    38  
    39  ```yaml
    40  layout:
    41    application:
    42      - name: configure
    43        source: asset:serverConfigureapi
    44        target: "{{ joinFilePath .Target .ServerPackage }}"
    45        file_name: "configure_{{ .Name }}.go"
    46        skip_exists: true
    47      - name: main
    48        source: asset:serverMain
    49        target: "{{ joinFilePath .Target \"cmd\" (dasherize (pascalize .Name)) }}-server"
    50        file_name: "main.go"
    51      - name: embedded_spec
    52        source: asset:swaggerJsonEmbed
    53        target: "{{ joinFilePath .Target .ServerPackage }}"
    54        file_name: "embedded_spec.go"
    55      - name: server
    56        source: asset:serverServer
    57        target: "{{ joinFilePath .Target .ServerPackage }}"
    58        file_name: "server.go"
    59      - name: builder
    60        source: asset:serverBuilder
    61        target: "{{ joinFilePath .Target .ServerPackage .Package }}"
    62        file_name: "{{ snakize (pascalize .Name) }}_api.go"
    63      - name: doc
    64        source: asset:serverDoc
    65        target: "{{ joinFilePath .Target .ServerPackage }}"
    66        file_name: "doc.go"
    67    models:
    68      - name: definition
    69        source: asset:model
    70        target: "{{ joinFilePath .Target .ModelPackage }}"
    71        file_name: "{{ (snakize (pascalize .Name)) }}.go"
    72    operations:
    73      - name: parameters
    74        source: asset:serverParameter
    75        target: "{{ if gt (len .Tags) 0 }}{{ joinFilePath .Target .ServerPackage .APIPackage .Package  }}{{ else }}{{ joinFilePath .Target .ServerPackage .Package  }}{{ end }}"
    76        file_name: "{{ (snakize (pascalize .Name)) }}_parameters.go"
    77      - name: responses
    78        source: asset:serverResponses
    79        target: "{{ if gt (len .Tags) 0 }}{{ joinFilePath .Target .ServerPackage .APIPackage .Package  }}{{ else }}{{ joinFilePath .Target .ServerPackage .Package  }}{{ end }}"
    80        file_name: "{{ (snakize (pascalize .Name)) }}_responses.go"
    81      - name: handler
    82        source: asset:serverOperation
    83        target: "{{ if gt (len .Tags) 0 }}{{ joinFilePath .Target .ServerPackage .APIPackage .Package  }}{{ else }}{{ joinFilePath .Target .ServerPackage .Package  }}{{ end }}"
    84        file_name: "{{ (snakize (pascalize .Name)) }}.go"
    85    operation_groups:
    86  
    87  ```
    88  
    89  ## Client generation
    90  
    91  ```
    92  swagger generate client -A TodoList -f ./swagger.json -C default-client.yml
    93  ```
    94  
    95  For the default client generator this config file would have the following content.
    96  
    97  ```yaml
    98  layout:
    99    application:
   100      - name: facade
   101        source: asset:clientFacade
   102        target: "{{ joinFilePath .Target .ClientPackage }}"
   103        file_name: "{{ .Name }}_client.go"
   104    models:
   105      - name: definition
   106        source: asset:model
   107        target: "{{ joinFilePath .Target .ModelPackage }}"
   108        file_name: "{{ (snakize (pascalize .Name)) }}.go"
   109    operations:
   110      - name: parameters
   111        source: asset:clientParameter
   112        target: "{{ joinFilePath .Target .ClientPackage .Package }}"
   113        file_name: "{{ (snakize (pascalize .Name)) }}_parameters.go"
   114      - name: responses
   115        source: asset:clientResponse
   116        target: "{{ joinFilePath .Target .ClientPackage .Package }}"
   117        file_name: "{{ (snakize (pascalize .Name)) }}_responses.go"
   118    operation_groups:
   119      - name: client
   120        source: asset:clientClient
   121        target: "{{ joinFilePath .Target .ClientPackage .Name }}"
   122        file_name: "{{ (snakize (pascalize .Name)) }}_client.go"
   123  ```