Closed mertyildiran closed 1 year ago
Replacing int64
key type of the map
with string
as a workaround fixes the panic
but defining a JavaScript object literal like {1: "foo", 2: "bar"}
that integer as key is valid as far as I know. So, I believe ToValue
function should support it.
Thanks for reporting do you have a full test case for this?
@stevenh this is as close as I can get while being minimal:
package main
import (
"fmt"
"log"
"github.com/robertkrimen/otto"
)
type Something struct {
Foo string `json:"foo"`
Bar string `json:"bar"`
}
func main() {
// Panicing map
myMap := map[int64]map[string]*Something{
0: {
"a": &Something{
Foo: "foo",
Bar: "bar",
},
"b": &Something{
Foo: "foo",
Bar: "bar",
},
},
1: {
"a": &Something{
Foo: "foo",
Bar: "bar",
},
"b": &Something{
Foo: "foo",
Bar: "bar",
},
},
}
// This also panics
// myMap := map[int64]string{
// 0: "foo",
// 1: "bar",
// }
// Working map
// myMap := map[string]string{
// "a": "foo",
// "b": "bar",
// }
vm := otto.New()
value, err := vm.ToValue(myMap) // panics
if err != nil {
log.Println(err.Error())
} else {
fmt.Printf("value: %+v\n", value)
}
}
which returns a panic message for some reason: value: %!v(PANIC=String method: strconv.ParseInt: parsing "toString": invalid syntax)
I have a data structure:
map[int64]map[string]*Something
I'm trying to return that as a JavaScript object literal from a function:
I get the error below when the
example
function is called from JavaScript:Is
int64
type supported asmap
key? If not then can we add it?P.S. Couldn't use
otto.ToValue
because it saysmap
is not supported here.