diff --git a/docs/config.md b/docs/config.md index c45568f..17adfd7 100644 --- a/docs/config.md +++ b/docs/config.md @@ -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` diff --git a/impl/impl.go b/impl/impl.go index 22422c9..22726db 100644 --- a/impl/impl.go +++ b/impl/impl.go @@ -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=, 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": diff --git a/parser/parser_test.go b/parser/parser_test.go index e8eff4e..f1f1946 100644 --- a/parser/parser_test.go +++ b/parser/parser_test.go @@ -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)) @@ -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