Skip to content

Commit

Permalink
- updated JSFn function
Browse files Browse the repository at this point in the history
- updated docs
- added new JSONUnmarshal function for simple json unmarshaling
  • Loading branch information
rocketlaunchr-cto committed Aug 5, 2019
1 parent c1bd92d commit 888af85
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 5 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
32 changes: 28 additions & 4 deletions helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
19 changes: 19 additions & 0 deletions structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package react

import (
"errors"
"reflect"
"strings"

Expand Down Expand Up @@ -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
}

0 comments on commit 888af85

Please sign in to comment.