Files
tasks/service/frontend/protojson_marshaler.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

136 lines
2.9 KiB
Go

// This whole file exists because grpc-gateway's runtime.JSONPb doesn't support indentation
package frontend
import (
"encoding/json"
"io"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
commonpb "go.temporal.io/api/common/v1"
"go.temporal.io/api/temporalproto"
"google.golang.org/protobuf/proto"
)
var _ runtime.Marshaler = temporalProtoMarshaler{}
type temporalProtoMarshaler struct {
contentType string
mOpts temporalproto.CustomJSONMarshalOptions
uOpts temporalproto.CustomJSONUnmarshalOptions
}
type temporalProtoEncoder struct {
mOpts temporalproto.CustomJSONMarshalOptions
writer io.Writer
json *json.Encoder
}
type temporalProtoDecoder struct {
uOpts temporalproto.CustomJSONUnmarshalOptions
reader io.Reader
json *json.Decoder
}
func newTemporalProtoMarshaler(indent string, enablePayloadShorthand bool) (string, temporalProtoMarshaler) {
metadata := map[string]interface{}{}
if enablePayloadShorthand {
metadata[commonpb.EnablePayloadShorthandMetadataKey] = true
}
// Shorthand is enabled by default
contentType := runtime.MIMEWildcard
if enablePayloadShorthand {
if indent != "" {
contentType = "application/json+pretty"
}
} else {
if indent != "" {
contentType = "application/json+pretty+no-payload-shorthand"
} else {
contentType = "application/json+no-payload-shorthand"
}
}
return contentType, temporalProtoMarshaler{
contentType: contentType,
mOpts: temporalproto.CustomJSONMarshalOptions{
Indent: indent,
Metadata: metadata,
},
uOpts: temporalproto.CustomJSONUnmarshalOptions{
Metadata: metadata,
},
}
}
func (p temporalProtoMarshaler) Marshal(v any) ([]byte, error) {
if m, ok := v.(proto.Message); ok {
return p.mOpts.Marshal(m)
}
if p.mOpts.Indent != "" {
return json.MarshalIndent(v, "", p.mOpts.Indent)
}
return json.Marshal(v)
}
func (p temporalProtoMarshaler) Unmarshal(data []byte, v interface{}) error {
if m, ok := v.(proto.Message); ok {
return p.uOpts.Unmarshal(data, m)
}
return json.Unmarshal(data, v)
}
func (p temporalProtoMarshaler) NewDecoder(r io.Reader) runtime.Decoder {
return temporalProtoDecoder{
p.uOpts,
r,
json.NewDecoder(r),
}
}
func (p temporalProtoMarshaler) NewEncoder(w io.Writer) runtime.Encoder {
return temporalProtoEncoder{
p.mOpts,
w,
json.NewEncoder(w),
}
}
func (p temporalProtoMarshaler) ContentType(_ any) string {
return p.contentType
}
func (d temporalProtoDecoder) Decode(v any) error {
m, ok := v.(proto.Message)
if !ok {
return d.json.Decode(v)
}
var bs json.RawMessage
if err := d.json.Decode(&bs); err != nil {
return err
}
return d.uOpts.Unmarshal([]byte(bs), m)
}
func (e temporalProtoEncoder) Encode(v any) error {
m, ok := v.(proto.Message)
if !ok {
return e.json.Encode(v)
}
bs, err := e.mOpts.Marshal(m)
if err != nil {
return err
}
_, err = e.writer.Write(bs)
if err != nil {
return err
}
_, err = e.writer.Write([]byte{'\n'})
return err
}