-
Notifications
You must be signed in to change notification settings - Fork 270
feat(go): add map type support #1583
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -30,7 +30,6 @@ impl LanguageMethods for Go { | |
| config.error_context | ||
| || name == "async-trait-function.wit" | ||
| || name == "named-fixed-length-list.wit" | ||
| || name == "map.wit" | ||
| } | ||
|
|
||
| fn default_bindgen_args_for_codegen(&self) -> &[&str] { | ||
|
|
@@ -162,7 +161,7 @@ fn replace_bindings_go_mod(runner: &Runner, bindings_dir: &Path) -> Result<()> { | |
| super::write_if_different( | ||
| &bindings_dir.join("go.mod"), | ||
| format!( | ||
| "module wit_component\n\ngo 1.25\n\nreplace go.bytecodealliance.org => {}", | ||
| "module wit_component\n\ngo 1.25\n\nreplace go.bytecodealliance.org/pkg => {}", | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. related to #1548 As far as I can tell, it suppose to be
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems reasonable, but I wonder how the tests have been passing so far without that. @asteurer might have ideas.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, that was definitely a mistake. I'm not sure why Go allows an incomplete prefix for a replace directive like that. Great catch! |
||
| go_package_path.display() | ||
| ), | ||
| )?; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,188 @@ | ||
| //@ wasmtime-flags = '-Wcomponent-model-map' | ||
|
|
||
| package export_wit_world | ||
|
|
||
| import ( | ||
| "fmt" | ||
| test "wit_component/test_maps_to_test" | ||
|
|
||
| . "go.bytecodealliance.org/pkg/wit/types" | ||
| ) | ||
|
|
||
| func Run() { | ||
| testNamedRoundtrip() | ||
| testBytesRoundtrip() | ||
| testEmptyRoundtrip() | ||
| testOptionRoundtrip() | ||
| testRecordRoundtrip() | ||
| testInlineRoundtrip() | ||
| testLargeRoundtrip() | ||
| testMultiParamRoundtrip() | ||
| testNestedRoundtrip() | ||
| testVariantRoundtrip() | ||
| testResultRoundtrip() | ||
| testTupleRoundtrip() | ||
| testSingleEntryRoundtrip() | ||
| } | ||
|
|
||
| func testNamedRoundtrip() { | ||
| input := test.NamesById{ | ||
| 1: "uno", | ||
| 2: "two", | ||
| } | ||
| result := test.NamedRoundtrip(input) | ||
| assertEqual(result["uno"], uint32(1)) | ||
| assertEqual(result["two"], uint32(2)) | ||
| } | ||
|
|
||
| func testBytesRoundtrip() { | ||
| input := test.BytesByName{ | ||
| "hello": []uint8("world"), | ||
| "bin": {0, 1, 2}, | ||
| } | ||
| result := test.BytesRoundtrip(input) | ||
| assertSliceEqual(result["hello"], []uint8("world")) | ||
| assertSliceEqual(result["bin"], []uint8{0, 1, 2}) | ||
| } | ||
|
|
||
| func testEmptyRoundtrip() { | ||
| input := test.NamesById{} | ||
| result := test.EmptyRoundtrip(input) | ||
| assertEqual(len(result), 0) | ||
| } | ||
|
|
||
| func testOptionRoundtrip() { | ||
| input := map[string]Option[uint32]{ | ||
| "some": Some[uint32](42), | ||
| "none": None[uint32](), | ||
| } | ||
| result := test.OptionRoundtrip(input) | ||
| assertEqual(len(result), 2) | ||
| assertEqual(result["some"].Some(), uint32(42)) | ||
| assertEqual(result["none"].Tag(), OptionNone) | ||
| } | ||
|
|
||
| func testRecordRoundtrip() { | ||
| entry := test.LabeledEntry{ | ||
| Label: "test-label", | ||
| Values: test.NamesById{ | ||
| 10: "ten", | ||
| 20: "twenty", | ||
| }, | ||
| } | ||
| result := test.RecordRoundtrip(entry) | ||
| assertEqual(result.Label, "test-label") | ||
| assertEqual(len(result.Values), 2) | ||
| assertEqual(result.Values[10], "ten") | ||
| assertEqual(result.Values[20], "twenty") | ||
| } | ||
|
|
||
| func testInlineRoundtrip() { | ||
| input := map[uint32]string{ | ||
| 1: "one", | ||
| 2: "two", | ||
| } | ||
| result := test.InlineRoundtrip(input) | ||
| assertEqual(len(result), 2) | ||
| assertEqual(result["one"], uint32(1)) | ||
| assertEqual(result["two"], uint32(2)) | ||
| } | ||
|
|
||
| func testLargeRoundtrip() { | ||
| input := make(test.NamesById) | ||
| for i := uint32(0); i < 100; i++ { | ||
| input[i] = fmt.Sprintf("value-%d", i) | ||
| } | ||
| result := test.LargeRoundtrip(input) | ||
| assertEqual(len(result), 100) | ||
| for i := uint32(0); i < 100; i++ { | ||
| assertEqual(result[i], fmt.Sprintf("value-%d", i)) | ||
| } | ||
| } | ||
|
|
||
| func testMultiParamRoundtrip() { | ||
| names := test.NamesById{ | ||
| 1: "one", | ||
| 2: "two", | ||
| } | ||
| bytes := test.BytesByName{ | ||
| "key": {42}, | ||
| } | ||
| ids, bytesOut := test.MultiParamRoundtrip(names, bytes) | ||
| assertEqual(len(ids), 2) | ||
| assertEqual(ids["one"], uint32(1)) | ||
| assertEqual(ids["two"], uint32(2)) | ||
| assertEqual(len(bytesOut), 1) | ||
| assertSliceEqual(bytesOut["key"], []uint8{42}) | ||
| } | ||
|
|
||
| func testNestedRoundtrip() { | ||
| input := map[string]map[uint32]string{ | ||
| "group-a": { | ||
| 1: "one", | ||
| 2: "two", | ||
| }, | ||
| "group-b": { | ||
| 10: "ten", | ||
| }, | ||
| } | ||
| result := test.NestedRoundtrip(input) | ||
| assertEqual(len(result), 2) | ||
| assertEqual(result["group-a"][1], "one") | ||
| assertEqual(result["group-a"][2], "two") | ||
| assertEqual(result["group-b"][10], "ten") | ||
| } | ||
|
|
||
| func testVariantRoundtrip() { | ||
| m := test.NamesById{1: "one"} | ||
| asMap := test.VariantRoundtrip(test.MakeMapOrStringAsMap(m)) | ||
| assertEqual(asMap.Tag(), test.MapOrStringAsMap) | ||
| assertEqual(asMap.AsMap()[1], "one") | ||
|
|
||
| asStr := test.VariantRoundtrip(test.MakeMapOrStringAsString("hello")) | ||
| assertEqual(asStr.Tag(), test.MapOrStringAsString) | ||
| assertEqual(asStr.AsString(), "hello") | ||
| } | ||
|
|
||
| func testResultRoundtrip() { | ||
| m := test.NamesById{5: "five"} | ||
| okResult := test.ResultRoundtrip(Ok[test.NamesById, string](m)) | ||
| assertEqual(okResult.Tag(), ResultOk) | ||
| assertEqual(okResult.Ok()[5], "five") | ||
|
|
||
| errResult := test.ResultRoundtrip(Err[test.NamesById, string]("bad input")) | ||
| assertEqual(errResult.Tag(), ResultErr) | ||
| assertEqual(errResult.Err(), "bad input") | ||
| } | ||
|
|
||
| func testTupleRoundtrip() { | ||
| m := test.NamesById{7: "seven"} | ||
| resultMap, resultNum := test.TupleRoundtrip(Tuple2[test.NamesById, uint64]{m, 42}) | ||
| assertEqual(len(resultMap), 1) | ||
| assertEqual(resultMap[7], "seven") | ||
| assertEqual(resultNum, uint64(42)) | ||
| } | ||
|
|
||
| func testSingleEntryRoundtrip() { | ||
| input := test.NamesById{99: "ninety-nine"} | ||
| result := test.SingleEntryRoundtrip(input) | ||
| assertEqual(len(result), 1) | ||
| assertEqual(result[99], "ninety-nine") | ||
| } | ||
|
|
||
| func assertEqual[T comparable](a T, b T) { | ||
| if a != b { | ||
| panic(fmt.Sprintf("%v not equal to %v", a, b)) | ||
| } | ||
| } | ||
|
|
||
| func assertSliceEqual[T comparable](a []T, b []T) { | ||
| if len(a) != len(b) { | ||
| panic(fmt.Sprintf("slices have different lengths: %d vs %d", len(a), len(b))) | ||
| } | ||
| for i := range a { | ||
| if a[i] != b[i] { | ||
| panic(fmt.Sprintf("slices differ at index %d: %v vs %v", i, a[i], b[i])) | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| package export_test_maps_to_test | ||
|
|
||
| import ( | ||
| . "wit_component/test_maps_to_test" | ||
|
|
||
| . "go.bytecodealliance.org/pkg/wit/types" | ||
| ) | ||
|
|
||
| func NamedRoundtrip(a NamesById) IdsByName { | ||
| result := make(IdsByName) | ||
| for id, name := range a { | ||
| result[name] = id | ||
| } | ||
| return result | ||
| } | ||
|
|
||
| func BytesRoundtrip(a BytesByName) BytesByName { | ||
| return a | ||
| } | ||
|
|
||
| func EmptyRoundtrip(a NamesById) NamesById { | ||
| return a | ||
| } | ||
|
|
||
| func OptionRoundtrip(a map[string]Option[uint32]) map[string]Option[uint32] { | ||
| return a | ||
| } | ||
|
|
||
| func RecordRoundtrip(a LabeledEntry) LabeledEntry { | ||
| return a | ||
| } | ||
|
|
||
| func InlineRoundtrip(a map[uint32]string) map[string]uint32 { | ||
| result := make(map[string]uint32) | ||
| for k, v := range a { | ||
| result[v] = k | ||
| } | ||
| return result | ||
| } | ||
|
|
||
| func LargeRoundtrip(a NamesById) NamesById { | ||
| return a | ||
| } | ||
|
|
||
| func MultiParamRoundtrip(a NamesById, b BytesByName) (IdsByName, BytesByName) { | ||
| ids := make(IdsByName) | ||
| for id, name := range a { | ||
| ids[name] = id | ||
| } | ||
| return ids, b | ||
| } | ||
|
|
||
| func NestedRoundtrip(a map[string]map[uint32]string) map[string]map[uint32]string { | ||
| return a | ||
| } | ||
|
|
||
| func VariantRoundtrip(a MapOrString) MapOrString { | ||
| return a | ||
| } | ||
|
|
||
| func ResultRoundtrip(a Result[NamesById, string]) Result[NamesById, string] { | ||
| return a | ||
| } | ||
|
|
||
| func TupleRoundtrip(a Tuple2[NamesById, uint64]) (NamesById, uint64) { | ||
| return a.F0, a.F1 | ||
| } | ||
|
|
||
| func SingleEntryRoundtrip(a NamesById) NamesById { | ||
| return a | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.