From baa952d1455779793eeb3b495f111ffe2bd2b52b Mon Sep 17 00:00:00 2001 From: Randy Reddig Date: Mon, 25 Sep 2023 13:36:40 -0700 Subject: [PATCH] wit, testdata: generate WIT for enums --- testdata/types.wit.json.golden.wit | 4 +- testdata/wasi.wit.json.golden.wit | 82 +++++++++++++++++++++++++++++- wit/wit.go | 28 +++++++++- 3 files changed, 108 insertions(+), 6 deletions(-) diff --git a/testdata/types.wit.json.golden.wit b/testdata/types.wit.json.golden.wit index 046bea59..3148a0fe 100644 --- a/testdata/types.wit.json.golden.wit +++ b/testdata/types.wit.json.golden.wit @@ -35,8 +35,8 @@ interface types { variant t36 { a, b(u32) } variant t37 { a, b(option) } type t4 = u64 - /* TODO(t41) */ - /* TODO(t42) */ + enum t41 { a, b, c } + enum t42 { a, b, c } type t43 = bool type t44 = string type t45 = list>> diff --git a/testdata/wasi.wit.json.golden.wit b/testdata/wasi.wit.json.golden.wit index 1a1dc3f8..9b0145e9 100644 --- a/testdata/wasi.wit.json.golden.wit +++ b/testdata/wasi.wit.json.golden.wit @@ -1,7 +1,85 @@ package wasi:filesystem interface wasi { - /* TODO(clockid) */ - /* TODO(errno) */ + enum clockid { realtime, monotonic } + enum errno { + success, + toobig, + access, + addrinuse, + addrnotavail, + afnosupport, + again, + already, + badf, + badmsg, + busy, + canceled, + child, + connaborted, + connrefused, + connreset, + deadlk, + destaddrreq, + dom, + dquot, + exist, + fault, + fbig, + hostunreach, + idrm, + ilseq, + inprogress, + intr, + inval, + io, + isconn, + isdir, + loop, + mfile, + mlink, + msgsize, + multihop, + nametoolong, + netdown, + netreset, + netunreach, + nfile, + nobufs, + nodev, + noent, + noexec, + nolck, + nolink, + nomem, + nomsg, + noprotoopt, + nospc, + nosys, + notconn, + notdir, + notempty, + notrecoverable, + notsock, + notsup, + notty, + nxio, + overflow, + ownerdead, + perm, + pipe, + proto, + protonosupport, + prototype, + range, + rofs, + spipe, + srch, + stale, + timedout, + txtbsy, + xdev, + notcapable + } type timestamp = u64 } diff --git a/wit/wit.go b/wit/wit.go index 23337d95..cfcb3580 100644 --- a/wit/wit.go +++ b/wit/wit.go @@ -278,7 +278,7 @@ func (t *Tuple) WIT(ctx Node, _ string) string { return b.String() } -func (v *Variant) WIT(ctx Node, name string) string { +func (v *Variant) WIT(_ Node, name string) string { var b strings.Builder b.WriteString("variant ") b.WriteString(name) @@ -289,7 +289,7 @@ func (v *Variant) WIT(ctx Node, name string) string { if i > 0 { b.WriteString(",\n") } - b.WriteString(indent(v.Cases[i].WIT(ctx, ""))) + b.WriteString(indent(v.Cases[i].WIT(v, ""))) } b.WriteRune('\n') } @@ -309,6 +309,30 @@ func (c *Case) WIT(_ Node, _ string) string { return b.String() } +func (e *Enum) WIT(_ Node, name string) string { + var b strings.Builder + b.WriteString("enum ") + b.WriteString(name) + b.WriteString(" {") + if len(e.Cases) > 0 { + b.WriteRune('\n') + for i := range e.Cases { + if i > 0 { + b.WriteString(",\n") + } + b.WriteString(indent(e.Cases[i].WIT(e, ""))) + } + b.WriteRune('\n') + } + b.WriteRune('}') + return unwrap(b.String()) +} + +func (c *EnumCase) WIT(_ Node, _ string) string { + // TODO: docs + return c.Name +} + func (o *Option) WIT(_ Node, name string) string { var b strings.Builder if name != "" {