Skip to content
This repository has been archived by the owner on Nov 4, 2023. It is now read-only.

Handle JSON encoding of void types. #11

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions capnpc-go/capnpc-go.go
Original file line number Diff line number Diff line change
Expand Up @@ -744,7 +744,10 @@ func (f *Field) json(w io.Writer) {
switch f.Which() {
case FIELD_SLOT:
fs := f.Slot()
fprintf(w, "{ s := s.%s(); ", title(f.Name()))
fprintf(w, "{")
if fs.Type().hasAccessor() {
fprintf(w, "s := s.%s(); ", title(f.Name()))
}
fs.Type().json(w)
fprintf(w, "}; ")
case FIELD_GROUP:
Expand All @@ -756,6 +759,11 @@ func (f *Field) json(w io.Writer) {
}
}

func (t Type) hasAccessor() bool {
w := t.Which()
return w != TYPE_INTERFACE && w != TYPE_VOID
}

func (t Type) json(w io.Writer) {
switch t.Which() {
case TYPE_UINT8, TYPE_UINT16, TYPE_UINT32, TYPE_UINT64,
Expand All @@ -774,13 +782,20 @@ func (t Type) json(w io.Writer) {
case TYPE_LIST:
fprintf(w, "{ err = b.WriteByte('[');")
writeErrCheck(w)
fprintf(w, "for i, s := range s.ToArray() {")
if t.List().ElementType().hasAccessor() {
fprintf(w, "for i, s := range s.ToArray() {")
} else {
fprintf(w, "for i := 0; i < s.Len(); i++ {")
}
fprintf(w, `if i != 0 { _, err = b.WriteString(", "); };`)
writeErrCheck(w)
typ := t.List().ElementType()
typ.json(w)
fprintf(w, "}; err = b.WriteByte(']'); };")
writeErrCheck(w)
case TYPE_VOID, TYPE_INTERFACE:
fprintf(w, "_, err = b.WriteString(\"null\");")
writeErrCheck(w)
}
}

Expand Down