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
41 changes: 41 additions & 0 deletions docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,47 @@ given subfield path.

[Example](examples/sort_repeated_fields_by_subfield.OUT.textproto)

## FieldSortOrder

`# txtpbfmt: field_sort_order=[NodeName]:[field1] [field2] ...`

Specify a custom sort order for fields within a specific node type. `NodeName`
is the name of the message node. Use `__ROOT__` to specify the sort order for
top-level fields in the file. Fields not listed will bubble to the top (unless
`require_field_sort_order_to_match_all_fields_in_node` is set).

### Example

```textproto
# txtpbfmt: field_sort_order=MyNode:foo bar baz

MyNode {
bar: 1
foo: 2
baz: 3
}
```

After formatting:

```textproto
# txtpbfmt: field_sort_order=MyNode:foo bar baz

MyNode {
foo: 2
bar: 1
baz: 3
}
```

## RequireFieldSortOrderToMatchAllFieldsInNode

`# txtpbfmt: require_field_sort_order_to_match_all_fields_in_node`

Enforce that all fields present in a node must be specified in the
`field_sort_order` for that node. If a field is found that is not in the sort
order, formatting will fail with an error.

## ReverseSort

`# txtpbfmt: reverse_sort`
Expand Down
13 changes: 13 additions & 0 deletions impl/impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,10 +221,23 @@ func addToConfig(metaComment string, c *config.Config) error {
c.Disable = true
case "expand_all_children":
c.ExpandAllChildren = true
case "field_sort_order":
if !hasEqualSign {
return fmt.Errorf("format should be %s=<string>, got: %s", key, metaComment)
}
parts := strings.SplitN(val, ":", 2)
if len(parts) != 2 {
return fmt.Errorf("format should be %s=NodeName:field1 field2..., got: %s", key, metaComment)
}
nodeName := strings.TrimSpace(parts[0])
fields := strings.Fields(parts[1])
c.AddFieldSortOrder(nodeName, fields...)
case "preserve_angle_brackets":
c.PreserveAngleBrackets = true
case "remove_duplicate_values_for_repeated_fields":
c.RemoveDuplicateValuesForRepeatedFields = true
case "require_field_sort_order_to_match_all_fields_in_node":
c.RequireFieldSortOrderToMatchAllFieldsInNode = true
case "skip_all_colons":
c.SkipAllColons = true
case "smartquotes":
Expand Down
34 changes: 34 additions & 0 deletions parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ func TestError(t *testing.T) {
}, {
in: `# txtpbfmt: off`,
err: "unterminated txtpbfmt off",
}, {
in: `# txtpbfmt: field_sort_order=MyNode:foo, require_field_sort_order_to_match_all_fields_in_node
MyNode {
foo: 1
bar: 2
}
`,
err: "fields parsed that were not specified in the parser.AddFieldSortOrder() call",
}}
for _, input := range inputs {
out, err := Format([]byte(input.in))
Expand Down Expand Up @@ -1163,6 +1171,32 @@ presubmit: {
id: 2
}
}
`}, {
name: "field_sort_order",
in: `# txtpbfmt: field_sort_order=MyNode:foo bar baz
MyNode {
bar: 1
foo: 2
baz: 3
}
`,
out: `# txtpbfmt: field_sort_order=MyNode:foo bar baz
MyNode {
foo: 2
bar: 1
baz: 3
}
`}, {
name: "field_sort_order for root node",
in: `# txtpbfmt: field_sort_order=__ROOT__:foo bar baz
bar: 1
foo: 2
baz: 3
`,
out: `foo: 2
# txtpbfmt: field_sort_order=__ROOT__:foo bar baz
bar: 1
baz: 3
`}, {
name: "sort by deeper subfield path",
in: `# txtpbfmt: sort_repeated_fields_by_subfield=test.metadata.identifiers.id
Expand Down
Loading