diff --git a/README.md b/README.md index 6a92704..f6d92f7 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,15 @@ Go with React [![GoDoc](http://godoc.org/github.com/rocketlaunchr/react?status.s Facebook's React is one of the most dominant libraries for front-end development around. Google's Go programming language is one of the most elegantly crafted languages for server development. Why not combine the two? -This package is an extremely thin wrapper over the native react.js API. The objective was to make it light-weight, developer-friendly and intuitive. You shouldn’t have to scour the documentation to get going — a few peeks should be adequate. If you know your way around the React API and you know a bit of Go, then you should be able to make prototypes and production-worthy applications in no time. +This package is an extremely thin wrapper over the native react.js API. The objective was to make it light-weight, developer-friendly and intuitive. You shouldn’t have to scour the documentation to get going — a few peeks should be adequate. If you know your way around the React API and you know a bit of Go, then you should be able to make prototypes and production-worthy applications in no time. + +This package is best suited for making Desktop applications using these technologies: +* [webview](https://github.com/zserge/webview) +* [lorca](https://github.com/zserge/lorca) +* [go-astilectron](https://github.com/asticode/go-astilectron) +* [gotron](https://github.com/Equanox/gotron) +* **[electron.js](https://electronjs.org/)** + The package is **production ready**. An optional (but highly convenient) `elements` sub-package is also included. diff --git a/helpers.go b/helpers.go index 33add3f..1f41fab 100644 --- a/helpers.go +++ b/helpers.go @@ -32,10 +32,34 @@ func JSX(component interface{}, props interface{}, children ...interface{}) *js. return React.Call("createElement", args...) } -// JSFn is a convenience function used to call javascript functions that are -// part of the standard library. -func JSFn(name string, args ...interface{}) *js.Object { - return js.Global.Call(name, args...) +// JSFn is a convenience function used to call javascript native functions. +// If the native function throws an exception, then a *js.Error is returned. +// +// Example: +// +// JSFn(nil, "alert", "Hello World!") // alert('Hello World!') +// JSFn("JSON", "parse", `{"name":"John"}`) // JSON.parse('{"name":"John"}') +// +func JSFn(obj interface{}, funcName string, args ...interface{}) (_ *js.Object, rErr error) { + defer func() { + if e := recover(); e != nil { + err, ok := e.(*js.Error) + if !ok { + panic(e) + } + rErr = err + } + }() + + switch v := obj.(type) { + case nil: + return js.Global.Call(funcName, args...), nil + case string: + return js.Global.Get(v).Call(funcName, args...), nil + default: + _ = obj.(string) // deliberately panic + } + return nil, nil } // CreateRef will create a Ref. diff --git a/structs.go b/structs.go index 169ff77..5d1a215 100644 --- a/structs.go +++ b/structs.go @@ -3,6 +3,7 @@ package react import ( + "errors" "reflect" "strings" @@ -216,3 +217,21 @@ func HydrateProps(this *js.Object, strct interface{}) error { func HydrateState(this *js.Object, strct interface{}) error { return UnmarshalState(this, strct) } + +// JSONUnmarshal provides a simple way to unmarshal json encoded strings to structs. +// +// See: https://github.com/gopherjs/gopherjs/wiki/Using-native-JSON-parsing-to-realize-a-slim-JSON-decoder +// for a tutorial with a example. +func JSONUnmarshal(json string) (*js.Object, error) { + + obj, err := JSFn("JSON", "parse", json) + if err != nil { + return nil, err + } + + if obj == nil { + return nil, errors.New("JSONUnmarshal: something went wrong.") + } + + return obj, nil +}