Skip to content

Commit

Permalink
make View and Redirect variadic, accept options in View
Browse files Browse the repository at this point in the history
  • Loading branch information
terrablue committed Nov 12, 2023
1 parent a89682e commit b77241f
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 27 deletions.
53 changes: 29 additions & 24 deletions primate/binding.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,47 +4,52 @@ import "syscall/js";
import "encoding/json";
import "fmt";

func Redirect1(location string) interface{} {
return js.FuncOf(func(this js.Value, args[] js.Value) interface{} {
return map[string]interface{}{
"handler": "redirect",
"location": location,
};
});
func tryposition(array []map[string]any, position uint8) map[string]any {
if (len(array) == int(position)) {
return map[string]any{};
}
return array[position];
}

func Redirect(location string, options map[string]interface{}) interface{} {
return js.FuncOf(func(this js.Value, args[] js.Value) interface{} {
return map[string]interface{}{
func serialize(data map[string]any) (string, error) {
serialized, err := json.Marshal(data);
if err != nil {
return "", err;
}
return string(serialized), nil;
}

func Redirect(location string, rest ...map[string]any) any {
options := tryposition(rest, 0);

return js.FuncOf(func(this js.Value, args[] js.Value) any {
return map[string]any{
"handler": "redirect",
"location": location,
"options": options,
};
});
}

func View1(component string) interface{} {
return js.FuncOf(func(this js.Value, args[] js.Value) interface{} {
return map[string]interface{}{
"handler": "view",
"component": component,
};
});
}
func View(component string, rest ...map[string]any) any {
props, err := serialize(tryposition(rest, 0));
if err != nil {
fmt.Println(err.Error());
return nil;
}

func View(component string, props map[string]interface{}) interface{} {
serialized, err := json.Marshal(props);
options, err := serialize(tryposition(rest, 1));
if err != nil {
fmt.Println(err.Error());
return nil;
}
stringified_props := string(serialized);

return js.FuncOf(func(this js.Value, args[] js.Value) interface{} {
return map[string]interface{}{
return js.FuncOf(func(this js.Value, args[] js.Value) any {
return map[string]any{
"handler": "view",
"component": component,
"props": stringified_props,
"props": props,
"options": options,
};
});
}
2 changes: 1 addition & 1 deletion test/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ go 1.21.3

replace github.com/primatejs/go/primate => ../primate

require github.com/primatejs/go/primate v0.0.0-20231105213429-e38c2a977313 // indirect
require github.com/primatejs/go/primate v0.0.0-20231107194614-a89682e78170 // indirect
1 change: 1 addition & 0 deletions test/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func Get(request Request) interface{} {
map[string]interface{}{"HO": "HI"},
},
});
return primate.View("test");
}

// {{{ start primate wrapper, postfix
Expand Down
7 changes: 5 additions & 2 deletions test/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ const typedArray = new Uint8Array(file);

const getter = {
get() {
return "bar";
},
getAll() {
return {foo: "bar"};
}
}
Expand All @@ -33,7 +36,7 @@ await WebAssembly.instantiate(typedArray, {
const get = globalThis.Get;
const response = make_response(get(make_request(fake_request(request))));
//const $response = typeof response === "object" ? JSON.stringify(response) : response;
console.log("T", response);
return new Response(response);
console.log("T", response, response());
return new Response(1);
}, {host: "0.0.0.0", port: 6161});
});
1 change: 1 addition & 0 deletions test/update-release.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
go get github.com/primatejs/go/primate@latest

0 comments on commit b77241f

Please sign in to comment.