Skip to content

Commit

Permalink
fix: String method on the OptionalString (ipfs#153)
Browse files Browse the repository at this point in the history
* fix: String method on the OptionalString
* test(OptionalString): empty string is preserved

Co-authored-by: Marcin Rataj <lidel@lidel.org>
  • Loading branch information
marten-seemann and lidel committed Oct 29, 2021
1 parent 7f2a2d7 commit 7a9d248
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
2 changes: 1 addition & 1 deletion config/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ func (p OptionalString) String() string {
if p.value == nil {
return "default"
}
return fmt.Sprintf("%d", p.value)
return *p.value
}

var _ json.Unmarshaler = (*OptionalInteger)(nil)
Expand Down
17 changes: 11 additions & 6 deletions config/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -407,11 +407,13 @@ func TestOptionalString(t *testing.T) {
t.Fatal("should be the default")
}
if val := defaultOptionalString.WithDefault(""); val != "" {
t.Errorf("optional integer should have been empty, got %s", val)
t.Errorf("optional string should have been empty, got %s", val)
}
if val := defaultOptionalString.String(); val != "default" {
t.Fatalf("default optional string should be the 'default' string, got %s", val)
}

if val := defaultOptionalString.WithDefault("foo"); val != "foo" {
t.Errorf("optional integer should have been foo, got %s", val)
t.Errorf("optional string should have been foo, got %s", val)
}

var filledStr OptionalString
Expand All @@ -420,17 +422,20 @@ func TestOptionalString(t *testing.T) {
t.Fatal("should not be the default")
}
if val := filledStr.WithDefault("bar"); val != "foo" {
t.Errorf("optional integer should have been foo, got %s", val)
t.Errorf("optional string should have been foo, got %s", val)
}
if val := filledStr.String(); val != "foo" {
t.Fatalf("optional string should have been foo, got %s", val)
}

filledStr = OptionalString{value: makeStringPointer("")}
if val := filledStr.WithDefault("foo"); val != "" {
t.Errorf("optional integer should have been 0, got %s", val)
t.Errorf("optional string should have been 0, got %s", val)
}

for jsonStr, goValue := range map[string]OptionalString{
"null": {},
"\"0\"": {value: makeStringPointer("0")},
"\"\"": {value: makeStringPointer("")},
`"1"`: {value: makeStringPointer("1")},
`"-1"`: {value: makeStringPointer("-1")},
`"qwerty"`: {value: makeStringPointer("qwerty")},
Expand Down

0 comments on commit 7a9d248

Please sign in to comment.