Files
tasks/tools/tdbg/task_encoder.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

109 lines
4.9 KiB
Go

package tdbg
import (
"fmt"
"io"
commonpb "go.temporal.io/api/common/v1"
"go.temporal.io/server/common/persistence/serialization"
"go.temporal.io/server/service/history/tasks"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"
)
type (
// TaskBlobEncoder takes a blob for a given task category and encodes it to a human-readable format.
// Here's a breakdown of the relationship between all the related types needed to implement a custom encoder:
// - NewCliApp accepts a list of Option objects.
// - Option objects modify Params.
// - Params contain a TaskBlobEncoder.
// - TaskBlobEncoder is implemented by ProtoTaskBlobEncoder.
// - ProtoTaskBlobEncoder uses protojson to marshal [proto.Message] objects from a TaskBlobProtoDeserializer.
// - TaskBlobProtoDeserializer is implemented by the stock PredefinedTaskBlobDeserializer.
// - PredefinedTaskBlobDeserializer deserializes [commonpb.DataBlob] objects into [proto.Message] objects.
TaskBlobEncoder interface {
Encode(writer io.Writer, taskCategoryID int, blob *commonpb.DataBlob) error
}
// TaskBlobEncoderFn implements TaskBlobEncoder by calling a function.
TaskBlobEncoderFn func(writer io.Writer, taskCategoryID int, blob *commonpb.DataBlob) error
// TaskBlobProtoDeserializer is used to deserialize task blobs into proto messages. This makes it easier to create
// an encoder if your tasks are all backed by protos. We separate this from the encoder because we don't want the
// encoder to be tied to protos as the wire format.
TaskBlobProtoDeserializer interface {
Deserialize(taskCategoryID int, blob *commonpb.DataBlob) (proto.Message, error)
}
// ProtoTaskBlobEncoder is a TaskBlobEncoder that uses a TaskBlobProtoDeserializer to deserialize the blob into a
// proto message, and then uses protojson to marshal the proto message into a human-readable format.
ProtoTaskBlobEncoder struct {
deserializer TaskBlobProtoDeserializer
}
// PredefinedTaskBlobDeserializer is a TaskBlobProtoDeserializer that deserializes task blobs into the predefined
// task categories that are used by Temporal. If your server has custom categories, you'll want to build something
// on top of this.
PredefinedTaskBlobDeserializer struct{}
)
var (
jsonpbMarshaler = protojson.MarshalOptions{
UseEnumNumbers: false, // It's ok to use strings because this is for human consumption.
Indent: " ", // Indent with two spaces to pretty-print.
UseProtoNames: true, // The proto field names are more JSON-esque than the Go field names.
}
)
// NewProtoTaskBlobEncoder returns a TaskBlobEncoder that uses a TaskBlobProtoDeserializer to deserialize the blob.
func NewProtoTaskBlobEncoder(deserializer TaskBlobProtoDeserializer) *ProtoTaskBlobEncoder {
return &ProtoTaskBlobEncoder{
deserializer: deserializer,
}
}
// NewPredefinedTaskBlobDeserializer returns a TaskBlobProtoDeserializer that works for the stock task categories of the
// server. You need to extend this if you have custom task categories.
func NewPredefinedTaskBlobDeserializer() PredefinedTaskBlobDeserializer {
return PredefinedTaskBlobDeserializer{}
}
// Deserialize a task blob from one of the server's predefined task categories into a proto message.
func (d PredefinedTaskBlobDeserializer) Deserialize(categoryID int, blob *commonpb.DataBlob) (proto.Message, error) {
switch categoryID {
case tasks.CategoryIDTransfer:
return serialization.TransferTaskInfoFromBlob(blob.Data, blob.EncodingType.String())
case tasks.CategoryIDTimer:
return serialization.TimerTaskInfoFromBlob(blob.Data, blob.EncodingType.String())
case tasks.CategoryIDVisibility:
return serialization.VisibilityTaskInfoFromBlob(blob.Data, blob.EncodingType.String())
case tasks.CategoryIDReplication:
return serialization.ReplicationTaskInfoFromBlob(blob.Data, blob.EncodingType.String())
case tasks.CategoryIDArchival:
return serialization.ArchivalTaskInfoFromBlob(blob.Data, blob.EncodingType.String())
case tasks.CategoryIDOutbound:
return serialization.OutboundTaskInfoFromBlob(blob.Data, blob.EncodingType.String())
default:
return nil, fmt.Errorf("unsupported task category %v", categoryID)
}
}
// Encode a blob for a given task category to a human-readable format by deserializing the blob into a proto message and
// then pretty-printing it using protojson.
func (e *ProtoTaskBlobEncoder) Encode(writer io.Writer, categoryID int, blob *commonpb.DataBlob) error {
message, err := e.deserializer.Deserialize(categoryID, blob)
if err != nil {
return fmt.Errorf("failed to deserialize task blob: %w", err)
}
bs, err := jsonpbMarshaler.Marshal(message)
if err != nil {
return fmt.Errorf("failed to marshal task blob: %w", err)
}
_, err = writer.Write(bs)
if err != nil {
return fmt.Errorf("failed to write marshalled task blob: %w", err)
}
return nil
}
// Encode the task by calling the function.
func (e TaskBlobEncoderFn) Encode(writer io.Writer, taskCategoryID int, blob *commonpb.DataBlob) error {
return e(writer, taskCategoryID, blob)
}