Skip to content

Commit

Permalink
Merge pull request #239 from ipld/bindnode-stringjoin-fix-for-first-f…
Browse files Browse the repository at this point in the history
…ield-empty

bindnode: fix for stringjoin struct emission when first field is the empty string
  • Loading branch information
warpfork authored Sep 3, 2021
2 parents f9a4ced + dbe1bef commit c32e220
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
5 changes: 4 additions & 1 deletion node/bindnode/repr.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,7 @@ func (w *_nodeRepr) AsString() (string, error) {
case schema.StructRepresentation_Stringjoin:
var b strings.Builder
itr := (*_node)(w).MapIterator()
first := true
for !itr.Done() {
_, v, err := itr.Next()
if err != nil {
Expand All @@ -373,7 +374,9 @@ func (w *_nodeRepr) AsString() (string, error) {
if err != nil {
return "", err
}
if b.Len() > 0 {
if first {
first = false
} else {
b.WriteString(stg.GetDelim())
}
b.WriteString(s)
Expand Down
29 changes: 29 additions & 0 deletions node/tests/schemaStructReprStringjoin.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,35 @@ func SchemaTestStructReprStringjoin(t *testing.T, engine Engine) {
})
})

t.Run("first field empty string works", func(t *testing.T) {
np := engine.PrototypeByName("ManystringStruct")
nrp := engine.PrototypeByName("ManystringStruct.Repr")
var n schema.TypedNode
t.Run("typed-create", func(t *testing.T) {
n = fluent.MustBuildMap(np, 2, func(ma fluent.MapAssembler) {
ma.AssembleEntry("foo").AssignString("")
ma.AssembleEntry("bar").AssignString("v2")
}).(schema.TypedNode)
t.Run("typed-read", func(t *testing.T) {
Require(t, n.Kind(), ShouldEqual, datamodel.Kind_Map)
Wish(t, n.Length(), ShouldEqual, int64(2))
Wish(t, must.String(must.Node(n.LookupByString("foo"))), ShouldEqual, "")
Wish(t, must.String(must.Node(n.LookupByString("bar"))), ShouldEqual, "v2")
})
t.Run("repr-read", func(t *testing.T) {
nr := n.Representation()
Require(t, nr.Kind(), ShouldEqual, datamodel.Kind_String)
Wish(t, must.String(nr), ShouldEqual, ":v2") // Note the leading colon is still present.
})
})
t.Run("repr-create", func(t *testing.T) {
nr := fluent.MustBuild(nrp, func(na fluent.NodeAssembler) {
na.AssignString(":v2")
})
Wish(t, datamodel.DeepEqual(n, nr), ShouldEqual, true)
})
})

t.Run("nested stringjoin structs work", func(t *testing.T) {
np := engine.PrototypeByName("Recurzorator")
nrp := engine.PrototypeByName("Recurzorator.Repr")
Expand Down

0 comments on commit c32e220

Please sign in to comment.