Skip to content

Commit

Permalink
Merge from upstream
Browse files Browse the repository at this point in the history
  • Loading branch information
jcburley committed Jan 2, 2022
2 parents 644d7f3 + dbf2f3d commit a731ef1
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 5 deletions.
2 changes: 1 addition & 1 deletion core/procs.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const (
PRINT_IF_NOT_NIL
)

const VERSION = "v0.17.3-gostd"
const VERSION = "v0.18.0-gostd"

const (
CLJ Dialect = iota
Expand Down
15 changes: 15 additions & 0 deletions docs/joker.json.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ <h2 id="_summary">Summary</h2>
<h2 id="_index">Index</h2>
<ul class="index">
<li>
<a class="var-kind Function" href="#json-seq">json-seq</a>
</li>
<li>
<a class="var-kind Function" href="#read-string">read-string</a>
</li>
<li>
Expand Down Expand Up @@ -62,6 +65,18 @@ <h2 id="_variables">Variables</h2>
<h2 id="_functions">Functions, Macros, and Special Forms</h2>
<ul>
<li>
<h3 class="Function" id="json-seq">json-seq</h3>
<span class="var-kind Function">Function</span>
<span class="var-added">v1.0</span>
<pre class="var-usage"><div><code>(json-seq rdr)</code></div>
<div><code>(json-seq rdr opts)</code></div>
</pre>
<p class="var-docstr">Returns the json records from rdr as a lazy sequence.<br>
rdr must be a string or implement io.Reader.<br>
Optional opts map may have the following keys:<br>
:keywords? - if true, JSON keys will be converted from strings to keywords.</p>
</li>
<li>
<h3 class="Function" id="read-string">read-string</h3>
<span class="var-kind Function">Function</span>
<span class="var-added">v1.0</span>
Expand Down
Binary file modified docs/joker.tgz
Binary file not shown.
2 changes: 1 addition & 1 deletion docs/joker.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<entry>
<version>0.17.2-gostd</version>
<version>0.18.0-gostd</version>
<url>https://burleyarch.com/joker/docs/joker.tgz</url>
</entry>
7 changes: 6 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/candid82/joker

go 1.14
go 1.17

require (
github.com/candid82/liner v1.4.0
Expand All @@ -10,3 +10,8 @@ require (
go.etcd.io/bbolt v1.3.6
gopkg.in/yaml.v2 v2.4.0
)

require (
github.com/mattn/go-runewidth v0.0.3 // indirect
golang.org/x/sys v0.0.0-20200923182605-d9f96fdee20d // indirect
)
11 changes: 10 additions & 1 deletion std/json.joke
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,13 @@
:go "writeString(v)"}
[^Object v])


(defn json-seq
"Returns the json records from rdr as a lazy sequence.
rdr must be a string or implement io.Reader.
Optional opts map may have the following keys:
:keywords? - if true, JSON keys will be converted from strings to keywords."
{:added "1.0"
:go {1 "jsonSeqOpts(rdr, EmptyArrayMap())"
2 "jsonSeqOpts(rdr, opts)"}}
([^Object rdr])
([^Object rdr ^Map opts]))
23 changes: 23 additions & 0 deletions std/json/a_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,29 @@ import (
. "github.com/candid82/joker/core"
)

var __json_seq__P ProcFn = __json_seq_
var json_seq_ Proc = Proc{Fn: __json_seq__P, Name: "json_seq_", Package: "std/json"}

func __json_seq_(_args []Object) Object {
_c := len(_args)
switch {
case _c == 1:
rdr := ExtractObject(_args, 0)
_res := jsonSeqOpts(rdr, EmptyArrayMap())
return _res

case _c == 2:
rdr := ExtractObject(_args, 0)
opts := ExtractMap(_args, 1)
_res := jsonSeqOpts(rdr, opts)
return _res

default:
PanicArity(_c)
}
return NIL
}

var __read_string__P ProcFn = __read_string_
var read_string_ Proc = Proc{Fn: __read_string__P, Name: "read_string_", Package: "std/json"}

Expand Down
8 changes: 8 additions & 0 deletions std/json/a_json_slow_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ func InternsOrThunks() {
}
jsonNamespace.ResetMeta(MakeMeta(nil, `Implements encoding and decoding of JSON as defined in RFC 4627.`, "1.0"))

jsonNamespace.InternVar("json-seq", json_seq_,
MakeMeta(
NewListFrom(NewVectorFrom(MakeSymbol("rdr")), NewVectorFrom(MakeSymbol("rdr"), MakeSymbol("opts"))),
`Returns the json records from rdr as a lazy sequence.
rdr must be a string or implement io.Reader.
Optional opts map may have the following keys:
:keywords? - if true, JSON keys will be converted from strings to keywords.`, "1.0"))

jsonNamespace.InternVar("read-string", read_string_,
MakeMeta(
NewListFrom(NewVectorFrom(MakeSymbol("s")), NewVectorFrom(MakeSymbol("s"), MakeSymbol("opts"))),
Expand Down
36 changes: 35 additions & 1 deletion std/json/json_native.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package json
import (
"encoding/json"
"fmt"

. "github.com/candid82/joker/core"
"io"
"strings"
)

func fromObject(obj Object) interface{} {
Expand Down Expand Up @@ -96,6 +97,39 @@ func readString(s string, opts Map) Object {
return toObject(v, keywordize)
}

func jsonSeqOpts(src Object, opts Map) Object {
var dec *json.Decoder
var keywordize bool
var jsonLazySeq func() *LazySeq
switch src := src.(type) {
case String:
dec = json.NewDecoder(strings.NewReader(src.S))
case io.Reader:
dec = json.NewDecoder(src)
default:
panic(RT.NewError("src must be a string or io.Reader"))
}
if opts != nil {
if ok, v := opts.Get(MakeKeyword("keywords?")); ok {
keywordize = ToBool(v)
}
}
jsonLazySeq = func() *LazySeq {
var c = func(args []Object) Object {
var o interface{}
err := dec.Decode(&o)
if err == io.EOF {
return EmptyList
}
PanicOnErr(err)
obj := toObject(o, keywordize)
return NewConsSeq(obj, jsonLazySeq())
}
return NewLazySeq(Proc{Fn: c})
}
return jsonLazySeq()
}

func writeString(obj Object) String {
res, err := json.Marshal(fromObject(obj))
if err != nil {
Expand Down

0 comments on commit a731ef1

Please sign in to comment.