Skip to content
This repository has been archived by the owner on Mar 9, 2022. It is now read-only.

fix: String method on the OptionalString #153

Merged
merged 3 commits into from
Oct 29, 2021
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
2 changes: 1 addition & 1 deletion 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 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