mirror of
https://github.com/hanzoai/tasks.git
synced 2026-11-03 03:56:04 +00:00
Full module path rebrand from Temporal to Hanzo Tasks: - go.mod module: github.com/hanzoai/tasks - All internal Go imports updated (1906 files) - cmd/server renamed to cmd/tasksd - Binary name: tasksd (was temporal-server) - Docker images: hanzoai/tasks (was temporalio/server) - README rewritten with Hanzo Tasks branding - LLM.md created with integration notes - Makefile, goreleaser, CI workflows updated - go.temporal.io/sdk and go.temporal.io/api imports preserved (separate repos) Fork of Temporal.io (MIT license).
93 lines
2.5 KiB
Go
93 lines
2.5 KiB
Go
package payload
|
|
|
|
import (
|
|
"bytes"
|
|
"maps"
|
|
|
|
commonpb "go.temporal.io/api/common/v1"
|
|
"go.temporal.io/sdk/converter"
|
|
"github.com/hanzoai/tasks/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 any) (*commonpb.Payload, error) {
|
|
return defaultDataConverter.ToPayload(value)
|
|
}
|
|
|
|
func Decode(p *commonpb.Payload, valuePtr any) 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())
|
|
}
|