Herbie Ong | 7b828bc | 2019-02-08 19:56:24 -0800 | [diff] [blame] | 1 | // Copyright 2019 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
Damien Neil | 5c5b531 | 2019-05-14 12:44:37 -0700 | [diff] [blame] | 5 | package protojson |
Herbie Ong | 7b828bc | 2019-02-08 19:56:24 -0800 | [diff] [blame] | 6 | |
| 7 | import ( |
| 8 | "encoding/base64" |
Herbie Ong | 0b0f403 | 2019-03-18 19:06:15 -0700 | [diff] [blame] | 9 | "fmt" |
Herbie Ong | 7b828bc | 2019-02-08 19:56:24 -0800 | [diff] [blame] | 10 | "sort" |
| 11 | |
Damien Neil | e89e624 | 2019-05-13 23:55:40 -0700 | [diff] [blame] | 12 | "google.golang.org/protobuf/internal/encoding/json" |
Joe Tsai | 5ae10aa | 2019-07-11 18:23:08 -0700 | [diff] [blame] | 13 | "google.golang.org/protobuf/internal/encoding/messageset" |
| 14 | "google.golang.org/protobuf/internal/errors" |
| 15 | "google.golang.org/protobuf/internal/flags" |
Damien Neil | e89e624 | 2019-05-13 23:55:40 -0700 | [diff] [blame] | 16 | "google.golang.org/protobuf/internal/pragma" |
| 17 | "google.golang.org/protobuf/proto" |
| 18 | pref "google.golang.org/protobuf/reflect/protoreflect" |
| 19 | "google.golang.org/protobuf/reflect/protoregistry" |
Herbie Ong | 7b828bc | 2019-02-08 19:56:24 -0800 | [diff] [blame] | 20 | ) |
| 21 | |
Joe Tsai | 74b1460 | 2020-01-06 15:44:09 -0800 | [diff] [blame] | 22 | const defaultIndent = " " |
| 23 | |
| 24 | // Format formats the message as a multiline string. |
| 25 | // This function is only intended for human consumption and ignores errors. |
| 26 | // Do not depend on the output being stable. It may change over time across |
| 27 | // different versions of the program. |
| 28 | func Format(m proto.Message) string { |
| 29 | return MarshalOptions{Multiline: true}.Format(m) |
| 30 | } |
| 31 | |
Herbie Ong | 7b828bc | 2019-02-08 19:56:24 -0800 | [diff] [blame] | 32 | // Marshal writes the given proto.Message in JSON format using default options. |
Herbie Ong | 9e356de | 2019-08-05 11:40:38 +0800 | [diff] [blame] | 33 | // Do not depend on the output being stable. It may change over time across |
Herbie Ong | 582ab3d | 2019-09-06 15:56:09 -0700 | [diff] [blame] | 34 | // different versions of the program. |
Herbie Ong | 7b828bc | 2019-02-08 19:56:24 -0800 | [diff] [blame] | 35 | func Marshal(m proto.Message) ([]byte, error) { |
| 36 | return MarshalOptions{}.Marshal(m) |
| 37 | } |
| 38 | |
| 39 | // MarshalOptions is a configurable JSON format marshaler. |
| 40 | type MarshalOptions struct { |
| 41 | pragma.NoUnkeyedLiterals |
| 42 | |
Herbie Ong | 329be5b | 2019-03-27 14:47:59 -0700 | [diff] [blame] | 43 | // AllowPartial allows messages that have missing required fields to marshal |
| 44 | // without returning an error. If AllowPartial is false (the default), |
| 45 | // Marshal will return error if there are any missing required fields. |
| 46 | AllowPartial bool |
| 47 | |
Herbie Ong | 956cd6d | 2019-09-06 15:17:22 -0700 | [diff] [blame] | 48 | // UseProtoNames uses proto field name instead of lowerCamelCase name in JSON |
| 49 | // field names. |
| 50 | UseProtoNames bool |
| 51 | |
Herbie Ong | 9111f3b | 2019-09-06 14:35:09 -0700 | [diff] [blame] | 52 | // UseEnumNumbers emits enum values as numbers. |
| 53 | UseEnumNumbers bool |
| 54 | |
Herbie Ong | 984e528 | 2019-09-06 00:29:48 -0700 | [diff] [blame] | 55 | // EmitUnpopulated specifies whether to emit unpopulated fields. It does not |
| 56 | // emit unpopulated oneof fields or unpopulated extension fields. |
| 57 | // The JSON value emitted for unpopulated fields are as follows: |
Herbie Ong | 956cd6d | 2019-09-06 15:17:22 -0700 | [diff] [blame] | 58 | // ╔═══════╤════════════════════════════╗ |
| 59 | // ║ JSON │ Protobuf field ║ |
| 60 | // ╠═══════╪════════════════════════════╣ |
| 61 | // ║ false │ proto3 boolean fields ║ |
| 62 | // ║ 0 │ proto3 numeric fields ║ |
| 63 | // ║ "" │ proto3 string/bytes fields ║ |
| 64 | // ║ null │ proto2 scalar fields ║ |
| 65 | // ║ null │ message fields ║ |
| 66 | // ║ [] │ list fields ║ |
| 67 | // ║ {} │ map fields ║ |
| 68 | // ╚═══════╧════════════════════════════╝ |
Herbie Ong | 984e528 | 2019-09-06 00:29:48 -0700 | [diff] [blame] | 69 | EmitUnpopulated bool |
| 70 | |
Joe Tsai | 74b1460 | 2020-01-06 15:44:09 -0800 | [diff] [blame] | 71 | // Multiline specifies whether the marshaler should format the output in |
| 72 | // indented-form with every textual element on a new line. |
| 73 | // If Indent is an empty string, then an arbitrary indent is chosen. |
| 74 | Multiline bool |
| 75 | |
| 76 | // Indent specifies the set of indentation characters to use in a multiline |
| 77 | // formatted output such that every entry is preceded by Indent and |
| 78 | // terminated by a newline. If non-empty, then Multiline is treated as true. |
| 79 | // Indent can only be composed of space or tab characters. |
Herbie Ong | 0b0f403 | 2019-03-18 19:06:15 -0700 | [diff] [blame] | 80 | Indent string |
| 81 | |
Joe Tsai | 1c28304 | 2019-05-14 14:28:19 -0700 | [diff] [blame] | 82 | // Resolver is used for looking up types when expanding google.protobuf.Any |
| 83 | // messages. If nil, this defaults to using protoregistry.GlobalTypes. |
| 84 | Resolver interface { |
Damien Neil | 95faac2 | 2019-06-19 10:03:37 -0700 | [diff] [blame] | 85 | protoregistry.ExtensionTypeResolver |
Joe Tsai | 1c28304 | 2019-05-14 14:28:19 -0700 | [diff] [blame] | 86 | protoregistry.MessageTypeResolver |
| 87 | } |
Herbie Ong | 7b828bc | 2019-02-08 19:56:24 -0800 | [diff] [blame] | 88 | } |
| 89 | |
Joe Tsai | 74b1460 | 2020-01-06 15:44:09 -0800 | [diff] [blame] | 90 | // Format formats the message as a string. |
| 91 | // This method is only intended for human consumption and ignores errors. |
| 92 | // Do not depend on the output being stable. It may change over time across |
| 93 | // different versions of the program. |
| 94 | func (o MarshalOptions) Format(m proto.Message) string { |
| 95 | if m == nil || !m.ProtoReflect().IsValid() { |
| 96 | return "<nil>" // invalid syntax, but okay since this is for debugging |
| 97 | } |
| 98 | o.AllowPartial = true |
| 99 | b, _ := o.Marshal(m) |
| 100 | return string(b) |
| 101 | } |
| 102 | |
Herbie Ong | 0b0f403 | 2019-03-18 19:06:15 -0700 | [diff] [blame] | 103 | // Marshal marshals the given proto.Message in the JSON format using options in |
Herbie Ong | 582ab3d | 2019-09-06 15:56:09 -0700 | [diff] [blame] | 104 | // MarshalOptions. Do not depend on the output being stable. It may change over |
| 105 | // time across different versions of the program. |
Herbie Ong | 7b828bc | 2019-02-08 19:56:24 -0800 | [diff] [blame] | 106 | func (o MarshalOptions) Marshal(m proto.Message) ([]byte, error) { |
Joe Tsai | 74b1460 | 2020-01-06 15:44:09 -0800 | [diff] [blame] | 107 | if o.Multiline && o.Indent == "" { |
| 108 | o.Indent = defaultIndent |
| 109 | } |
| 110 | if o.Resolver == nil { |
| 111 | o.Resolver = protoregistry.GlobalTypes |
| 112 | } |
| 113 | |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 114 | internalEnc, err := json.NewEncoder(o.Indent) |
Herbie Ong | 87608a7 | 2019-03-06 14:32:24 -0800 | [diff] [blame] | 115 | if err != nil { |
| 116 | return nil, err |
| 117 | } |
| 118 | |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 119 | enc := encoder{internalEnc, o} |
| 120 | if err := enc.marshalMessage(m.ProtoReflect()); err != nil { |
Herbie Ong | 7b828bc | 2019-02-08 19:56:24 -0800 | [diff] [blame] | 121 | return nil, err |
| 122 | } |
Damien Neil | 8c86fc5 | 2019-06-19 09:28:29 -0700 | [diff] [blame] | 123 | if o.AllowPartial { |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 124 | return enc.Bytes(), nil |
Damien Neil | 4686e23 | 2019-04-05 13:31:40 -0700 | [diff] [blame] | 125 | } |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 126 | return enc.Bytes(), proto.IsInitialized(m) |
| 127 | } |
| 128 | |
| 129 | type encoder struct { |
| 130 | *json.Encoder |
| 131 | opts MarshalOptions |
Herbie Ong | 87608a7 | 2019-03-06 14:32:24 -0800 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | // marshalMessage marshals the given protoreflect.Message. |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 135 | func (e encoder) marshalMessage(m pref.Message) error { |
Joe Tsai | 0fc49f8 | 2019-05-01 12:29:25 -0700 | [diff] [blame] | 136 | if isCustomType(m.Descriptor().FullName()) { |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 137 | return e.marshalCustomType(m) |
Herbie Ong | 0b0f403 | 2019-03-18 19:06:15 -0700 | [diff] [blame] | 138 | } |
| 139 | |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 140 | e.StartObject() |
| 141 | defer e.EndObject() |
| 142 | if err := e.marshalFields(m); err != nil { |
Herbie Ong | 0b0f403 | 2019-03-18 19:06:15 -0700 | [diff] [blame] | 143 | return err |
| 144 | } |
Herbie Ong | 87608a7 | 2019-03-06 14:32:24 -0800 | [diff] [blame] | 145 | |
Damien Neil | 8c86fc5 | 2019-06-19 09:28:29 -0700 | [diff] [blame] | 146 | return nil |
Herbie Ong | 0b0f403 | 2019-03-18 19:06:15 -0700 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | // marshalFields marshals the fields in the given protoreflect.Message. |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 150 | func (e encoder) marshalFields(m pref.Message) error { |
Joe Tsai | 5ae10aa | 2019-07-11 18:23:08 -0700 | [diff] [blame] | 151 | messageDesc := m.Descriptor() |
Joe Tsai | 1799d11 | 2019-08-08 13:31:59 -0700 | [diff] [blame] | 152 | if !flags.ProtoLegacy && messageset.IsMessageSet(messageDesc) { |
Joe Tsai | 5ae10aa | 2019-07-11 18:23:08 -0700 | [diff] [blame] | 153 | return errors.New("no support for proto1 MessageSets") |
| 154 | } |
| 155 | |
Herbie Ong | f83d5bb | 2019-03-14 00:01:27 -0700 | [diff] [blame] | 156 | // Marshal out known fields. |
Joe Tsai | 5ae10aa | 2019-07-11 18:23:08 -0700 | [diff] [blame] | 157 | fieldDescs := messageDesc.Fields() |
Joe Tsai | fc5f8c3 | 2019-09-17 22:32:53 -0700 | [diff] [blame] | 158 | for i := 0; i < fieldDescs.Len(); { |
Herbie Ong | 7b828bc | 2019-02-08 19:56:24 -0800 | [diff] [blame] | 159 | fd := fieldDescs.Get(i) |
Joe Tsai | fc5f8c3 | 2019-09-17 22:32:53 -0700 | [diff] [blame] | 160 | if od := fd.ContainingOneof(); od != nil { |
| 161 | fd = m.WhichOneof(od) |
| 162 | i += od.Fields().Len() |
| 163 | if fd == nil { |
| 164 | continue // unpopulated oneofs are not affected by EmitUnpopulated |
| 165 | } |
| 166 | } else { |
| 167 | i++ |
| 168 | } |
| 169 | |
Herbie Ong | 984e528 | 2019-09-06 00:29:48 -0700 | [diff] [blame] | 170 | val := m.Get(fd) |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame] | 171 | if !m.Has(fd) { |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 172 | if !e.opts.EmitUnpopulated { |
Herbie Ong | 984e528 | 2019-09-06 00:29:48 -0700 | [diff] [blame] | 173 | continue |
| 174 | } |
| 175 | isProto2Scalar := fd.Syntax() == pref.Proto2 && fd.Default().IsValid() |
| 176 | isSingularMessage := fd.Cardinality() != pref.Repeated && fd.Message() != nil |
| 177 | if isProto2Scalar || isSingularMessage { |
| 178 | // Use invalid value to emit null. |
| 179 | val = pref.Value{} |
| 180 | } |
Herbie Ong | 7b828bc | 2019-02-08 19:56:24 -0800 | [diff] [blame] | 181 | } |
| 182 | |
Herbie Ong | 87608a7 | 2019-03-06 14:32:24 -0800 | [diff] [blame] | 183 | name := fd.JSONName() |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 184 | if e.opts.UseProtoNames { |
Herbie Ong | 956cd6d | 2019-09-06 15:17:22 -0700 | [diff] [blame] | 185 | name = string(fd.Name()) |
| 186 | // Use type name for group field name. |
| 187 | if fd.Kind() == pref.GroupKind { |
| 188 | name = string(fd.Message().Name()) |
| 189 | } |
| 190 | } |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 191 | if err := e.WriteName(name); err != nil { |
Herbie Ong | 87608a7 | 2019-03-06 14:32:24 -0800 | [diff] [blame] | 192 | return err |
| 193 | } |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 194 | if err := e.marshalValue(val, fd); err != nil { |
Herbie Ong | 87608a7 | 2019-03-06 14:32:24 -0800 | [diff] [blame] | 195 | return err |
Herbie Ong | 7b828bc | 2019-02-08 19:56:24 -0800 | [diff] [blame] | 196 | } |
| 197 | } |
Herbie Ong | f83d5bb | 2019-03-14 00:01:27 -0700 | [diff] [blame] | 198 | |
| 199 | // Marshal out extensions. |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 200 | if err := e.marshalExtensions(m); err != nil { |
Herbie Ong | f83d5bb | 2019-03-14 00:01:27 -0700 | [diff] [blame] | 201 | return err |
| 202 | } |
Damien Neil | 8c86fc5 | 2019-06-19 09:28:29 -0700 | [diff] [blame] | 203 | return nil |
Herbie Ong | 7b828bc | 2019-02-08 19:56:24 -0800 | [diff] [blame] | 204 | } |
| 205 | |
Herbie Ong | 87608a7 | 2019-03-06 14:32:24 -0800 | [diff] [blame] | 206 | // marshalValue marshals the given protoreflect.Value. |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 207 | func (e encoder) marshalValue(val pref.Value, fd pref.FieldDescriptor) error { |
Joe Tsai | ac31a35 | 2019-05-13 14:32:56 -0700 | [diff] [blame] | 208 | switch { |
| 209 | case fd.IsList(): |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 210 | return e.marshalList(val.List(), fd) |
Joe Tsai | ac31a35 | 2019-05-13 14:32:56 -0700 | [diff] [blame] | 211 | case fd.IsMap(): |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 212 | return e.marshalMap(val.Map(), fd) |
Joe Tsai | ac31a35 | 2019-05-13 14:32:56 -0700 | [diff] [blame] | 213 | default: |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 214 | return e.marshalSingular(val, fd) |
Herbie Ong | 7b828bc | 2019-02-08 19:56:24 -0800 | [diff] [blame] | 215 | } |
Herbie Ong | 7b828bc | 2019-02-08 19:56:24 -0800 | [diff] [blame] | 216 | } |
| 217 | |
Herbie Ong | 87608a7 | 2019-03-06 14:32:24 -0800 | [diff] [blame] | 218 | // marshalSingular marshals the given non-repeated field value. This includes |
| 219 | // all scalar types, enums, messages, and groups. |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 220 | func (e encoder) marshalSingular(val pref.Value, fd pref.FieldDescriptor) error { |
Herbie Ong | 984e528 | 2019-09-06 00:29:48 -0700 | [diff] [blame] | 221 | if !val.IsValid() { |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 222 | e.WriteNull() |
Herbie Ong | 984e528 | 2019-09-06 00:29:48 -0700 | [diff] [blame] | 223 | return nil |
| 224 | } |
| 225 | |
Herbie Ong | 87608a7 | 2019-03-06 14:32:24 -0800 | [diff] [blame] | 226 | switch kind := fd.Kind(); kind { |
| 227 | case pref.BoolKind: |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 228 | e.WriteBool(val.Bool()) |
Herbie Ong | 87608a7 | 2019-03-06 14:32:24 -0800 | [diff] [blame] | 229 | |
| 230 | case pref.StringKind: |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 231 | if e.WriteString(val.String()) != nil { |
| 232 | return errors.InvalidUTF8(string(fd.FullName())) |
Herbie Ong | 87608a7 | 2019-03-06 14:32:24 -0800 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | case pref.Int32Kind, pref.Sint32Kind, pref.Sfixed32Kind: |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 236 | e.WriteInt(val.Int()) |
Herbie Ong | 87608a7 | 2019-03-06 14:32:24 -0800 | [diff] [blame] | 237 | |
| 238 | case pref.Uint32Kind, pref.Fixed32Kind: |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 239 | e.WriteUint(val.Uint()) |
Herbie Ong | 7b828bc | 2019-02-08 19:56:24 -0800 | [diff] [blame] | 240 | |
| 241 | case pref.Int64Kind, pref.Sint64Kind, pref.Uint64Kind, |
| 242 | pref.Sfixed64Kind, pref.Fixed64Kind: |
Herbie Ong | 87608a7 | 2019-03-06 14:32:24 -0800 | [diff] [blame] | 243 | // 64-bit integers are written out as JSON string. |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 244 | e.WriteString(val.String()) |
Herbie Ong | 7b828bc | 2019-02-08 19:56:24 -0800 | [diff] [blame] | 245 | |
Herbie Ong | 87608a7 | 2019-03-06 14:32:24 -0800 | [diff] [blame] | 246 | case pref.FloatKind: |
| 247 | // Encoder.WriteFloat handles the special numbers NaN and infinites. |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 248 | e.WriteFloat(val.Float(), 32) |
Herbie Ong | 87608a7 | 2019-03-06 14:32:24 -0800 | [diff] [blame] | 249 | |
| 250 | case pref.DoubleKind: |
| 251 | // Encoder.WriteFloat handles the special numbers NaN and infinites. |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 252 | e.WriteFloat(val.Float(), 64) |
Herbie Ong | 7b828bc | 2019-02-08 19:56:24 -0800 | [diff] [blame] | 253 | |
| 254 | case pref.BytesKind: |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 255 | e.WriteString(base64.StdEncoding.EncodeToString(val.Bytes())) |
Herbie Ong | 7b828bc | 2019-02-08 19:56:24 -0800 | [diff] [blame] | 256 | |
| 257 | case pref.EnumKind: |
Joe Tsai | d24bc72 | 2019-04-15 23:39:09 -0700 | [diff] [blame] | 258 | if fd.Enum().FullName() == "google.protobuf.NullValue" { |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 259 | e.WriteNull() |
Herbie Ong | 87608a7 | 2019-03-06 14:32:24 -0800 | [diff] [blame] | 260 | } else { |
Herbie Ong | 9111f3b | 2019-09-06 14:35:09 -0700 | [diff] [blame] | 261 | desc := fd.Enum().Values().ByNumber(val.Enum()) |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 262 | if e.opts.UseEnumNumbers || desc == nil { |
| 263 | e.WriteInt(int64(val.Enum())) |
Herbie Ong | 9111f3b | 2019-09-06 14:35:09 -0700 | [diff] [blame] | 264 | } else { |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 265 | e.WriteString(string(desc.Name())) |
Herbie Ong | 9111f3b | 2019-09-06 14:35:09 -0700 | [diff] [blame] | 266 | } |
Herbie Ong | 7b828bc | 2019-02-08 19:56:24 -0800 | [diff] [blame] | 267 | } |
Herbie Ong | 7b828bc | 2019-02-08 19:56:24 -0800 | [diff] [blame] | 268 | |
| 269 | case pref.MessageKind, pref.GroupKind: |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 270 | if err := e.marshalMessage(val.Message()); err != nil { |
Herbie Ong | 87608a7 | 2019-03-06 14:32:24 -0800 | [diff] [blame] | 271 | return err |
| 272 | } |
Herbie Ong | 7b828bc | 2019-02-08 19:56:24 -0800 | [diff] [blame] | 273 | |
Herbie Ong | 87608a7 | 2019-03-06 14:32:24 -0800 | [diff] [blame] | 274 | default: |
Herbie Ong | 0b0f403 | 2019-03-18 19:06:15 -0700 | [diff] [blame] | 275 | panic(fmt.Sprintf("%v has unknown kind: %v", fd.FullName(), kind)) |
Herbie Ong | 87608a7 | 2019-03-06 14:32:24 -0800 | [diff] [blame] | 276 | } |
Damien Neil | 8c86fc5 | 2019-06-19 09:28:29 -0700 | [diff] [blame] | 277 | return nil |
Herbie Ong | 7b828bc | 2019-02-08 19:56:24 -0800 | [diff] [blame] | 278 | } |
| 279 | |
Herbie Ong | 87608a7 | 2019-03-06 14:32:24 -0800 | [diff] [blame] | 280 | // marshalList marshals the given protoreflect.List. |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 281 | func (e encoder) marshalList(list pref.List, fd pref.FieldDescriptor) error { |
| 282 | e.StartArray() |
| 283 | defer e.EndArray() |
Herbie Ong | 87608a7 | 2019-03-06 14:32:24 -0800 | [diff] [blame] | 284 | |
Herbie Ong | 87608a7 | 2019-03-06 14:32:24 -0800 | [diff] [blame] | 285 | for i := 0; i < list.Len(); i++ { |
Herbie Ong | 7b828bc | 2019-02-08 19:56:24 -0800 | [diff] [blame] | 286 | item := list.Get(i) |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 287 | if err := e.marshalSingular(item, fd); err != nil { |
Herbie Ong | 87608a7 | 2019-03-06 14:32:24 -0800 | [diff] [blame] | 288 | return err |
Herbie Ong | 7b828bc | 2019-02-08 19:56:24 -0800 | [diff] [blame] | 289 | } |
Herbie Ong | 7b828bc | 2019-02-08 19:56:24 -0800 | [diff] [blame] | 290 | } |
Damien Neil | 8c86fc5 | 2019-06-19 09:28:29 -0700 | [diff] [blame] | 291 | return nil |
Herbie Ong | 7b828bc | 2019-02-08 19:56:24 -0800 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | type mapEntry struct { |
| 295 | key pref.MapKey |
| 296 | value pref.Value |
| 297 | } |
| 298 | |
Herbie Ong | 87608a7 | 2019-03-06 14:32:24 -0800 | [diff] [blame] | 299 | // marshalMap marshals given protoreflect.Map. |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 300 | func (e encoder) marshalMap(mmap pref.Map, fd pref.FieldDescriptor) error { |
| 301 | e.StartObject() |
| 302 | defer e.EndObject() |
Herbie Ong | 87608a7 | 2019-03-06 14:32:24 -0800 | [diff] [blame] | 303 | |
Herbie Ong | 7b828bc | 2019-02-08 19:56:24 -0800 | [diff] [blame] | 304 | // Get a sorted list based on keyType first. |
| 305 | entries := make([]mapEntry, 0, mmap.Len()) |
| 306 | mmap.Range(func(key pref.MapKey, val pref.Value) bool { |
| 307 | entries = append(entries, mapEntry{key: key, value: val}) |
| 308 | return true |
| 309 | }) |
Joe Tsai | ac31a35 | 2019-05-13 14:32:56 -0700 | [diff] [blame] | 310 | sortMap(fd.MapKey().Kind(), entries) |
Herbie Ong | 7b828bc | 2019-02-08 19:56:24 -0800 | [diff] [blame] | 311 | |
Herbie Ong | 87608a7 | 2019-03-06 14:32:24 -0800 | [diff] [blame] | 312 | // Write out sorted list. |
Herbie Ong | 7b828bc | 2019-02-08 19:56:24 -0800 | [diff] [blame] | 313 | for _, entry := range entries { |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 314 | if err := e.WriteName(entry.key.String()); err != nil { |
Herbie Ong | 87608a7 | 2019-03-06 14:32:24 -0800 | [diff] [blame] | 315 | return err |
Herbie Ong | 7b828bc | 2019-02-08 19:56:24 -0800 | [diff] [blame] | 316 | } |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 317 | if err := e.marshalSingular(entry.value, fd.MapValue()); err != nil { |
Herbie Ong | 87608a7 | 2019-03-06 14:32:24 -0800 | [diff] [blame] | 318 | return err |
| 319 | } |
Herbie Ong | 7b828bc | 2019-02-08 19:56:24 -0800 | [diff] [blame] | 320 | } |
Damien Neil | 8c86fc5 | 2019-06-19 09:28:29 -0700 | [diff] [blame] | 321 | return nil |
Herbie Ong | 7b828bc | 2019-02-08 19:56:24 -0800 | [diff] [blame] | 322 | } |
| 323 | |
Herbie Ong | f83d5bb | 2019-03-14 00:01:27 -0700 | [diff] [blame] | 324 | // sortMap orders list based on value of key field for deterministic ordering. |
Herbie Ong | 7b828bc | 2019-02-08 19:56:24 -0800 | [diff] [blame] | 325 | func sortMap(keyKind pref.Kind, values []mapEntry) { |
Herbie Ong | f83d5bb | 2019-03-14 00:01:27 -0700 | [diff] [blame] | 326 | sort.Slice(values, func(i, j int) bool { |
| 327 | switch keyKind { |
| 328 | case pref.Int32Kind, pref.Sint32Kind, pref.Sfixed32Kind, |
| 329 | pref.Int64Kind, pref.Sint64Kind, pref.Sfixed64Kind: |
Herbie Ong | 7b828bc | 2019-02-08 19:56:24 -0800 | [diff] [blame] | 330 | return values[i].key.Int() < values[j].key.Int() |
Herbie Ong | f83d5bb | 2019-03-14 00:01:27 -0700 | [diff] [blame] | 331 | |
| 332 | case pref.Uint32Kind, pref.Fixed32Kind, |
| 333 | pref.Uint64Kind, pref.Fixed64Kind: |
Herbie Ong | 7b828bc | 2019-02-08 19:56:24 -0800 | [diff] [blame] | 334 | return values[i].key.Uint() < values[j].key.Uint() |
| 335 | } |
Herbie Ong | f83d5bb | 2019-03-14 00:01:27 -0700 | [diff] [blame] | 336 | return values[i].key.String() < values[j].key.String() |
| 337 | }) |
| 338 | } |
| 339 | |
| 340 | // marshalExtensions marshals extension fields. |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 341 | func (e encoder) marshalExtensions(m pref.Message) error { |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame] | 342 | type entry struct { |
| 343 | key string |
| 344 | value pref.Value |
| 345 | desc pref.FieldDescriptor |
Herbie Ong | 7b828bc | 2019-02-08 19:56:24 -0800 | [diff] [blame] | 346 | } |
Herbie Ong | f83d5bb | 2019-03-14 00:01:27 -0700 | [diff] [blame] | 347 | |
Herbie Ong | f83d5bb | 2019-03-14 00:01:27 -0700 | [diff] [blame] | 348 | // Get a sorted list based on field key first. |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame] | 349 | var entries []entry |
| 350 | m.Range(func(fd pref.FieldDescriptor, v pref.Value) bool { |
| 351 | if !fd.IsExtension() { |
| 352 | return true |
| 353 | } |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame] | 354 | |
Joe Tsai | 5ae10aa | 2019-07-11 18:23:08 -0700 | [diff] [blame] | 355 | // For MessageSet extensions, the name used is the parent message. |
Joe Tsai | d421150 | 2019-07-02 14:58:02 -0700 | [diff] [blame] | 356 | name := fd.FullName() |
Joe Tsai | 5ae10aa | 2019-07-11 18:23:08 -0700 | [diff] [blame] | 357 | if messageset.IsMessageSetExtension(fd) { |
| 358 | name = name.Parent() |
Herbie Ong | f83d5bb | 2019-03-14 00:01:27 -0700 | [diff] [blame] | 359 | } |
| 360 | |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame] | 361 | // Use [name] format for JSON field name. |
| 362 | entries = append(entries, entry{ |
| 363 | key: string(name), |
| 364 | value: v, |
| 365 | desc: fd, |
| 366 | }) |
Herbie Ong | f83d5bb | 2019-03-14 00:01:27 -0700 | [diff] [blame] | 367 | return true |
| 368 | }) |
| 369 | |
| 370 | // Sort extensions lexicographically. |
| 371 | sort.Slice(entries, func(i, j int) bool { |
| 372 | return entries[i].key < entries[j].key |
| 373 | }) |
| 374 | |
| 375 | // Write out sorted list. |
Herbie Ong | f83d5bb | 2019-03-14 00:01:27 -0700 | [diff] [blame] | 376 | for _, entry := range entries { |
| 377 | // JSON field name is the proto field name enclosed in [], similar to |
| 378 | // textproto. This is consistent with Go v1 lib. C++ lib v3.7.0 does not |
| 379 | // marshal out extension fields. |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 380 | if err := e.WriteName("[" + entry.key + "]"); err != nil { |
Herbie Ong | f83d5bb | 2019-03-14 00:01:27 -0700 | [diff] [blame] | 381 | return err |
| 382 | } |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 383 | if err := e.marshalValue(entry.value, entry.desc); err != nil { |
Herbie Ong | f83d5bb | 2019-03-14 00:01:27 -0700 | [diff] [blame] | 384 | return err |
| 385 | } |
| 386 | } |
Damien Neil | 8c86fc5 | 2019-06-19 09:28:29 -0700 | [diff] [blame] | 387 | return nil |
Herbie Ong | f83d5bb | 2019-03-14 00:01:27 -0700 | [diff] [blame] | 388 | } |