Skip to content

Commit

Permalink
bindnode: fix for stringjoin struct emission when first field is the …
Browse files Browse the repository at this point in the history
…empty string.

In the case of an empty string as the first field, the buffer length
is not a valid proxy for whether we're on the first field or not.

This means if we have some type like:
`type Foo struct {a String; b String} representation stringjoin(":")`,
and the value of it is `{"", "b"}`, then the string of that
should still be ":b".
Before this fix, it would incorrectly be emitted as "b" (no joiner),
which would not round-trip.
  • Loading branch information
warpfork committed Sep 2, 2021
1 parent f9a4ced commit c6a744d
Showing 1 changed file with 4 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

0 comments on commit c6a744d

Please sign in to comment.