8000 Refactoring : builders & formatters by jfbus · Pull Request #1 · cocoonspace/dynjson · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Refactoring : builders & formatters #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2020 Cocoon Space

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE< 10000 /span>
SOFTWARE.
63 changes: 58 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# dynjson
User-customizable JSON formats for dynamic APIs.
Client-customizable JSON formats for dynamic APIs.

## Introduction

Expand All @@ -22,8 +22,6 @@ go get github.com/cocoon-space/dynjson

## Usage

Standard use :

```go
type APIResult struct {
Foo int `json:"foo"`
Expand All @@ -33,9 +31,64 @@ type APIResult struct {
f := dynjson.NewFormatter()

res := &APIResult{Foo:1, Bar:"bar"}
o, err := f.FormatObject(o, []string{"foo"}, nil)
o, err := f.Format(res, []string{"foo"}, nil)
if err != nil {
// handle error
}
err := json.Marshal(w, o) // {"foo": 1}
```
```

With slices:

```go
type APIResult struct {
Foo int `json:"foo"`
Bar string `json:"bar"`
}

f := dynjson.NewFormatter()

res := []APIResult{{Foo:1, Bar:"bar"}}
o, err := f.Format(res, []string{"foo"}, nil)
if err != nil {
// handle error
}
err := json.Marshal(w, o) // [{"foo": 1}]
```


```go
type APIResult struct {
Foo int `json:"foo"`
Bar []APIItem `json:"bar"`
}

type APIItem struct {
BarFoo int `json:"barfoo"`
BarBar string `json:"barbar"`
}

f := dynjson.NewFormatter()

res := &APIResult{Foo:1, Bar:[]APIItem{{BarFoo:1, BarBar: "bar"}}}
o, err := f.Format(res, []string{"foo", "bar.barfoo"}, nil)
if err != nil {
// handle error
}
err := json.Marshal(w, o) // {"foo": 1, "bar":[{"barfoo": 1}]}
```

# Performance impact

```
BenchmarkFormat_Fields
BenchmarkFormat_Fields-8 2466639 480 ns/op 184 B/op 7 allocs/op
BenchmarkFormat_NoFields
BenchmarkFormat_NoFields-8 5255031 232 ns/op 32 B/op 1 allocs/op
BenchmarkRawJSON
BenchmarkRawJSON-8 5351313 223 ns/op 32 B/op 1 allocs/op
```

# License

MIT - See LICENSE
30 changes: 30 additions & 0 deletions builder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package dynjson

import (
"reflect"
)

type builder interface {
build(fields []string) (formatter, error)
}

func makeBuilder(t reflect.Type) (builder, error) {
switch t.Kind() {
case reflect.Struct:
return makeStructBuilder(t)
case reflect.Ptr:
if t.Elem().Kind() == reflect.Struct {
return makePointerBuilder(t)
} else {
return makePrimitiveBuilder(t)
}
case reflect.Slice:
if t.Elem().Kind() == reflect.Struct {
return makeSliceBuilder(t)
} else {
return makePrimitiveBuilder(t)
}
default:
return makePrimitiveBuilder(t)
}
}
Loading
0