17
ptext "google.golang.org/protobuf/internal/encoding/text"
18
"google.golang.org/protobuf/internal/errors"
19
"google.golang.org/protobuf/reflect/protoreflect"
38
func Unmarshal(s string, k protoreflect.Kind, evs protoreflect.EnumValueDescriptors, f Format) (protoreflect.Value, protoreflect.EnumValueDescriptor, error) {
40
case protoreflect.BoolKind:
44
return protoreflect.ValueOfBool(true), nil, nil
46
return protoreflect.ValueOfBool(false), nil, nil
51
return protoreflect.ValueOfBool(true), nil, nil
53
return protoreflect.ValueOfBool(false), nil, nil
56
case protoreflect.EnumKind:
59
if n, err := strconv.ParseInt(s, 10, 32); err == nil {
60
if ev := evs.ByNumber(protoreflect.EnumNumber(n)); ev != nil {
61
return protoreflect.ValueOfEnum(ev.Number()), ev, nil
66
ev := evs.ByName(protoreflect.Name(s))
68
return protoreflect.ValueOfEnum(ev.Number()), ev, nil
71
case protoreflect.Int32Kind, protoreflect.Sint32Kind, protoreflect.Sfixed32Kind:
72
if v, err := strconv.ParseInt(s, 10, 32); err == nil {
73
return protoreflect.ValueOfInt32(int32(v)), nil, nil
75
case protoreflect.Int64Kind, protoreflect.Sint64Kind, protoreflect.Sfixed64Kind:
76
if v, err := strconv.ParseInt(s, 10, 64); err == nil {
77
return protoreflect.ValueOfInt64(int64(v)), nil, nil
79
case protoreflect.Uint32Kind, protoreflect.Fixed32Kind:
80
if v, err := strconv.ParseUint(s, 10, 32); err == nil {
81
return protoreflect.ValueOfUint32(uint32(v)), nil, nil
83
case protoreflect.Uint64Kind, protoreflect.Fixed64Kind:
84
if v, err := strconv.ParseUint(s, 10, 64); err == nil {
85
return protoreflect.ValueOfUint64(uint64(v)), nil, nil
87
case protoreflect.FloatKind, protoreflect.DoubleKind:
98
v, err = strconv.ParseFloat(s, 64)
101
if k == protoreflect.FloatKind {
102
return protoreflect.ValueOfFloat32(float32(v)), nil, nil
104
return protoreflect.ValueOfFloat64(float64(v)), nil, nil
107
case protoreflect.StringKind:
109
return protoreflect.ValueOfString(s), nil, nil
110
case protoreflect.BytesKind:
111
if b, ok := unmarshalBytes(s); ok {
112
return protoreflect.ValueOfBytes(b), nil, nil
115
return protoreflect.Value{}, nil, errors.New("could not parse value for %v: %q", k, s)
121
func Marshal(v protoreflect.Value, ev protoreflect.EnumValueDescriptor, k protoreflect.Kind, f Format) (string, error) {
123
case protoreflect.BoolKind:
137
case protoreflect.EnumKind:
139
return strconv.FormatInt(int64(v.Enum()), 10), nil
141
return string(ev.Name()), nil
143
case protoreflect.Int32Kind, protoreflect.Sint32Kind, protoreflect.Sfixed32Kind, protoreflect.Int64Kind, protoreflect.Sint64Kind, protoreflect.Sfixed64Kind:
144
return strconv.FormatInt(v.Int(), 10), nil
145
case protoreflect.Uint32Kind, protoreflect.Fixed32Kind, protoreflect.Uint64Kind, protoreflect.Fixed64Kind:
146
return strconv.FormatUint(v.Uint(), 10), nil
147
case protoreflect.FloatKind, protoreflect.DoubleKind:
150
case math.IsInf(f, -1):
152
case math.IsInf(f, +1):
157
if k == protoreflect.FloatKind {
158
return strconv.FormatFloat(f, 'g', -1, 32), nil
160
return strconv.FormatFloat(f, 'g', -1, 64), nil
163
case protoreflect.StringKind:
165
return v.String(), nil
166
case protoreflect.BytesKind:
167
if s, ok := marshalBytes(v.Bytes()); ok {
171
return "", errors.New("could not format value for %v: %v", k, v)
175
func unmarshalBytes(s string) ([]byte, bool) {
178
v, err := ptext.UnmarshalString(`"` + s + `"`)
182
return []byte(v), true
185
// marshalBytes serializes bytes by using C escaping.
186
// To match the exact output of protoc, this is identical to the
187
// CEscape function in strutil.cc of the protoc source code.
188
func marshalBytes(b []byte) (string, bool) {
190
for _, c := range b {
193
s = append(s, `\n`...)
195
s = append(s, `\r`...)
197
s = append(s, `\t`...)
199
s = append(s, `\"`...)
201
s = append(s, `\'`...)
203
s = append(s, `\\`...)
205
if printableASCII := c >= 0x20 && c <= 0x7e; printableASCII {
208
s = append(s, fmt.Sprintf(`\%03o`, c)...)
212
return string(s), true