github.com/sacloud/iaas-api-go@v1.12.0/internal/dsl/result_type.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  // ResultType Operationからの戻り値の型情報
    24  type ResultType struct {
    25  	resourceName string
    26  	operation    *Operation
    27  	results      Results
    28  }
    29  
    30  // Type モデルの型情報
    31  func (r *ResultType) Type() meta.Type {
    32  	return r
    33  }
    34  
    35  // GoType 型名
    36  func (r *ResultType) GoType() string {
    37  	return fmt.Sprintf("%s%sResult", r.resourceName, r.operation.Name)
    38  }
    39  
    40  // GoPkg パッケージ名
    41  func (r *ResultType) GoPkg() string {
    42  	if IsOutOfSacloudPackage {
    43  		return "sacloud"
    44  	}
    45  	return ""
    46  }
    47  
    48  // GoImportPath インポートパス
    49  func (r *ResultType) GoImportPath() string {
    50  	if IsOutOfSacloudPackage {
    51  		return "github.com/sacloud/iaas-api-go"
    52  	}
    53  	return ""
    54  }
    55  
    56  // GoTypeSourceCode ソースコードでの型表現
    57  func (r *ResultType) GoTypeSourceCode() string {
    58  	name := r.GoType()
    59  	prefix := ""
    60  	if IsOutOfSacloudPackage {
    61  		prefix = "iaas."
    62  	}
    63  	return fmt.Sprintf("*%s%s", prefix, name)
    64  }
    65  
    66  // ZeroInitializeSourceCode 型に応じたzero値での初期化コード
    67  func (r *ResultType) ZeroInitializeSourceCode() string {
    68  	name := r.GoType()
    69  	if IsOutOfSacloudPackage {
    70  		name = "iaas." + name
    71  	}
    72  	return fmt.Sprintf("&%s{}", name)
    73  }
    74  
    75  // ZeroValueSourceCode 型に応じたzero値コード
    76  func (r *ResultType) ZeroValueSourceCode() string {
    77  	return "nil"
    78  }
    79  
    80  // ToPtrType ポインタ型への変換
    81  func (r *ResultType) ToPtrType() meta.Type {
    82  	return nil // not use
    83  }