github.com/sacloud/iaas-api-go@v1.12.0/internal/define/names/resource_name.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 names
    16  
    17  import (
    18  	"fmt"
    19  	"strings"
    20  
    21  	"github.com/sacloud/iaas-api-go/internal/dsl"
    22  )
    23  
    24  // ResourceFieldName リソース名がペイロードなどで利用される場合のフィールド名、コード生成時に利用される
    25  func ResourceFieldName(resourceName string, form dsl.PayloadForm) string {
    26  	switch {
    27  	case form.IsSingular():
    28  		return resourceName
    29  	case form.IsPlural():
    30  		switch {
    31  		case
    32  			resourceName == "AutoScale",
    33  			resourceName == "ESME",
    34  			resourceName == "NFS",
    35  			resourceName == "DNS",
    36  			resourceName == "Internet",
    37  			resourceName == "IPAddress",
    38  			strings.HasSuffix(resourceName, "Info"):
    39  			return resourceName
    40  		case resourceName == "ContainerRegistry":
    41  			return "ContainerRegistries"
    42  		case resourceName == "CertificateAuthority":
    43  			return "CertificateAuthorities"
    44  		case
    45  			strings.HasSuffix(resourceName, "ch"),
    46  			strings.HasSuffix(resourceName, "ss"):
    47  			return resourceName + "es"
    48  		default:
    49  			return resourceName + "s"
    50  		}
    51  	default:
    52  		return ""
    53  	}
    54  }
    55  
    56  // CreateParameterName Create操作に渡すパラメータの名称
    57  func CreateParameterName(resourceName string) string {
    58  	return RequestParameterName(resourceName, "Create")
    59  }
    60  
    61  // UpdateParameterName Update操作に渡すパラメータの名称
    62  func UpdateParameterName(resourceName string) string {
    63  	return RequestParameterName(resourceName, "Update")
    64  }
    65  
    66  // UpdateSettingsParameterName UpdateSettings操作に渡すパラメータの名称
    67  func UpdateSettingsParameterName(resourceName string) string {
    68  	return RequestParameterName(resourceName, "UpdateSettings")
    69  }
    70  
    71  // RequestParameterName 任意の操作に渡すパラメータの名称
    72  func RequestParameterName(resourceName, funcName string) string {
    73  	return fmt.Sprintf("%s%sRequest", resourceName, funcName)
    74  }