Skip to content

Commit

Permalink
Add simple-json (andyarvanitis#7)
Browse files Browse the repository at this point in the history
Add simple-json (andyarvanitis#7)
  • Loading branch information
alvivi authored and andyarvanitis committed Sep 6, 2019
1 parent 31222f7 commit 7d1e72c
Show file tree
Hide file tree
Showing 9 changed files with 232 additions and 0 deletions.
17 changes: 17 additions & 0 deletions purescript-effect/Effect_Uncurried.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package purescript_effect

import (
. "github.com/purescript-native/go-runtime"
)

func init() {
exports := Foreign("Effect.Uncurried")

exports["runEffectFn1"] = func(fn Any) Any {
return func(a Any) Any {
return func() Any {
return Apply(fn, a)
}
}
}
}
13 changes: 13 additions & 0 deletions purescript-effect/Effect_Unsafe.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package purescript_effect

import (
. "github.com/purescript-native/go-runtime"
)

func init() {
exports := Foreign("Effect.Unsafe")

exports["unsafePerformEffect"] = func(f Any) Any {
return Run(f)
}
}
6 changes: 6 additions & 0 deletions purescript-exceptions/Effect_Exception.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package purescript_exceptions
import (
"errors"
"fmt"

. "github.com/purescript-native/go-runtime"
)

Expand All @@ -13,6 +14,11 @@ func init() {
return errors.New(msg.(string))
}

exports["message"] = func(e_ Any) Any {
e, _ := e_.(error)
return e.Error()
}

exports["throwException"] = func(e Any) Any {
return func() Any {
panic(e)
Expand Down
86 changes: 86 additions & 0 deletions purescript-foreign/Foreign.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package purescript_foreign

import (
. "github.com/purescript-native/go-runtime"
)

func init() {
exports := Foreign("Foreign")

exports["unsafeToForeign"] = func(value Any) Any {
return value
}

exports["unsafeFromForeign"] = func(value Any) Any {
return value
}

exports["typeOf"] = func(value Any) Any {
switch value.(type) {
case string:
return "string"

case float64:
return "number"

case int:
return "number"

case bool:
return "boolean"

case Dict:
return "object"

case Any:
return "object"

case nil:
return "object"

default:
return "undefined"
}
}

exports["tagOf"] = func(value Any) Any {
switch value.(type) {
case string:
return "String"

case float64:
return "Number"

case int:
return "Number"

case bool:
return "Boolean"

case Dict:
return "Object"

case Any:
return "Array"

case nil:
return "Null"

default:
return "Undefined"
}
}

exports["isNull"] = func(value Any) Any {
return value == nil
}

exports["isUndefined"] = func(value Any) Any {
return false
}

exports["isArray"] = func(value Any) Any {
_, ok := value.([]Any)
return ok
}
}
18 changes: 18 additions & 0 deletions purescript-foreign/Foreign_Index.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package purescript_foreign

import (
. "github.com/purescript-native/go-runtime"
)

func init() {
exports := Foreign("Foreign.Index")

exports["unsafeReadPropImpl"] = func(f, s, key_, value_ Any) Any {
value, ok := value_.(Dict)
if !ok {
return f
}
key, _ := key_.(string)
return Apply(s, value[key])
}
}
19 changes: 19 additions & 0 deletions purescript-globals/Global_Unsafe.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package purescript_global

import (
"encoding/json"

. "github.com/purescript-native/go-runtime"
)

func init() {
exports := Foreign("Global.Unsafe")

exports["unsafeStringify"] = func(value Any) Any {
blob, err := json.Marshal(value)
if err != nil {
panic(err.Error())
}
return string(blob)
}
}
22 changes: 22 additions & 0 deletions purescript-nullable/Data_Nullable.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package purescript_nullable

import (
. "github.com/purescript-native/go-runtime"
)

func init() {
exports := Foreign("Data.Nullable")

exports["null"] = nil

exports["nullable"] = func(a, r, f Any) Any {
if a == nil {
return r
}
return Apply(f, a)
}

exports["notNull"] = func(x Any) Any {
return x
}
}
29 changes: 29 additions & 0 deletions purescript-record/Record_Builder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package purescript_record

import (
. "github.com/purescript-native/go-runtime"
)

func init() {
exports := Foreign("Record.Builder")

exports["copyRecord"] = func(rec_ Any) Any {
rec, _ := rec_.(Dict)
cpy := make(Dict)
for key, value := range rec {
cpy[key] = value
}
return cpy
}

exports["unsafeInsert"] = func(l_ Any) Any {
return func(a Any) Any {
return func(rec_ Any) Any {
l, _ := l_.(string)
rec, _ := rec_.(Dict)
rec[l] = a
return rec
}
}
}
}
22 changes: 22 additions & 0 deletions purescript-simple-json/Simple_JSON.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package purescript_global

import (
"encoding/json"

. "github.com/purescript-native/go-runtime"
)

func init() {
exports := Foreign("Simple.JSON")

exports["_parseJSON"] = func(text_ Any) Any {
text, _ := text_.(string)
var result Any
if err := json.Unmarshal([]byte(text), &result); err != nil {
panic(err.Error())
}
return result
}

exports["_undefined"] = nil
}

0 comments on commit 7d1e72c

Please sign in to comment.