go.uber.org/cadence@v1.2.9/internal/compatibility/thrift/helpers.go (about)

     1  // Copyright (c) 2021 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package thrift
    22  
    23  import (
    24  	"time"
    25  
    26  	gogo "github.com/gogo/protobuf/types"
    27  
    28  	"go.uber.org/cadence/internal/common"
    29  )
    30  
    31  func boolPtr(b bool) *bool {
    32  	return &b
    33  }
    34  
    35  func toDoubleValue(v *gogo.DoubleValue) *float64 {
    36  	if v == nil {
    37  		return nil
    38  	}
    39  	return &v.Value
    40  }
    41  
    42  func toInt64Value(v *gogo.Int64Value) *int64 {
    43  	if v == nil {
    44  		return nil
    45  	}
    46  	return common.Int64Ptr(v.Value)
    47  }
    48  
    49  func timeToUnixNano(t *gogo.Timestamp) *int64 {
    50  	if t == nil {
    51  		return nil
    52  	}
    53  	timestamp, err := gogo.TimestampFromProto(t)
    54  	if err != nil {
    55  		panic(err)
    56  	}
    57  	return common.Int64Ptr(timestamp.UnixNano())
    58  }
    59  
    60  func durationToDays(d *gogo.Duration) *int32 {
    61  	if d == nil {
    62  		return nil
    63  	}
    64  	duration, err := gogo.DurationFromProto(d)
    65  	if err != nil {
    66  		panic(err)
    67  	}
    68  	return common.Int32Ptr(int32(duration / (24 * time.Hour)))
    69  }
    70  
    71  func durationToSeconds(d *gogo.Duration) *int32 {
    72  	if d == nil {
    73  		return nil
    74  	}
    75  	duration, err := gogo.DurationFromProto(d)
    76  	if err != nil {
    77  		panic(err)
    78  	}
    79  	return common.Int32Ptr(int32(duration / time.Second))
    80  }
    81  
    82  func int32To64(v *int32) *int64 {
    83  	if v == nil {
    84  		return nil
    85  	}
    86  	return common.Int64Ptr(int64(*v))
    87  }
    88  
    89  type fieldSet map[string]struct{}
    90  
    91  func newFieldSet(mask *gogo.FieldMask) fieldSet {
    92  	if mask == nil {
    93  		return nil
    94  	}
    95  	fs := map[string]struct{}{}
    96  	for _, field := range mask.Paths {
    97  		fs[field] = struct{}{}
    98  	}
    99  	return fs
   100  }
   101  
   102  func (fs fieldSet) isSet(field string) bool {
   103  	if fs == nil {
   104  		return true
   105  	}
   106  	_, ok := fs[field]
   107  	return ok
   108  }