Skip to content

Commit

Permalink
wit, testdata: add Variant.WIT()
Browse files Browse the repository at this point in the history
  • Loading branch information
ydnar committed Sep 25, 2023
1 parent 4879541 commit f489548
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 8 deletions.
24 changes: 19 additions & 5 deletions testdata/types.wit.json.golden.wit
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,25 @@ interface types {
flags t30 {}
flags t31 {a, b, c}
flags t32 {a, b, c}
/* TODO(t33) */
/* TODO(t34) */
/* TODO(t35) */
/* TODO(t36) */
/* TODO(t37) */
variant t33 {
a
}
variant t34 {
a,
b
}
variant t35 {
a,
b
}
variant t36 {
a,
b(u32)
}
variant t37 {
a,
b(/* TODO() */)
}
type t4 = u64
/* TODO(t41) */
/* TODO(t42) */
Expand Down
4 changes: 3 additions & 1 deletion testdata/worlds-with-types.wit.json.golden.wit
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ world the-test {
record a {
x: u32
}
/* TODO(b) */
variant b {
c(a)
}
import foo: func(a: a) -> b
export bar: func(a: a) -> b
}
32 changes: 30 additions & 2 deletions wit/wit.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,6 @@ func (f *Flags) WIT(ctx Node, name string) string {
return b.String()
}

const wrapFlags = 3

func (f *Flag) WIT(_ Node, _ string) string {
// TODO: docs
return f.Name
Expand All @@ -259,6 +257,36 @@ func (t *Tuple) WIT(ctx Node, _ string) string {
return b.String()
}

func (v *Variant) WIT(ctx Node, name string) string {
var b strings.Builder
b.WriteString("variant ")
b.WriteString(name)
b.WriteString(" {")
if len(v.Cases) > 0 {
b.WriteRune('\n')
for i := range v.Cases {
if i > 0 {
b.WriteString(",\n")
}
b.WriteString(indent(v.Cases[i].WIT(ctx, "")))
}
b.WriteRune('\n')
}
b.WriteRune('}')
return b.String()
}

func (c *Case) WIT(_ Node, _ string) string {
var b strings.Builder
b.WriteString(c.Name)
if c.Type != nil {
b.WriteRune('(')
b.WriteString(c.Type.WIT(c, ""))
b.WriteRune(')')
}
return b.String()
}

func (l *List) WIT(ctx Node, _ string) string {
return "list<" + l.Type.WIT(l, "") + ">"
}
Expand Down

0 comments on commit f489548

Please sign in to comment.