-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* refactor: renamed json parsing error * feat: parse json package * fix: close closer if available * docs: zjson docs * docs: updated docs for json
- Loading branch information
Showing
12 changed files
with
146 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
--- | ||
sidebar_position: 3 | ||
sidebar_position: 4 | ||
--- | ||
|
||
# i18n | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
--- | ||
sidebar_position: 4 | ||
sidebar_position: 5 | ||
--- | ||
|
||
# zconst | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
--- | ||
sidebar_position: 2 | ||
sidebar_position: 3 | ||
--- | ||
|
||
# zenv | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
--- | ||
sidebar_position: 2 | ||
--- | ||
|
||
# zjson | ||
|
||
A very small package for using Zog schemas to parse json into structs. It exports a single function `Decode` which takes in an `io.Reader` or an `io.ReaderCloser` and returns the necessary structures for Zog to parse the json into a struct. This package is used by the `zhttp` package. | ||
|
||
```go | ||
import ( | ||
z "github.com/Oudwins/zog" | ||
"github.com/Oudwins/zog/parsers/zjson" | ||
"bytes" | ||
) | ||
var userSchema = z.Struct(z.Schema{ | ||
"name": z.String().Required(), | ||
"age": z.Int().Required().GT(18), | ||
}) | ||
type User struct { | ||
Name string | ||
Age int | ||
} | ||
|
||
func ParseJson(json []byte) { | ||
var user User | ||
errs := userSchema.Parse(zjson.Decode(bytes.NewReader(json)), &user) | ||
if errs != nil { | ||
// handle errors | ||
} | ||
user.Name // defined | ||
user.Age // defined | ||
} | ||
``` | ||
|
||
> **WARNING** The `zjson` package does NOT currently support parsing into any data type that is NOT a struct. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package zjson | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"io" | ||
|
||
p "github.com/Oudwins/zog/internals" | ||
"github.com/Oudwins/zog/zconst" | ||
) | ||
|
||
// func Unmarshal(data []byte) p.DpFactory { | ||
// return func() (p.DataProvider, p.ZogError) { | ||
// var m map[string]any | ||
// err := json.Unmarshal(data, &m) | ||
// if err != nil { | ||
// return nil, &p.ZogErr{C: zconst.ErrCodeInvalidJSON, Err: err} | ||
// } | ||
// if m == nil { | ||
// return nil, &p.ZogErr{C: zconst.ErrCodeInvalidJSON, Err: errors.New("nill json body")} | ||
// } | ||
// return p.NewMapDataProvider(m), nil | ||
// } | ||
// } | ||
|
||
// Decodes JSON data. Does not support json arrays or primitives | ||
/* | ||
- "null" -> nil -> Not accepted by zhttp -> errs["$root"]-> required error | ||
- "{}" -> okay -> map[]{} | ||
- "" -> parsing error -> errs["$root"]-> parsing error | ||
- "1213" -> zhttp -> plain value | ||
- struct schema -> hey this valid input | ||
- "string is not an object" | ||
*/ | ||
func Decode(r io.Reader) p.DpFactory { | ||
return func() (p.DataProvider, p.ZogError) { | ||
closer, ok := r.(io.Closer) | ||
if ok { | ||
defer closer.Close() | ||
} | ||
var m map[string]any | ||
decod := json.NewDecoder(r) | ||
err := decod.Decode(&m) | ||
if err != nil { | ||
return nil, &p.ZogErr{C: zconst.ErrCodeInvalidJSON, Err: err} | ||
} | ||
if m == nil { | ||
return nil, &p.ZogErr{C: zconst.ErrCodeInvalidJSON, Err: errors.New("nill json body")} | ||
} | ||
return p.NewMapDataProvider(m), nil | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters