Skip to content

Commit

Permalink
Merge pull request #152 from ydnar/ydnar/issue-151
Browse files Browse the repository at this point in the history
wit: support JSON emitted by wasm-tools without feature gate support
  • Loading branch information
ydnar authored Aug 30, 2024
2 parents dc4cac5 + 697ce09 commit df30e68
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Fixed
- [#151](https://github.com/ydnar/wasm-tools-go/issues/151): backport support for JSON generated by `wasm-tools` prior to v1.209.0, which added `@since` and `@unstable` feature gates.

## [v0.1.5] — 2024-08-23

### Added
Expand Down
25 changes: 20 additions & 5 deletions wit/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ func (res *Resolve) ResolveCodec(v any) codec.Codec {
case *WorldItem:
return &worldItemCodec{v}

// Other
case *InterfaceRef:
return &interfaceRefCodec{v, res}

// Imported
case *semver.Version:
return &semverCodec{&v}
Expand Down Expand Up @@ -215,14 +219,25 @@ func (pn *Ident) DecodeString(s string) error {
return err
}

// DecodeField implements the [codec.FieldDecoder] interface
// to decode a struct or JSON object.
func (i *InterfaceRef) DecodeField(dec codec.Decoder, name string) error {
// interfaceRefCodec translates WIT interface references into an *InterfaceRef.
type interfaceRefCodec struct {
ref *InterfaceRef
*Resolve
}

// This exists to support legacy JSON from wasm-tools pre v1.209.0.
// See https://github.com/ydnar/wasm-tools-go/issues/151.
func (c *interfaceRefCodec) DecodeInt(i int) error {
c.ref.Interface = c.getInterface(i)
return nil
}

func (c *interfaceRefCodec) DecodeField(dec codec.Decoder, name string) error {
switch name {
case "id":
return dec.Decode(&i.Interface)
return dec.Decode(&c.ref.Interface)
case "stability":
return dec.Decode(&i.Stability)
return dec.Decode(&c.ref.Stability)
}
return nil
}
Expand Down

0 comments on commit df30e68

Please sign in to comment.