go.temporal.io/server@v1.23.0/common/convert/convert.go (about) 1 // The MIT License 2 // 3 // Copyright (c) 2020 Temporal Technologies Inc. All rights reserved. 4 // 5 // Copyright (c) 2020 Uber Technologies, Inc. 6 // 7 // Permission is hereby granted, free of charge, to any person obtaining a copy 8 // of this software and associated documentation files (the "Software"), to deal 9 // in the Software without restriction, including without limitation the rights 10 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 // copies of the Software, and to permit persons to whom the Software is 12 // furnished to do so, subject to the following conditions: 13 // 14 // The above copyright notice and this permission notice shall be included in 15 // all copies or substantial portions of the Software. 16 // 17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 // THE SOFTWARE. 24 25 package convert 26 27 import ( 28 "math" 29 "strconv" 30 ) 31 32 // IntPtr makes a copy and returns the pointer to an int. 33 func IntPtr(v int) *int { 34 return &v 35 } 36 37 // Int16Ptr makes a copy and returns the pointer to an int16. 38 func Int16Ptr(v int16) *int16 { 39 return &v 40 } 41 42 // Int32Ptr makes a copy and returns the pointer to an int32. 43 func Int32Ptr(v int32) *int32 { 44 return &v 45 } 46 47 // Int64Ptr makes a copy and returns the pointer to an int64. 48 func Int64Ptr(v int64) *int64 { 49 return &v 50 } 51 52 // BoolPtr makes a copy and returns the pointer to a bool. 53 func BoolPtr(v bool) *bool { 54 return &v 55 } 56 57 // StringPtr makes a copy and returns the pointer to a string. 58 func StringPtr(v string) *string { 59 return &v 60 } 61 62 // Int32Ceil return the int32 ceil of a float64 63 func Int32Ceil(v float64) int32 { 64 return int32(math.Ceil(v)) 65 } 66 67 // Int64Ceil return the int64 ceil of a float64 68 func Int64Ceil(v float64) int64 { 69 return int64(math.Ceil(v)) 70 } 71 72 func IntToString(v int) string { 73 return Int64ToString(int64(v)) 74 } 75 76 func Uint64ToString(v uint64) string { 77 return strconv.FormatUint(v, 10) 78 } 79 80 func Int64ToString(v int64) string { 81 return strconv.FormatInt(v, 10) 82 } 83 84 func Int32ToString(v int32) string { 85 return Int64ToString(int64(v)) 86 } 87 88 func Uint16ToString(v uint16) string { 89 return strconv.FormatUint(uint64(v), 10) 90 } 91 92 func Int64SetToSlice( 93 inputs map[int64]struct{}, 94 ) []int64 { 95 outputs := make([]int64, len(inputs)) 96 i := 0 97 for item := range inputs { 98 outputs[i] = item 99 i++ 100 } 101 return outputs 102 } 103 104 func Int64SliceToSet( 105 inputs []int64, 106 ) map[int64]struct{} { 107 outputs := make(map[int64]struct{}, len(inputs)) 108 for _, item := range inputs { 109 outputs[item] = struct{}{} 110 } 111 return outputs 112 } 113 114 func StringSetToSlice( 115 inputs map[string]struct{}, 116 ) []string { 117 outputs := make([]string, len(inputs)) 118 i := 0 119 for item := range inputs { 120 outputs[i] = item 121 i++ 122 } 123 return outputs 124 } 125 func StringSliceToSet( 126 inputs []string, 127 ) map[string]struct{} { 128 outputs := make(map[string]struct{}, len(inputs)) 129 for _, item := range inputs { 130 outputs[item] = struct{}{} 131 } 132 return outputs 133 }