@cryptotaxi247 / kubo / commits / ba9d9f609

go-ipfs-config: fix: String method on the OptionalString (#153)

* fix: String method on the OptionalString * test(OptionalString): empty string is preserved Co-authored-by: Marcin Rataj <lidel@lidel.org>

Marten Seemann committed Oct 29, 2021 at 17:21 UTC ba9d9f609c972b807bc98bebc42a6a9ff44f1aa4
2 files changed +12 -7
config/types.go
+1 -1
@@ -360,7 +360,7 @@ func (p OptionalString) String() string {
360 if p.value == nil {
361 return "default"
362 }
363 - return fmt.Sprintf("%d", p.value)
363 + return *p.value
364 }
365
366 var _ json.Unmarshaler = (*OptionalInteger)(nil)
config/types_test.go
+11 -6
@@ -407,11 +407,13 @@ func TestOptionalString(t *testing.T) {
407 t.Fatal("should be the default")
408 }
409 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)
414 }
412 -
415 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)
417 }
418
419 var filledStr OptionalString
@@ -420,17 +422,20 @@ func TestOptionalString(t *testing.T) {
422 t.Fatal("should not be the default")
423 }
424 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)
429 }
425 -
430 filledStr = OptionalString{value: makeStringPointer("")}
431 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)
433 }
434
435 for jsonStr, goValue := range map[string]OptionalString{
436 "null": {},
437 "\"0\"": {value: makeStringPointer("0")},
438 + "\"\"": {value: makeStringPointer("")},
439 `"1"`: {value: makeStringPointer("1")},
440 `"-1"`: {value: makeStringPointer("-1")},
441 `"qwerty"`: {value: makeStringPointer("qwerty")},