Files
tasks/common/payload/payload.go
Alex ShtinandGitHub 8e8f1b0f7c Remove license header from every file (#7689)
## What changed?
<!-- Describe what has changed in this PR -->
Remove license header from every file. Because it is really hard to
follow in this PR here is the summary:
1. License header is removed from all `.go` and `.proto` files
:fireworks::fireworks:🎆.
2. `LICENSE` file in the root directory has only Temporal and Uber
copyrights.
3. 5 other `LICENSE` files added to the packages which have copyrights
different from Temporal and Uber: Datadog, Xargin, "Mat Ryer, Tyler
Bunnell and contributors".
4. `license_file` flag is removed from all code generation tools.
5. `copyright_file` flag is removed from `go:generate mockgen`
directive.
6. All copyright related targets are removed from `Makefile`.
7. Updated Temporal copyright year to 2025 everywhere.

## Why?
<!-- Tell your future self why have you made these changes -->
I double checked with legal department that it is not needed to have
license header in every file. One file per repo is enough. I put all
copyrights to the root `LICENSE` file and removed header from all other
files. Also updated tools and `Makefile`.
2025-05-01 18:50:21 -07:00

93 lines
2.6 KiB
Go

package payload
import (
"bytes"
"maps"
commonpb "go.temporal.io/api/common/v1"
"go.temporal.io/sdk/converter"
"go.temporal.io/server/common/util"
)
var (
defaultDataConverter = converter.GetDefaultDataConverter()
nilPayload, _ = Encode(nil)
emptySlicePayload, _ = Encode([]string{})
)
func EncodeString(str string) *commonpb.Payload {
// Error can be safely ignored here because string always can be converted to JSON
p, _ := defaultDataConverter.ToPayload(str)
return p
}
func EncodeBytes(bytes []byte) *commonpb.Payload {
// Error can be safely ignored here because []byte always can be raw encoded
p, _ := defaultDataConverter.ToPayload(bytes)
return p
}
func Encode(value interface{}) (*commonpb.Payload, error) {
return defaultDataConverter.ToPayload(value)
}
func Decode(p *commonpb.Payload, valuePtr interface{}) error {
return defaultDataConverter.FromPayload(p, valuePtr)
}
func ToString(p *commonpb.Payload) string {
return defaultDataConverter.ToString(p)
}
// MergeMapOfPayload returns a new map resulting from merging map `src` into `dst`.
// If a key in `src` already exists in `dst`, then the value in `src` replaces
// the value in `dst`.
// If a key in `src` has nil or empty slice payload value, then it deletes
// the key from `dst` if it exists.
// For example:
//
// dst := map[string]*commonpb.Payload{
// "key1": EncodeString("value1"),
// "key2": EncodeString("value2"),
// }
// src := map[string]*commonpb.Payload{
// "key1": EncodeString("newValue1"),
// "key2": nilPayload,
// }
// res := MergeMapOfPayload(dst, src)
//
// The resulting map `res` is:
//
// map[string]*commonpb.Payload{
// "key1": EncodeString("newValue1"),
// }
func MergeMapOfPayload(
dst map[string]*commonpb.Payload,
src map[string]*commonpb.Payload,
) map[string]*commonpb.Payload {
if src == nil {
return maps.Clone(dst)
}
res := util.CloneMapNonNil(dst)
for k, v := range src {
if isEqual(v, nilPayload) || isEqual(v, emptySlicePayload) {
delete(res, k)
} else {
res[k] = v
}
}
return res
}
// isEqual returns true if both have the same encoding and data.
// It does not take additional metadata into consideration.
// Note that data equality it's not the same as semantic equality, ie.,
// `[]` and `[ ]` are semantically the same, but different not data-wise.
// Only use if you know that the data is encoded the same way.
func isEqual(a, b *commonpb.Payload) bool {
aEnc := a.GetMetadata()[converter.MetadataEncoding]
bEnc := a.GetMetadata()[converter.MetadataEncoding]
return bytes.Equal(aEnc, bEnc) && bytes.Equal(a.GetData(), b.GetData())
}