-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #280 from bytecodealliance/ydnar/enum-json
cm, wit/bindgen: add JSON marshaling for enum types
- Loading branch information
Showing
12 changed files
with
322 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package cm | ||
|
||
import "errors" | ||
|
||
// CaseUnmarshaler returns an function that can unmarshal text into | ||
// [variant] or [enum] case T. | ||
// | ||
// [enum]: https://component-model.bytecodealliance.org/design/wit.html#enums | ||
// [variant]: https://component-model.bytecodealliance.org/design/wit.html#variants | ||
func CaseUnmarshaler[T ~uint8 | ~uint16 | ~uint32](cases []string) func(v *T, text []byte) error { | ||
if len(cases) <= linearScanThreshold { | ||
return func(v *T, text []byte) error { | ||
if len(text) == 0 { | ||
return errEmpty | ||
} | ||
s := string(text) | ||
for i := 0; i < len(cases); i++ { | ||
if cases[i] == s { | ||
*v = T(i) | ||
return nil | ||
} | ||
} | ||
return errNoMatchingCase | ||
} | ||
} | ||
|
||
m := make(map[string]T, len(cases)) | ||
for i, v := range cases { | ||
m[v] = T(i) | ||
} | ||
|
||
return func(v *T, text []byte) error { | ||
if len(text) == 0 { | ||
return errEmpty | ||
} | ||
s := string(text) | ||
c, ok := m[s] | ||
if !ok { | ||
return errNoMatchingCase | ||
} | ||
*v = c | ||
return nil | ||
} | ||
} | ||
|
||
const linearScanThreshold = 16 | ||
|
||
var ( | ||
errEmpty = errors.New("empty text") | ||
errNoMatchingCase = errors.New("no matching case") | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package cm | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestCaseUnmarshaler(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
cases []string | ||
}{ | ||
{"nil", nil}, | ||
{"empty slice", []string{}}, | ||
{"a b c", strings.SplitAfter("abc", "")}, | ||
{"a b c d e f g", strings.SplitAfter("abcdefg", "")}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
f := CaseUnmarshaler[uint8](tt.cases) | ||
for want, c := range tt.cases { | ||
var got uint8 | ||
err := f(&got, []byte(c)) | ||
if err != nil { | ||
t.Error(err) | ||
return | ||
} | ||
if got != uint8(want) { | ||
t.Errorf("f(%q): got %d, expected %d", c, got, want) | ||
} | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
42 changes: 34 additions & 8 deletions
42
tests/generated/wasi/sockets/v0.2.0/network/network.wit.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.