github.com/sacloud/iaas-api-go@v1.12.0/internal/dsl/argument.go (about) 1 // Copyright 2022-2023 The sacloud/iaas-api-go Authors 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 dsl 16 17 import ( 18 "fmt" 19 20 "github.com/sacloud/iaas-api-go/internal/dsl/meta" 21 ) 22 23 // Arguments Operationへの引数リスト 24 type Arguments []*Argument 25 26 var ( 27 // ArgumentID 引数でのIDを示すValue 28 ArgumentID = &Argument{ 29 Name: "id", 30 Type: meta.TypeID, 31 } 32 ) 33 34 // Argument 引数の型情報 35 type Argument struct { 36 Name string // パラメータ名、引数名に利用される 37 Type meta.Type // パラメータの型情報 38 PathFormatAlias string // リクエストパス組み立て時に利用するパラメータ名のエイリアス 省略時はNameとなる 39 MapConvTag string 40 } 41 42 // ImportStatements コード生成時に利用するimport文を生成する 43 func (a *Argument) ImportStatements() []string { 44 if a.Type.GoPkg() == "" { 45 return []string{} 46 } 47 return wrapByDoubleQuote(a.Type.GoImportPath()) 48 } 49 50 // PackageName インポートパスからパッケージ名を取得する 51 func (a *Argument) PackageName() string { 52 return a.Type.GoPkg() 53 } 54 55 // ArgName 引数の変数名、コード生成で利用される 56 func (a *Argument) ArgName() string { 57 return a.Name 58 } 59 60 // TypeName 型名の文字列表現、コード生成で利用される 61 func (a *Argument) TypeName() string { 62 return a.Type.GoTypeSourceCode() 63 } 64 65 // ZeroInitializer 値を0初期化する文のコードの文字列表現、コード生成で利用される 66 func (a *Argument) ZeroInitializer() string { 67 return a.Type.ZeroInitializeSourceCode() 68 } 69 70 // ZeroValueOnSource コード上でのゼロ値の文字列表現。コード生成時に利用する 71 func (a *Argument) ZeroValueOnSource() string { 72 return a.Type.ZeroValueSourceCode() 73 } 74 75 // MapConvTagSrc コード上でのmapconvタグの文字列表現。コード生成時に利用する 76 func (a *Argument) MapConvTagSrc() string { 77 if a.MapConvTag == "" { 78 return "" 79 } 80 return fmt.Sprintf("`mapconv:\"%s\"`", a.MapConvTag) 81 } 82 83 // PathFormatName リクエストパス組み立て時に利用するパラメータ名の 84 func (a *Argument) PathFormatName() string { 85 if a.PathFormatAlias != "" { 86 return a.PathFormatAlias 87 } 88 return a.Name 89 } 90 91 // MappableArgument 引数定義の追加 92 func MappableArgument(name string, model *Model, destField string) *Argument { 93 return &Argument{ 94 Name: name, 95 Type: model, 96 MapConvTag: fmt.Sprintf("%s,recursive", destField), 97 } 98 } 99 100 // PassthroughModelArgument 引数定義の追加 101 func PassthroughModelArgument(name string, model *Model) *Argument { 102 return &Argument{ 103 Name: name, 104 Type: model, 105 MapConvTag: ",squash", 106 } 107 }