Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion experimental/fdp/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package fdp

import (
"bytes"
"math"
"slices"
"strconv"
Expand Down Expand Up @@ -577,7 +578,16 @@ func (g *generator) field(f ir.Member, fdp *descriptorpb.FieldDescriptorProto, s
fdp.DefaultValue = addr(strconv.FormatFloat(v, 'g', -1, 64))
}
} else if v, ok := d.AsString(); ok {
fdp.DefaultValue = addr(v)
if fdp.GetType() == descriptorpb.FieldDescriptorProto_TYPE_BYTES {
// For bytes fields, the default value needs to be escaped.
// Reference for default value encoding:
// https://protobuf.com/docs/descriptors#encoding-default-values
var buf bytes.Buffer
internal.WriteEscapedBytes(&buf, []byte(v))
fdp.DefaultValue = addr(buf.String())
} else {
fdp.DefaultValue = addr(v)
}
}

g.addSourceLocationWithSourcePathElements(
Expand Down
2 changes: 2 additions & 0 deletions experimental/ir/testdata/fields/default.proto
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ message M {
bool b2 = 32 [default = 1];
bool b3 = 33 [default = false];

bytes b = 40 [default = "\0\1\2\3\4\5\6\7fubar!"];

enum E {
E0 = 0;
E1 = 1;
Expand Down
6 changes: 6 additions & 0 deletions experimental/ir/testdata/fields/default.proto.fds.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,12 @@ file:
type: TYPE_BOOL
default_value: "false"
json_name: "b3"
- name: "b"
number: 40
label: LABEL_OPTIONAL
type: TYPE_BYTES
default_value: "\\000\\001\\002\\003\\004\\005\\006\\007fubar!"
json_name: "b"
- name: "e1"
number: 41
label: LABEL_OPTIONAL
Expand Down
18 changes: 9 additions & 9 deletions experimental/ir/testdata/fields/default.proto.stderr.txt
Original file line number Diff line number Diff line change
Expand Up @@ -109,40 +109,40 @@ warning: redundant custom default
making this option redundant

warning: redundant custom default
--> testdata/fields/default.proto:53:26
--> testdata/fields/default.proto:55:26
|
53 | E e3 = 43 [default = E0];
55 | E e3 = 43 [default = E0];
| ^^ this is the zero value for `buf.test.M.E`
|
= help: fields without a custom default will default to the zero value,
making this option redundant

error: expected singular scalar- or enum-typed field, found scalar type `int32`
--> testdata/fields/default.proto:55:14
--> testdata/fields/default.proto:57:14
|
55 | repeated int32 w1 = 101 [default = 1, default = 2];
57 | repeated int32 w1 = 101 [default = 1, default = 2];
| ^^^^^ ------- custom default specified here
|
= help: custom defaults are only for non-repeated fields that have a
non-message type

error: expected singular scalar- or enum-typed field, found message type
`buf.test.M`
--> testdata/fields/default.proto:56:5
--> testdata/fields/default.proto:58:5
|
56 | M w2 = 102 [default = {
58 | M w2 = 102 [default = {
| ^ ------- custom default specified here
|
= help: custom defaults are only for non-repeated fields that have a
non-message type

error: expected singular scalar- or enum-typed field, found message type
`buf.test.M`
--> testdata/fields/default.proto:60:5
--> testdata/fields/default.proto:62:5
|
60 | M w3 = 103 [
62 | M w3 = 103 [
| ^
61 | default.i1 = 42,
63 | default.i1 = 42,
| ------- custom default specified here
|
= help: custom defaults are only for non-repeated fields that have a
Expand Down
Loading