github.com/sacloud/iaas-api-go@v1.12.0/internal/dsl/result.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 /****************************************************************************** 24 * Results 25 *****************************************************************************/ 26 27 // Results Resultの配列 28 type Results []*Result 29 30 // Models Resultsに登録されているModelを返す 31 func (r *Results) Models() Models { 32 ms := Models{} 33 for _, res := range *r { 34 ms = append(ms, res.Model) 35 ms = append(ms, res.Model.FieldModels()...) 36 } 37 return ms 38 } 39 40 /****************************************************************************** 41 * Result 42 ***************,**************************************************************/ 43 44 // Result Operationでの戻り値定義 45 type Result struct { 46 SourceField string // エンベロープのフィールド名 47 DestField string // xxxResultでのフィールド名 48 IsPlural bool 49 Model *Model // パラメータの型情報 50 Tags *FieldTags 51 } 52 53 // TagString タグの文字列表現 54 func (r *Result) TagString() string { 55 if r.Tags == nil { 56 prefix := "" 57 if r.IsPlural { 58 prefix = "[]" 59 } 60 r.Tags = &FieldTags{ 61 JSON: ",omitempty", 62 MapConv: fmt.Sprintf("%s%s,omitempty,recursive", prefix, r.SourceField), 63 } 64 } 65 return fmt.Sprintf("`%s`", r.Tags.String()) 66 } 67 68 // ImportStatements コード生成時に利用するimport文を生成する 69 func (r *Result) ImportStatements(additionalImports ...string) []string { 70 return r.Model.ImportStatementsForModelDef(additionalImports...) 71 } 72 73 // Type モデルの型情報 74 func (r *Result) Type() meta.Type { 75 return r 76 } 77 78 // GoType 型名 79 func (r *Result) GoType() string { 80 return r.Model.Name 81 } 82 83 // GoPkg パッケージ名 84 func (r *Result) GoPkg() string { 85 return r.Model.GoPkg() 86 } 87 88 // GoImportPath インポートパス 89 func (r *Result) GoImportPath() string { 90 return r.Model.GoImportPath() 91 } 92 93 // GoTypeSourceCode ソースコードでの型表現 94 func (r *Result) GoTypeSourceCode() string { 95 return r.Model.GoTypeSourceCode() 96 } 97 98 // ZeroInitializeSourceCode 型に応じたzero値での初期化コード 99 func (r *Result) ZeroInitializeSourceCode() string { 100 return r.Model.ZeroInitializeSourceCode() 101 } 102 103 // ZeroValueSourceCode 型に応じたzero値コード 104 func (r *Result) ZeroValueSourceCode() string { 105 return r.Model.ZeroValueSourceCode() 106 } 107 108 // ToPtrType ポインタ型への変換 109 func (r *Result) ToPtrType() meta.Type { 110 return nil // not use 111 }