Skip to content

Commit d131754

Browse files
authored
revert: revert omit nil and empty string slices (#3492)
Fields with empty slices (e.g. disabled_rustdoc_warnings: []) are meaningful to generator to signal differences from default. context: #3484 (comment) Added comment to call out this behavior. Test out locally, no diff shown: In the librarian repo `git checkout revert-3434-pr3` `go install ./cmd/librarian` `go install ./tool/cmd/migrate` Run migration and generation from the google-cloud-rust repo: `migrate .` `librarian generate common` Reverts #3434 Fix #3484
1 parent 76ffec3 commit d131754

3 files changed

Lines changed: 37 additions & 2 deletions

File tree

internal/config/language.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
package config
1616

17+
import "github.com/googleapis/librarian/internal/yaml"
18+
1719
// GoModule represents the Go-specific configuration for a library.
1820
type GoModule struct {
1921
DeleteGenerationOutputPaths []string `yaml:"delete_generation_output_paths,omitempty"`
@@ -46,8 +48,8 @@ type RustDefault struct {
4648
// Each module specifies what proto source to use, which template to apply,
4749
// and where to output the generated code.
4850
type RustModule struct {
49-
// DisabledRustdocWarnings is a list of rustdoc warnings to disable.
50-
DisabledRustdocWarnings []string `yaml:"disabled_rustdoc_warnings,omitempty"`
51+
// DisabledRustdocWarnings specifies rustdoc lints to disable. An empty slice explicitly enables all warnings.
52+
DisabledRustdocWarnings yaml.StringSlice `yaml:"disabled_rustdoc_warnings,omitempty"`
5153

5254
// DocumentationOverrides contains overrides for element documentation.
5355
DocumentationOverrides []RustDocumentationOverride `yaml:"documentation_overrides,omitempty"`

internal/yaml/yaml.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,23 @@ import (
2323
"gopkg.in/yaml.v3"
2424
)
2525

26+
// StringSlice is a custom slice of strings that allows for fine-grained control
27+
// over YAML marshaling when used with the 'omitempty' tag.
28+
//
29+
// By implementing the yaml.IsZeroer interface, it ensures that:
30+
// 1. A nil slice is considered "zero" and is omitted from the output.
31+
// 2. An empty but initialized slice (e.g., []string{}) is NOT considered "zero"
32+
// and is explicitly marshaled as an empty YAML sequence ([]).
33+
type StringSlice []string
34+
35+
// IsZero implements the yaml.IsZeroer interface, which determines whether a
36+
// field should be considered "empty" when the 'omitempty' struct tag is used.
37+
func (s StringSlice) IsZero() bool {
38+
// return true ONLY if nil (omit field)
39+
// return false if empty slice (keep field)
40+
return s == nil
41+
}
42+
2643
// Unmarshal parses YAML data into a value of type T.
2744
func Unmarshal[T any](data []byte) (*T, error) {
2845
var v T

internal/yaml/yaml_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,19 @@ func TestWriteError(t *testing.T) {
8787
t.Error("Write() expected error for invalid path")
8888
}
8989
}
90+
91+
func TestStringSlice_EmptySlice(t *testing.T) {
92+
strSlice := StringSlice{}
93+
got := strSlice.IsZero()
94+
if diff := cmp.Diff(false, got); diff != "" {
95+
t.Errorf("mismatch (-want +got):\n%s", diff)
96+
}
97+
}
98+
99+
func TestStringSlice_NilSlice(t *testing.T) {
100+
var strSlice StringSlice
101+
got := strSlice.IsZero()
102+
if diff := cmp.Diff(true, got); diff != "" {
103+
t.Errorf("mismatch (-want +got):\n%s", diff)
104+
}
105+
}

0 commit comments

Comments
 (0)