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

Commit 7aa6b00

Browse files
fix: String method on the OptionalString (#153)
* fix: String method on the OptionalString * test(OptionalString): empty string is preserved Co-authored-by: Marcin Rataj <[email protected]>
1 parent e4a3935 commit 7aa6b00

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

types.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ func (p OptionalString) String() string {
360360
if p.value == nil {
361361
return "default"
362362
}
363-
return fmt.Sprintf("%d", p.value)
363+
return *p.value
364364
}
365365

366366
var _ json.Unmarshaler = (*OptionalInteger)(nil)

types_test.go

+11-6
Original file line numberDiff line numberDiff line change
@@ -407,11 +407,13 @@ func TestOptionalString(t *testing.T) {
407407
t.Fatal("should be the default")
408408
}
409409
if val := defaultOptionalString.WithDefault(""); val != "" {
410-
t.Errorf("optional integer should have been empty, got %s", val)
410+
t.Errorf("optional string should have been empty, got %s", val)
411+
}
412+
if val := defaultOptionalString.String(); val != "default" {
413+
t.Fatalf("default optional string should be the 'default' string, got %s", val)
411414
}
412-
413415
if val := defaultOptionalString.WithDefault("foo"); val != "foo" {
414-
t.Errorf("optional integer should have been foo, got %s", val)
416+
t.Errorf("optional string should have been foo, got %s", val)
415417
}
416418

417419
var filledStr OptionalString
@@ -420,17 +422,20 @@ func TestOptionalString(t *testing.T) {
420422
t.Fatal("should not be the default")
421423
}
422424
if val := filledStr.WithDefault("bar"); val != "foo" {
423-
t.Errorf("optional integer should have been foo, got %s", val)
425+
t.Errorf("optional string should have been foo, got %s", val)
426+
}
427+
if val := filledStr.String(); val != "foo" {
428+
t.Fatalf("optional string should have been foo, got %s", val)
424429
}
425-
426430
filledStr = OptionalString{value: makeStringPointer("")}
427431
if val := filledStr.WithDefault("foo"); val != "" {
428-
t.Errorf("optional integer should have been 0, got %s", val)
432+
t.Errorf("optional string should have been 0, got %s", val)
429433
}
430434

431435
for jsonStr, goValue := range map[string]OptionalString{
432436
"null": {},
433437
"\"0\"": {value: makeStringPointer("0")},
438+
"\"\"": {value: makeStringPointer("")},
434439
`"1"`: {value: makeStringPointer("1")},
435440
`"-1"`: {value: makeStringPointer("-1")},
436441
`"qwerty"`: {value: makeStringPointer("qwerty")},

0 commit comments

Comments
 (0)