github.com/oam-dev/kubevela@v1.9.11/pkg/definition/gen_sdk/openapi-generator/templates/go/utils.mustache (about)

     1  {{>partial_header}}
     2  package {{packageName}}
     3  
     4  import (
     5      "bytes"
     6  	"encoding/json"
     7  	"reflect"
     8  	"time"
     9  )
    10  
    11  // PtrBool is a helper routine that returns a pointer to given boolean value.
    12  func PtrBool(v bool) *bool { return &v }
    13  
    14  // PtrInt is a helper routine that returns a pointer to given integer value.
    15  func PtrInt(v int) *int { return &v }
    16  
    17  // PtrInt32 is a helper routine that returns a pointer to given integer value.
    18  func PtrInt32(v int32) *int32 { return &v }
    19  
    20  // PtrInt64 is a helper routine that returns a pointer to given integer value.
    21  func PtrInt64(v int64) *int64 { return &v }
    22  
    23  // PtrFloat32 is a helper routine that returns a pointer to given float value.
    24  func PtrFloat32(v float32) *float32 { return &v }
    25  
    26  // PtrFloat64 is a helper routine that returns a pointer to given float value.
    27  func PtrFloat64(v float64) *float64 { return &v }
    28  
    29  // PtrString is a helper routine that returns a pointer to given string value.
    30  func PtrString(v string) *string { return &v }
    31  
    32  // PtrTime is helper routine that returns a pointer to given Time value.
    33  func PtrTime(v time.Time) *time.Time { return &v }
    34  
    35  type NullableBool struct {
    36  	value *bool
    37  	isSet bool
    38  }
    39  
    40  func (v NullableBool) Get() *bool {
    41  	return v.value
    42  }
    43  
    44  func (v *NullableBool) Set(val *bool) {
    45  	v.value = val
    46  	v.isSet = true
    47  }
    48  
    49  func (v NullableBool) IsSet() bool {
    50  	return v.isSet
    51  }
    52  
    53  func (v *NullableBool) Unset() {
    54  	v.value = nil
    55  	v.isSet = false
    56  }
    57  
    58  func NewNullableBool(val *bool) *NullableBool {
    59  	return &NullableBool{value: val, isSet: true}
    60  }
    61  
    62  func (v NullableBool) MarshalJSON() ([]byte, error) {
    63  	return json.Marshal(v.value)
    64  }
    65  
    66  func (v *NullableBool) UnmarshalJSON(src []byte) error {
    67  	v.isSet = true
    68  	return json.Unmarshal(src, &v.value)
    69  }
    70  
    71  type NullableInt struct {
    72  	value *int
    73  	isSet bool
    74  }
    75  
    76  func (v NullableInt) Get() *int {
    77  	return v.value
    78  }
    79  
    80  func (v *NullableInt) Set(val *int) {
    81  	v.value = val
    82  	v.isSet = true
    83  }
    84  
    85  func (v NullableInt) IsSet() bool {
    86  	return v.isSet
    87  }
    88  
    89  func (v *NullableInt) Unset() {
    90  	v.value = nil
    91  	v.isSet = false
    92  }
    93  
    94  func NewNullableInt(val *int) *NullableInt {
    95  	return &NullableInt{value: val, isSet: true}
    96  }
    97  
    98  func (v NullableInt) MarshalJSON() ([]byte, error) {
    99  	return json.Marshal(v.value)
   100  }
   101  
   102  func (v *NullableInt) UnmarshalJSON(src []byte) error {
   103  	v.isSet = true
   104  	return json.Unmarshal(src, &v.value)
   105  }
   106  
   107  type NullableInt32 struct {
   108  	value *int32
   109  	isSet bool
   110  }
   111  
   112  func (v NullableInt32) Get() *int32 {
   113  	return v.value
   114  }
   115  
   116  func (v *NullableInt32) Set(val *int32) {
   117  	v.value = val
   118  	v.isSet = true
   119  }
   120  
   121  func (v NullableInt32) IsSet() bool {
   122  	return v.isSet
   123  }
   124  
   125  func (v *NullableInt32) Unset() {
   126  	v.value = nil
   127  	v.isSet = false
   128  }
   129  
   130  func NewNullableInt32(val *int32) *NullableInt32 {
   131  	return &NullableInt32{value: val, isSet: true}
   132  }
   133  
   134  func (v NullableInt32) MarshalJSON() ([]byte, error) {
   135  	return json.Marshal(v.value)
   136  }
   137  
   138  func (v *NullableInt32) UnmarshalJSON(src []byte) error {
   139  	v.isSet = true
   140  	return json.Unmarshal(src, &v.value)
   141  }
   142  
   143  type NullableInt64 struct {
   144  	value *int64
   145  	isSet bool
   146  }
   147  
   148  func (v NullableInt64) Get() *int64 {
   149  	return v.value
   150  }
   151  
   152  func (v *NullableInt64) Set(val *int64) {
   153  	v.value = val
   154  	v.isSet = true
   155  }
   156  
   157  func (v NullableInt64) IsSet() bool {
   158  	return v.isSet
   159  }
   160  
   161  func (v *NullableInt64) Unset() {
   162  	v.value = nil
   163  	v.isSet = false
   164  }
   165  
   166  func NewNullableInt64(val *int64) *NullableInt64 {
   167  	return &NullableInt64{value: val, isSet: true}
   168  }
   169  
   170  func (v NullableInt64) MarshalJSON() ([]byte, error) {
   171  	return json.Marshal(v.value)
   172  }
   173  
   174  func (v *NullableInt64) UnmarshalJSON(src []byte) error {
   175  	v.isSet = true
   176  	return json.Unmarshal(src, &v.value)
   177  }
   178  
   179  type NullableFloat32 struct {
   180  	value *float32
   181  	isSet bool
   182  }
   183  
   184  func (v NullableFloat32) Get() *float32 {
   185  	return v.value
   186  }
   187  
   188  func (v *NullableFloat32) Set(val *float32) {
   189  	v.value = val
   190  	v.isSet = true
   191  }
   192  
   193  func (v NullableFloat32) IsSet() bool {
   194  	return v.isSet
   195  }
   196  
   197  func (v *NullableFloat32) Unset() {
   198  	v.value = nil
   199  	v.isSet = false
   200  }
   201  
   202  func NewNullableFloat32(val *float32) *NullableFloat32 {
   203  	return &NullableFloat32{value: val, isSet: true}
   204  }
   205  
   206  func (v NullableFloat32) MarshalJSON() ([]byte, error) {
   207  	return json.Marshal(v.value)
   208  }
   209  
   210  func (v *NullableFloat32) UnmarshalJSON(src []byte) error {
   211  	v.isSet = true
   212  	return json.Unmarshal(src, &v.value)
   213  }
   214  
   215  type NullableFloat64 struct {
   216  	value *float64
   217  	isSet bool
   218  }
   219  
   220  func (v NullableFloat64) Get() *float64 {
   221  	return v.value
   222  }
   223  
   224  func (v *NullableFloat64) Set(val *float64) {
   225  	v.value = val
   226  	v.isSet = true
   227  }
   228  
   229  func (v NullableFloat64) IsSet() bool {
   230  	return v.isSet
   231  }
   232  
   233  func (v *NullableFloat64) Unset() {
   234  	v.value = nil
   235  	v.isSet = false
   236  }
   237  
   238  func NewNullableFloat64(val *float64) *NullableFloat64 {
   239  	return &NullableFloat64{value: val, isSet: true}
   240  }
   241  
   242  func (v NullableFloat64) MarshalJSON() ([]byte, error) {
   243  	return json.Marshal(v.value)
   244  }
   245  
   246  func (v *NullableFloat64) UnmarshalJSON(src []byte) error {
   247  	v.isSet = true
   248  	return json.Unmarshal(src, &v.value)
   249  }
   250  
   251  type NullableString struct {
   252  	value *string
   253  	isSet bool
   254  }
   255  
   256  func (v NullableString) Get() *string {
   257  	return v.value
   258  }
   259  
   260  func (v *NullableString) Set(val *string) {
   261  	v.value = val
   262  	v.isSet = true
   263  }
   264  
   265  func (v NullableString) IsSet() bool {
   266  	return v.isSet
   267  }
   268  
   269  func (v *NullableString) Unset() {
   270  	v.value = nil
   271  	v.isSet = false
   272  }
   273  
   274  func NewNullableString(val *string) *NullableString {
   275  	return &NullableString{value: val, isSet: true}
   276  }
   277  
   278  func (v NullableString) MarshalJSON() ([]byte, error) {
   279  	return json.Marshal(v.value)
   280  }
   281  
   282  func (v *NullableString) UnmarshalJSON(src []byte) error {
   283  	v.isSet = true
   284  	return json.Unmarshal(src, &v.value)
   285  }
   286  
   287  type NullableTime struct {
   288  	value *time.Time
   289  	isSet bool
   290  }
   291  
   292  func (v NullableTime) Get() *time.Time {
   293  	return v.value
   294  }
   295  
   296  func (v *NullableTime) Set(val *time.Time) {
   297  	v.value = val
   298  	v.isSet = true
   299  }
   300  
   301  func (v NullableTime) IsSet() bool {
   302  	return v.isSet
   303  }
   304  
   305  func (v *NullableTime) Unset() {
   306  	v.value = nil
   307  	v.isSet = false
   308  }
   309  
   310  func NewNullableTime(val *time.Time) *NullableTime {
   311  	return &NullableTime{value: val, isSet: true}
   312  }
   313  
   314  func (v NullableTime) MarshalJSON() ([]byte, error) {
   315  	return v.value.MarshalJSON()
   316  }
   317  
   318  func (v *NullableTime) UnmarshalJSON(src []byte) error {
   319  	v.isSet = true
   320  	return json.Unmarshal(src, &v.value)
   321  }
   322  
   323  // IsNil checks if an input is nil
   324  func IsNil(i interface{}) bool {
   325  	if i == nil {
   326  		return true
   327  	}
   328  	switch reflect.TypeOf(i).Kind() {
   329  	case reflect.Chan, reflect.Func, reflect.Map, reflect.Ptr, reflect.UnsafePointer, reflect.Interface, reflect.Slice:
   330  		return reflect.ValueOf(i).IsNil()
   331  	case reflect.Array:
   332  		return reflect.ValueOf(i).IsZero()
   333  	}
   334  	return false
   335  }
   336  
   337  type MappedNullable interface {
   338  	ToMap() (map[string]interface{}, error)
   339  }
   340  
   341  // A wrapper for strict JSON decoding
   342  func NewStrictDecoder(data []byte) *json.Decoder {
   343      dec := json.NewDecoder(bytes.NewBuffer(data))
   344      dec.DisallowUnknownFields()
   345      return dec
   346  }