Skip to content

Commit

Permalink
Merge pull request #3027 from andydotxyz/chrisbirster-new-string-with…
Browse files Browse the repository at this point in the history
…-format

Completing Chrisbirster new string with format
  • Loading branch information
andydotxyz committed Jun 2, 2022
2 parents efbf153 + 7ed5724 commit 8c5ac40
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 8 deletions.
17 changes: 15 additions & 2 deletions data/binding/sprintf.go
Expand Up @@ -20,7 +20,7 @@ type sprintfString struct {
// and will have all the same limitation as those function.
//
// Since: 2.2
func NewSprintf(format string, b ...DataItem) (String, error) {
func NewSprintf(format string, b ...DataItem) String {
ret := &sprintfString{
String: NewString(),
format: format,
Expand All @@ -31,7 +31,7 @@ func NewSprintf(format string, b ...DataItem) (String, error) {
value.AddListener(ret)
}

return ret, nil
return ret
}

func (s *sprintfString) DataChanged() {
Expand Down Expand Up @@ -203,3 +203,16 @@ func (s *sprintfString) Set(str string) error {

return nil
}

// StringToStringWithFormat creates a binding that converts a string to another string using the specified format.
// Changes to the returned String will be pushed to the passed in String and setting a new string value will parse and
// set the underlying String if it matches the format and the parse was successful.
//
// Since: 2.2
func StringToStringWithFormat(str String, format string) String {
if format == "%s" { // Same as not using custom formatting.
return str
}

return NewSprintf(format, str)
}
42 changes: 36 additions & 6 deletions data/binding/sprintf_test.go
Expand Up @@ -25,10 +25,8 @@ func TestSprintfConversionRead(t *testing.T) {
bu := BindURI(&u)

format := "Bool %v, Float %f, Int %i, Rune %v, String '%s', URI '%s'"
sp, err := NewSprintf(format, bb, bf, bi, br, bs, bu)
sp := NewSprintf(format, bb, bf, bi, br, bs, bu)
expected := fmt.Sprintf(format, b, f, i, r, s, u)

assert.Nil(t, err)
assert.NotNil(t, sp)

waitForItems()
Expand Down Expand Up @@ -73,10 +71,8 @@ func TestSprintfConversionReadWrite(t *testing.T) {
bu := BindURI(&u)

format := "Bool %v , Float %f , Int %v , Rune %v , String %s , URI %s"
sp, err := NewSprintf(format, bb, bf, bi, br, bs, bu)
sp := NewSprintf(format, bb, bf, bi, br, bs, bu)
expected := fmt.Sprintf(format, b, f, i, r, s, u)

assert.Nil(t, err)
assert.NotNil(t, sp)

waitForItems()
Expand Down Expand Up @@ -109,3 +105,37 @@ func TestSprintfConversionReadWrite(t *testing.T) {
assert.Equal(t, expectedChange, sChange)
assert.NotEqual(t, sGenerated, sChange)
}

func TestNewStringWithFormat(t *testing.T) {
var s = "this is a string"
bs := BindString(&s)

format := "String %s"
sp := StringToStringWithFormat(bs, format)
expected := fmt.Sprintf(format, s)
assert.NotNil(t, sp)

waitForItems()

sGenerated, err := sp.Get()
assert.Nil(t, err)
assert.NotNil(t, sGenerated)
assert.Equal(t, expected, sGenerated)

err = sp.Set("String nospacestring")
assert.Nil(t, err)

waitForItems()

assert.Equal(t, s, "nospacestring")
expectedChange := fmt.Sprintf(format, s)
sChange, err := sp.Get()
assert.Nil(t, err)
assert.NotNil(t, sChange)
assert.Equal(t, expectedChange, sChange)
assert.NotEqual(t, sGenerated, sChange)

emptyFormat := "%s"
wrapped := StringToStringWithFormat(bs, emptyFormat)
assert.Equal(t, bs, wrapped)
}

0 comments on commit 8c5ac40

Please sign in to comment.