Skip to content

Commit

Permalink
wit: add support for WIT files with multiple packages
Browse files Browse the repository at this point in the history
  • Loading branch information
ydnar committed Jun 8, 2024
1 parent ac8a3a3 commit 607b7c1
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 10 deletions.
3 changes: 0 additions & 3 deletions wit/testdata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,6 @@ func TestGoldenWITRoundTrip(t *testing.T) {
}
err := loadTestdata(func(path string, res *Resolve) error {
data := res.WIT(nil, "")
if strings.Count(data, "package ") > 1 {
return nil
}
t.Run(path, func(t *testing.T) {
// Run the generated WIT through wasm-tools to generate JSON.
cmd := exec.Command("wasm-tools", "component", "wit", "-j")
Expand Down
12 changes: 5 additions & 7 deletions wit/wit.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ func unwrap(s string) string {
// WITKind returns the WIT kind.
func (*Resolve) WITKind() string { return "resolve" }

// WIT returns the [WIT] text format for [Resolve] r. Note that the return value could
// represent multiple files, so may not be precisely valid WIT text.
// WIT returns the [WIT] text format for [Resolve] r.
//
// [WIT]: https://github.com/WebAssembly/component-model/blob/main/design/mvp/WIT.md
func (r *Resolve) WIT(_ Node, _ string) string {
Expand Down Expand Up @@ -875,31 +874,30 @@ func (p *Package) WIT(ctx Node, _ string) string {
b.WriteString(p.Docs.WIT(ctx, ""))
b.WriteString("package ")
b.WriteString(p.Name.String())
b.WriteString(";\n")
b.WriteString(" {\n")
if p.Interfaces.Len() > 0 {
b.WriteRune('\n')
i := 0
p.Interfaces.All()(func(name string, face *Interface) bool {
if i > 0 {
b.WriteRune('\n')
}
b.WriteString(face.WIT(p, name))
b.WriteString(indent(face.WIT(p, name)))
b.WriteRune('\n')
i++
return true
})
}
if p.Worlds.Len() > 0 {
b.WriteRune('\n')
i := 0
p.Worlds.All()(func(name string, w *World) bool {
if i > 0 {
b.WriteRune('\n')
}
b.WriteString(w.WIT(p, name))
b.WriteString(indent(w.WIT(p, name)))
b.WriteRune('\n')
return true
})
}
b.WriteRune('}')
return b.String()
}

0 comments on commit 607b7c1

Please sign in to comment.