Skip to content

Commit

Permalink
kebab to snake and back for record field names
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Nelson committed Dec 9, 2024
1 parent 9975e45 commit 277ab71
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 4 deletions.
18 changes: 18 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,24 @@
"kind": "build",
"isDefault": true
}
},
{
"label": "rust format",
"type": "process",
"command": "cargo",
"args": ["fmt"],
"options": {
"cwd": "${workspaceRoot}/native/wasmex"
},
"problemMatcher": [
"$mixCompileWarning",
"$mixCompileError"
],
"group": {
"kind": "build",
"isDefault": true
}
}

]
}
16 changes: 16 additions & 0 deletions native/wasmex/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions native/wasmex/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ wasi-common = "26.0.1"
wiggle = "26.0.1"
wat = "1.220.0"
wit-parser = "0.221.2"
convert_case = "0.6.0"
12 changes: 8 additions & 4 deletions native/wasmex/src/component_instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use crate::component::ComponentInstanceResource;
use crate::store::ComponentStoreData;
use crate::store::ComponentStoreResource;

use convert_case::{Case, Casing};

use rustler::types::atom::nil;
use rustler::types::tuple;
use rustler::types::tuple::make_tuple;
Expand Down Expand Up @@ -63,7 +65,9 @@ pub fn component_call_function<'a>(
let _ = function.post_return(&mut *component_store);
Ok(encode_result(env, result))
}
Err(err) => Ok(env.error_tuple(format!("Error executing function: {err}"))),
Err(err) => Err(rustler::Error::Term(Box::new(format!(
"Error executing function: {err}"
)))),
}
}

Expand Down Expand Up @@ -150,13 +154,13 @@ fn term_to_val(param_term: &Term, param_type: &Type) -> Result<Val, Error> {

fn term_to_field_name(key_term: &Term) -> String {
match key_term.get_type() {
TermType::Atom => key_term.atom_to_string().unwrap(),
_ => key_term.decode::<String>().unwrap(),
TermType::Atom => key_term.atom_to_string().unwrap().to_case(Case::Kebab),
_ => key_term.decode::<String>().unwrap().to_case(Case::Kebab),
}
}

fn field_name_to_term<'a>(env: &rustler::Env<'a>, field_name: &str) -> Term<'a> {
rustler::serde::atoms::str_to_term(env, field_name).unwrap()
rustler::serde::atoms::str_to_term(env, &field_name.to_case(Case::Snake)).unwrap()
}

fn encode_result(env: rustler::Env, vals: Vec<Val>) -> Term {
Expand Down
1 change: 1 addition & 0 deletions test/component_fixtures/hello_world/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
npx jco componentize -w hello-world.wit -o hello_world.wasm hello-world.js
4 changes: 4 additions & 0 deletions test/component_fixtures/hello_world/hello-world.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,8 @@ export function multiGreet(who, times) {

export function greetMany(people) {
return people.map((person) => `Hello, ${person}!`);
}

export function echoKebab(kebabRecord) {
return kebabRecord;
}
4 changes: 4 additions & 0 deletions test/component_fixtures/hello_world/hello-world.wit
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package local:hello-world;

world hello-world {
record kebab-record {
kebab-field: string
}
export greet: func(who: string) -> string;
export multi-greet: func(who: string, times: u16) -> list<string>;
export greet-many: func(people: list<string>) -> list<string>;
export echo-kebab: func(kebab-record: kebab-record) -> kebab-record;
}
Binary file modified test/component_fixtures/hello_world/hello_world.wasm
Binary file not shown.
14 changes: 14 additions & 0 deletions test/components/component_types_test.exs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
defmodule Wasm.Components.ComponentTypesTest do
use ExUnit.Case, async: true

alias Wasmex.Wasi.WasiP2Options

setup do
{:ok, store} = Wasmex.Components.Store.new()
component_bytes = File.read!("test/component_fixtures/component_types/component_types.wasm")
Expand Down Expand Up @@ -49,6 +51,18 @@ defmodule Wasm.Components.ComponentTypesTest do
])
end

test "record with kebab-field" do
{:ok, store} = Wasmex.Components.Store.new_wasi(%WasiP2Options{})
component_bytes = File.read!("test/component_fixtures/hello_world/hello_world.wasm")
{:ok, component} = Wasmex.Components.Component.new(store, component_bytes)
{:ok, instance} = Wasmex.Components.Instance.new(store, component)

assert {:ok, %{kebab_field: "foo"}} =
Wasmex.Components.Instance.call_function(instance, "echo-kebab", [
%{kebab_field: "foo"}
])
end

test "lists", %{instance: instance} do
assert {:ok, [1, 2, 3]} =
Wasmex.Components.Instance.call_function(instance, "id-list", [[1, 2, 3]])
Expand Down

0 comments on commit 277ab71

Please sign in to comment.