Skip to content

Latest commit

 

History

History
38 lines (29 loc) · 1.04 KB

README.md

File metadata and controls

38 lines (29 loc) · 1.04 KB

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.