github.com/sacloud/iaas-api-go@v1.12.0/internal/dsl/resource.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 // Resources []*Resourceのエイリアス 18 type Resources []*Resource 19 20 // ImportStatements コード生成時に利用するimport文を生成する 21 func (r Resources) ImportStatements(additionalImports ...string) []string { 22 ss := wrapByDoubleQuote(additionalImports...) 23 24 for _, re := range r { 25 ss = append(ss, re.ImportStatements()...) 26 } 27 28 return uniqStrings(ss) 29 } 30 31 // ImportStatementsForModelDef Resources配下に含まれる全てのモデルのフィールドを含めたimport文を生成する 32 func (r Resources) ImportStatementsForModelDef(additionalImports ...string) []string { 33 ss := wrapByDoubleQuote(additionalImports...) 34 for _, m := range r.Models() { 35 ss = append(ss, m.ImportStatementsForModelDef()...) 36 } 37 return uniqStrings(ss) 38 } 39 40 // Define リソースの定義 41 func (r *Resources) Define(rs *Resource) { 42 if *r == nil { 43 rr := Resources{} 44 *r = rr 45 } 46 *r = append(*r, rs) 47 } 48 49 // Models モデル一覧を取得 50 func (r Resources) Models() Models { 51 ms := Models{} 52 for _, res := range r { 53 for _, o := range res.Operations { 54 ms = append(ms, o.Models()...) 55 } 56 } 57 return ms.UniqByName() 58 } 59 60 // Resource APIで操作する対象のリソース 61 type Resource struct { 62 Name string // リソース名 e.g.: Server 63 PathName string // リソースのパス名 APIのURLで利用される e.g.: server 省略した場合はNameを小文字にしたものとなる 64 PathSuffix string // APIのURLで利用されるプレフィックス e.g.: api/cloud/1.1 65 IsGlobal bool // 全ゾーンで共通リソース(グローバルリソース) 66 Operations Operations // このリソースに対する操作 67 } 68 69 // GetPathName リソースのパス名 APIのエンドポイントURLの算出で利用される 例: server 70 // 71 // 省略した場合はNameをスネークケース(小文字+アンダーバー)に変換したものが利用される 72 func (r *Resource) GetPathName() string { 73 if r.PathName != "" { 74 return r.PathName 75 } 76 return toSnakeCaseName(r.Name) 77 } 78 79 // GetPathSuffix PathSuffixの取得 80 func (r *Resource) GetPathSuffix() string { 81 if r.PathSuffix != "" { 82 return r.PathSuffix 83 } 84 return CloudAPISuffix 85 } 86 87 // FileSafeName スネークケースにしたResourceの名前、コード生成時の保存先ファイル名に利用される 88 func (r *Resource) FileSafeName() string { 89 return toSnakeCaseName(r.Name) 90 } 91 92 // FileSafeServicePath Nameを全て小文字にしたもの、サービスコード生成時の保存先ディレクトリ名に利用される 93 func (r *Resource) FileSafeServicePath() string { 94 v := toLower(r.Name) 95 switch v { 96 case "switch": 97 return "swytch" 98 case "interface": 99 return "iface" 100 default: 101 return v 102 } 103 } 104 105 // TypeName 型名を返す、コード生成時の型定義などで利用される 106 func (r *Resource) TypeName() string { 107 return r.Name 108 } 109 110 // ImportStatements コード生成時に利用するimport文を生成する 111 func (r *Resource) ImportStatements(additionalImports ...string) []string { 112 ss := wrapByDoubleQuote(additionalImports...) 113 for _, o := range r.Operations { 114 ss = append(ss, o.ImportStatements()...) 115 } 116 117 return uniqStrings(ss) 118 }