go.temporal.io/server@v1.23.0/common/payload/payload.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 payload 26 27 import ( 28 "bytes" 29 30 commonpb "go.temporal.io/api/common/v1" 31 "go.temporal.io/sdk/converter" 32 "go.temporal.io/server/common/util" 33 "golang.org/x/exp/maps" 34 ) 35 36 var ( 37 defaultDataConverter = converter.GetDefaultDataConverter() 38 39 nilPayload, _ = Encode(nil) 40 emptySlicePayload, _ = Encode([]string{}) 41 ) 42 43 func EncodeString(str string) *commonpb.Payload { 44 // Error can be safely ignored here because string always can be converted to JSON 45 p, _ := defaultDataConverter.ToPayload(str) 46 return p 47 } 48 49 func EncodeBytes(bytes []byte) *commonpb.Payload { 50 // Error can be safely ignored here because []byte always can be raw encoded 51 p, _ := defaultDataConverter.ToPayload(bytes) 52 return p 53 } 54 55 func Encode(value interface{}) (*commonpb.Payload, error) { 56 return defaultDataConverter.ToPayload(value) 57 } 58 59 func Decode(p *commonpb.Payload, valuePtr interface{}) error { 60 return defaultDataConverter.FromPayload(p, valuePtr) 61 } 62 63 func ToString(p *commonpb.Payload) string { 64 return defaultDataConverter.ToString(p) 65 } 66 67 // MergeMapOfPayload returns a new map resulting from merging map `src` into `dst`. 68 // If a key in `src` already exists in `dst`, then the value in `src` replaces 69 // the value in `dst`. 70 // If a key in `src` has nil or empty slice payload value, then it deletes 71 // the key from `dst` if it exists. 72 // For example: 73 // 74 // dst := map[string]*commonpb.Payload{ 75 // "key1": EncodeString("value1"), 76 // "key2": EncodeString("value2"), 77 // } 78 // src := map[string]*commonpb.Payload{ 79 // "key1": EncodeString("newValue1"), 80 // "key2": nilPayload, 81 // } 82 // res := MergeMapOfPayload(dst, src) 83 // 84 // The resulting map `res` is: 85 // 86 // map[string]*commonpb.Payload{ 87 // "key1": EncodeString("newValue1"), 88 // } 89 func MergeMapOfPayload( 90 dst map[string]*commonpb.Payload, 91 src map[string]*commonpb.Payload, 92 ) map[string]*commonpb.Payload { 93 if src == nil { 94 return maps.Clone(dst) 95 } 96 res := util.CloneMapNonNil(dst) 97 for k, v := range src { 98 if isEqual(v, nilPayload) || isEqual(v, emptySlicePayload) { 99 delete(res, k) 100 } else { 101 res[k] = v 102 } 103 } 104 return res 105 } 106 107 // isEqual returns true if both have the same encoding and data. 108 // It does not take additional metadata into consideration. 109 // Note that data equality it's not the same as semantic equality, ie., 110 // `[]` and `[ ]` are semantically the same, but different not data-wise. 111 // Only use if you know that the data is encoded the same way. 112 func isEqual(a, b *commonpb.Payload) bool { 113 aEnc := a.GetMetadata()[converter.MetadataEncoding] 114 bEnc := a.GetMetadata()[converter.MetadataEncoding] 115 return bytes.Equal(aEnc, bEnc) && bytes.Equal(a.GetData(), b.GetData()) 116 }