Skip to content

martint17r/encodingjs

Repository files navigation

golang-encodingjs

A JavaScript Unmarshaler for the Go language

Build Status Coverage Status

Usage

encodingjs.Unmarshal works similar to json.Unmarshal - pass the result of the JavaScript evaluation from otto and a reference to the target structure.

vm := otto.New()
jsresult, err := vm.Run(`a={Foo: "bar", Fubar:"bob"}; a`)
if err != nil {
	panic(err)
}
result := struct {
	Foo   string
	Fubar string
}{}
err= encodingjs.Unmarshal(jsresult, &result)
if err != nil {
	panic(err)
}
fmt.Printf("%+v\n", result)
// Output: {Foo:bar Fubar:bob}

Undefined vs. empty values

Except for pointers, it is not possible in go to differentiate between undefined and empty values. Implement the Unmarshaler interface to flag the difference if you need it.

Shortcomings

error path from otto is untested - I have not found a way to trigger these.

About

A JavaScript Unmarshaler for the Go language

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages