github.com/sacloud/iaas-api-go@v1.12.0/types/id.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 types
    16  
    17  import (
    18  	"encoding/json"
    19  	"fmt"
    20  	"strconv"
    21  )
    22  
    23  // ID さくらのクラウド上のリソースのIDを示す
    24  //
    25  // APIリクエスト/レスポンスに文字列/数値が混在するためここで吸収する
    26  type ID int64
    27  
    28  // UnmarshalJSON implements unmarshal from both of JSON number and JSON string
    29  func (i *ID) UnmarshalJSON(b []byte) error {
    30  	s := string(b)
    31  	if s == "" || s == "null" {
    32  		return nil
    33  	}
    34  	var n json.Number
    35  	if err := json.Unmarshal(b, &n); err != nil {
    36  		return err
    37  	}
    38  	id, err := n.Int64()
    39  	if err != nil {
    40  		return err
    41  	}
    42  	*i = ID(id)
    43  	return nil
    44  }
    45  
    46  // IsEmpty return true if ID is empty or zero value
    47  func (i ID) IsEmpty() bool {
    48  	return i.Int64() == 0
    49  }
    50  
    51  // String returns the literal text of the number.
    52  func (i ID) String() string {
    53  	if i.IsEmpty() {
    54  		return ""
    55  	}
    56  	return fmt.Sprintf("%d", i)
    57  }
    58  
    59  // Int64 returns the number as an int64.
    60  func (i ID) Int64() int64 {
    61  	return int64(i)
    62  }
    63  
    64  // Int64ID creates new ID from int64
    65  func Int64ID(id int64) ID {
    66  	return ID(id)
    67  }
    68  
    69  // StringID creates new ID from string
    70  func StringID(id string) ID {
    71  	intID, _ := strconv.ParseInt(id, 10, 64)
    72  	return ID(intID)
    73  }
    74  
    75  // IDs IDのコレクション型
    76  type IDs []ID
    77  
    78  // StringSlice stringスライスを返す
    79  func (i IDs) StringSlice() []string {
    80  	var ret []string
    81  	for _, id := range i {
    82  		ret = append(ret, id.String())
    83  	}
    84  	return ret
    85  }
    86  
    87  // Int64Slice Int64スライスを返す
    88  func (i IDs) Int64Slice() []int64 {
    89  	var ret []int64
    90  	for _, id := range i {
    91  		ret = append(ret, id.Int64())
    92  	}
    93  	return ret
    94  }
    95  
    96  // IsEmptyAll すべてゼロ値な場合にtrueを返す
    97  func (i IDs) IsEmptyAll() bool {
    98  	for _, id := range i {
    99  		if !id.IsEmpty() {
   100  			return false
   101  		}
   102  	}
   103  	return true
   104  }
   105  
   106  // IsEmptyAny 1つでもゼロ値な場合にtrueを返す
   107  func (i IDs) IsEmptyAny() bool {
   108  	for _, id := range i {
   109  		if id.IsEmpty() {
   110  			return true
   111  		}
   112  	}
   113  	return false
   114  }