go.temporal.io/server/common/codec/jsonpb.go

147 LOC · 66 covered · 81 uncovered · 19 ranges · 787 concepts · 10 introducers · 412 tests

File neighbourhood

The centred file is linked to every concept that introduces one of its ranges, every test that runs code from the file, and the gray connector concepts standing between those tests and the file's own introducer concepts. Undirected links join concepts to every file where they introduce source and concepts to the tests they introduce; arrows show specialization between the displayed concepts and bridge only concepts omitted from this view. Concept colors match the source ranges below; connector concepts have no source color and are shown in gray.

Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the file

In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the related-file, concept, and source links on this page.

Graph controls are ready.

Interactive rendering requires JavaScript and WebGL. Use the related-file, concept, and source links on this page while the interactive map is unavailable.

1 package codec
2
3 import (
4 "bytes"
5 "encoding/json"
6 "fmt"
7
8 historypb "go.temporal.io/api/history/v1"
9 "go.temporal.io/api/temporalproto"
10 "google.golang.org/protobuf/encoding/protojson"
11 "google.golang.org/protobuf/proto"
12 )
13
14 type (
15 // JSONPBEncoder is JSON encoder/decoder for protobuf structs and slices of protobuf structs.
16 // This is an wrapper on top of jsonpb.Marshaler which supports not only single object serialization
17 // but also slices of concrete objects.
18 JSONPBEncoder struct {
19 marshaler protojson.MarshalOptions
20 unmarshaler temporalproto.CustomJSONUnmarshalOptions
21 }
22 )
23
24 // NewJSONPBEncoder creates a new JSONPBEncoder.
25 > func NewJSONPBEncoder() JSONPBEncoder { jsonpb.go ×1
26 > return JSONPBEncoder{}
27 > }
28
29 // NewJSONPBIndentEncoder creates a new JSONPBEncoder with indent.
30 > func NewJSONPBIndentEncoder(indent string) JSONPBEncoder { util.go ×3
31 > return JSONPBEncoder{
32 > marshaler: protojson.MarshalOptions{
33 > Indent: indent,
34 > },
35 > }
36 > }
37
38 // Encode protobuf struct to bytes.
39 > func (e JSONPBEncoder) Encode(pb proto.Message) ([]byte, error) { jsonpb.go ×1
40 > return e.marshaler.Marshal(pb)
41 > }
42
43 // Decode bytes to protobuf struct.
44 > func (e JSONPBEncoder) Decode(data []byte, pb proto.Message) error { jsonpb.go ×1
45 > return e.unmarshaler.Unmarshal(data, pb)
46 > }
47
48 // Encode HistoryEvent slice to bytes.
49 > func (e *JSONPBEncoder) EncodeHistoryEvents(historyEvents []*historypb.HistoryEvent) ([]byte, error) { proto_decoder.go ×19
50 > return e.encodeSlice(
51 > len(historyEvents),
52 > func(i int) proto.Message { return historyEvents[i] })
53 }
54
55 // Encode History slice to bytes.
56 > func (e *JSONPBEncoder) EncodeHistories(histories []*historypb.History) ([]byte, error) { jsonpb.go ×1
57 > return e.encodeSlice(
58 > len(histories),
59 > func(i int) proto.Message { return histories[i] })
60 }
61
62 // Decode HistoryEvent slice from bytes.
63 func (e *JSONPBEncoder) DecodeHistoryEvents(data []byte) ([]*historypb.HistoryEvent, error) {
64 var historyEvents []*historypb.HistoryEvent
65 err := e.DecodeSlice(
66 data,
67 func() proto.Message {
68 historyEvent := &historypb.HistoryEvent{}
69 historyEvents = append(historyEvents, historyEvent)
70 return historyEvent
71 })
72 return historyEvents, err
73 }
74
75 // Decode History slice from bytes.
76 > func (e *JSONPBEncoder) DecodeHistories(data []byte) ([]*historypb.History, error) { jsonpb.go ×8
77 > var histories []*historypb.History
78 > err := e.DecodeSlice(
79 > data,
80 > func() proto.Message {
81 > history := &historypb.History{}
82 > histories = append(histories, history)
83 > return history
84 > })
85
86 > return histories, err jsonpb.go ×8
87 }
88
89 // Due to the lack of generics in go
90 // this function accepts callback which should return particular item by it index.
91 func (e *JSONPBEncoder) encodeSlice(
92 len int,
93 item func(i int) proto.Message,
94 > ) ([]byte, error) { jsonpb.go ×2
95 > var buf bytes.Buffer
96 > buf.WriteString("[")
97 > for i := range len {
98 > pb := item(i) jsonpb.go ×2
99 > bs, err := e.marshaler.Marshal(pb)
100 > if err != nil {
101 return nil, err
102 }
103 > buf.Write(bs) jsonpb.go ×2
104 >
105 > if i < len-1 {
106 > buf.WriteString(",") jsonpb.go ×1
107 > }
108 }
109 > buf.WriteString("]") jsonpb.go ×2
110 > return buf.Bytes(), nil
111 }
112
113 // constructor callback must create empty object, add it to result slice, and return it.
114 func (e *JSONPBEncoder) DecodeSlice(
115 data []byte,
116 > constructor func() proto.Message) error { jsonpb.go ×8
117 >
118 > dec := json.NewDecoder(bytes.NewReader(data))
119 >
120 > tok, err := dec.Token()
121 > if err != nil {
122 return err
123 }
124 > if delim, ok := tok.(json.Delim); !ok || delim != '[' { jsonpb.go ×8
125 return fmt.Errorf("invalid json: expected [ but found %v", tok)
126 }
127
128 // We need DiscardUnknown here as the history json may have been written by a
129 // different proto revision
130 > unmarshaller := temporalproto.CustomJSONUnmarshalOptions{ jsonpb.go ×8
131 > DiscardUnknown: true,
132 > }
133 >
134 > var buf json.RawMessage
135 > for dec.More() {
136 > if err := dec.Decode(&buf); err != nil {
137 return err
138 }
139 > pb := constructor() jsonpb.go ×8
140 > if err := unmarshaller.Unmarshal([]byte(buf), pb); err != nil {
141 return err
142 }
143 > buf = buf[:0] jsonpb.go ×8
144 }
145
146 > return nil jsonpb.go ×8
147 }