Skip to content

Commit

Permalink
wit: reduce exported API surface
Browse files Browse the repository at this point in the history
  • Loading branch information
ydnar committed Oct 2, 2023
1 parent aaaac38 commit 94e499a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 20 deletions.
1 change: 0 additions & 1 deletion wit/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,6 @@ func (*Stream) Align() uintptr { return 0 }
// TypeOwner is the interface implemented by any type that can own a TypeDef,
// currently [World] and [Interface].
type TypeOwner interface {
RelativeName(*Package) string
isTypeOwner()
}

Expand Down
45 changes: 26 additions & 19 deletions wit/wit.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,6 @@ func (w *World) itemWIT(motion, name string, v WorldItem) string {
panic("BUG: unknown WorldItem")
}

func (w *World) RelativeName(p *Package) string {
if w.Package == p {
return w.Name
}
return w.Package.Name.String() + "/" + w.Name
}

// WIT returns the WIT representation of i.
func (i *Interface) WIT(ctx Node, name string) string {
if i.Name != nil && name == "" {
Expand All @@ -110,7 +103,7 @@ func (i *Interface) WIT(ctx Node, name string) string {
b.WriteString(name)
b.WriteRune(' ')
case *World:
rname := i.RelativeName(ctx.Package)
rname := relativeName(i, ctx.Package)
if rname != "" {
return rname
}
Expand Down Expand Up @@ -145,16 +138,6 @@ func (i *Interface) WIT(ctx Node, name string) string {
return b.String()
}

func (i *Interface) RelativeName(p *Package) string {
if i.Name == nil {
return ""
}
if i.Package == p {
return *i.Name
}
return i.Package.Name.String() + "/" + *i.Name
}

// WIT returns the WIT representation of [TypeDef] t.
func (t *TypeDef) WIT(ctx Node, name string) string {
if t.Name != nil && name == "" {
Expand All @@ -167,7 +150,7 @@ func (t *TypeDef) WIT(ctx Node, name string) string {
if t.Owner == ctx.Owner && t.Name != nil {
return "type " + name + " = " + *t.Name
}
ownerName := t.Owner.RelativeName(ctx.Package())
ownerName := relativeName(t.Owner, ctx.Package())
if t.Name != nil && *t.Name != name {
return fmt.Sprintf("use %s.{%s as %s}", ownerName, *t.Name, name)
}
Expand All @@ -186,6 +169,30 @@ func (t *TypeDef) WIT(ctx Node, name string) string {
return t.Kind.WIT(ctx, name)
}

func relativeName(o TypeOwner, p *Package) string {
var op *Package
var name string
switch o := o.(type) {
case *Interface:
if o.Name == nil {
return ""
}
op = o.Package
name = *o.Name

case *World:
op = o.Package
name = o.Name
}
if op == p {
return name
}
if op == nil {
return ""
}
return op.Name.String() + "/" + name
}

func (r *Record) WIT(ctx Node, name string) string {
var b strings.Builder
b.WriteString("record ")
Expand Down

0 comments on commit 94e499a

Please sign in to comment.