Compare commits
103 Commits
Author | SHA1 | Date | |
---|---|---|---|
734dfbc3d2 | |||
4e33d996f1 | |||
f46030313a | |||
9fed45dd97 | |||
92a5a4ea20 | |||
|
76a318cd21 | ||
9ab8c6b134 | |||
|
55cfe34fea | ||
a3bf78f1ad | |||
|
f1d7d4eae6 | ||
f67ebf3663 | |||
|
daeb8f13f5 | ||
8c51b4c5d9 | |||
|
9fb608b7f7 | ||
|
c7fb358326 | ||
|
f737dbcbe3 | ||
6c6f1e978f | |||
|
80f9e3d74a | ||
11f6e624b8 | |||
|
9c12823d6d | ||
f19d5a8a93 | |||
|
4f024c4087 | ||
446bca7dc3 | |||
|
f2ef28fe98 | ||
18d2adb6be | |||
|
bd3400ff1b | ||
20f2a7c3ab | |||
|
a8a04200c9 | ||
|
51a7882d1c | ||
a6995c8136 | |||
|
fcb3d52d13 | ||
143a78568c | |||
|
965a23d8a2 | ||
422985ae49 | |||
|
1762b37f02 | ||
|
e9fb9d551b | ||
|
7c7f4c0d6d | ||
|
5a291a5684 | ||
|
a6a5541438 | ||
|
10aece0447 | ||
1c8f686bf1 | |||
|
5359e57547 | ||
8aed240a1e | |||
|
bbab9bd110 | ||
|
ff0742d7f0 | ||
d551abcdd6 | |||
|
8101905626 | ||
|
81843421c8 | ||
|
2d49a4b747 | ||
|
6888d48958 | ||
fa14ab6bb2 | |||
|
8d06a5ddae | ||
6bd7602769 | |||
|
b9657f6d10 | ||
3ca8e5b1a1 | |||
|
3b0b8a0f53 | ||
|
9ebdb6d000 | ||
d827a8f988 | |||
|
0bc29350df | ||
85d3f253c0 | |||
ff01411e1a | |||
|
11b09ce9e6 | ||
|
eefca6f6d3 | ||
|
3b27cbc57a | ||
d35698a5c3 | |||
|
58deb442af | ||
be9a18d988 | |||
|
bd0a53bf1a | ||
47c70c4043 | |||
|
4e680f1f76 | ||
67aae1aca4 | |||
|
1cd9716fb3 | ||
b33a027f07 | |||
|
b49cba9cb7 | ||
eeb549afb9 | |||
|
783e1a8f95 | ||
330febcb69 | |||
|
229a1218a8 | ||
89a7186cf6 | |||
|
ff3e9edb2a | ||
3974a3e2fa | |||
|
f633c6c5a6 | ||
4c80d0786c | |||
|
8b0ec24966 | ||
238b89e2e7 | |||
|
ede9e230a2 | ||
|
e0f9a615aa | ||
4615b86fb5 | |||
|
0249d6a441 | ||
|
457a6354b2 | ||
df2b455647 | |||
8332d628b4 | |||
cddee4ddb0 | |||
be6d3b806f | |||
c1d6120d48 | |||
|
13a567f1d1 | ||
|
34a66889ad | ||
286221984e | |||
|
b708c2c156 | ||
c16b395a17 | |||
1365e7e6cf | |||
4175c23a48 | |||
b1264f5094 |
19
.github/dependabot.yml
vendored
Normal file
19
.github/dependabot.yml
vendored
Normal file
@ -0,0 +1,19 @@
|
||||
# To get started with Dependabot version updates, you'll need to specify which
|
||||
# package ecosystems to update and where the package manifests are located.
|
||||
# Please see the documentation for all configuration options:
|
||||
# https://help.github.com/github/administering-a-repository/configuration-options-for-dependency-updates
|
||||
|
||||
version: 2
|
||||
updates:
|
||||
|
||||
# Maintain dependencies for GitHub Actions
|
||||
- package-ecosystem: "github-actions"
|
||||
directory: "/"
|
||||
schedule:
|
||||
interval: "daily"
|
||||
|
||||
# Maintain dependencies for Golang
|
||||
- package-ecosystem: "gomod"
|
||||
directory: "/"
|
||||
schedule:
|
||||
interval: "daily"
|
19
.github/renovate.json
vendored
19
.github/renovate.json
vendored
@ -1,19 +0,0 @@
|
||||
{
|
||||
"extends": [
|
||||
"config:base"
|
||||
],
|
||||
"packageRules": [
|
||||
{
|
||||
"matchUpdateTypes": ["minor", "patch", "pin", "digest"],
|
||||
"automerge": true
|
||||
},
|
||||
{
|
||||
"groupName": "all deps",
|
||||
"separateMajorMinor": true,
|
||||
"groupSlug": "all",
|
||||
"packagePatterns": [
|
||||
"*"
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
13
.github/stale.sh
vendored
13
.github/stale.sh
vendored
@ -1,13 +0,0 @@
|
||||
#!/bin/bash -ex
|
||||
|
||||
export PATH=$PATH:$(pwd)/bin
|
||||
export GO111MODULE=on
|
||||
export GOBIN=$(pwd)/bin
|
||||
|
||||
#go get github.com/rvflash/goup@v0.4.1
|
||||
|
||||
#goup -v ./...
|
||||
#go get github.com/psampaz/go-mod-outdated@v0.6.0
|
||||
go list -u -m -mod=mod -json all | go-mod-outdated -update -direct -ci || true
|
||||
|
||||
#go list -u -m -json all | go-mod-outdated -update
|
20
.github/workflows/autoapprove.yml
vendored
Normal file
20
.github/workflows/autoapprove.yml
vendored
Normal file
@ -0,0 +1,20 @@
|
||||
name: "autoapprove"
|
||||
|
||||
on:
|
||||
pull_request_target:
|
||||
types: [assigned, opened, synchronize, reopened]
|
||||
|
||||
permissions:
|
||||
pull-requests: write
|
||||
contents: write
|
||||
|
||||
jobs:
|
||||
autoapprove:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: approve
|
||||
uses: hmarr/auto-approve-action@v3
|
||||
if: github.actor == 'vtolstov' || github.actor == 'dependabot[bot]'
|
||||
id: approve
|
||||
with:
|
||||
github-token: ${{ secrets.GITHUB_TOKEN }}
|
21
.github/workflows/automerge.yml
vendored
Normal file
21
.github/workflows/automerge.yml
vendored
Normal file
@ -0,0 +1,21 @@
|
||||
name: "automerge"
|
||||
|
||||
on:
|
||||
pull_request_target:
|
||||
types: [assigned, opened, synchronize, reopened]
|
||||
|
||||
permissions:
|
||||
pull-requests: write
|
||||
contents: write
|
||||
|
||||
jobs:
|
||||
automerge:
|
||||
runs-on: ubuntu-latest
|
||||
if: github.actor == 'vtolstov'
|
||||
steps:
|
||||
- name: merge
|
||||
id: merge
|
||||
run: gh pr merge --auto --merge "$PR_URL"
|
||||
env:
|
||||
PR_URL: ${{github.event.pull_request.html_url}}
|
||||
GITHUB_TOKEN: ${{secrets.TOKEN}}
|
13
.github/workflows/build.yml
vendored
13
.github/workflows/build.yml
vendored
@ -3,19 +3,20 @@ on:
|
||||
push:
|
||||
branches:
|
||||
- master
|
||||
- v3
|
||||
jobs:
|
||||
test:
|
||||
name: test
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: setup
|
||||
uses: actions/setup-go@v2
|
||||
uses: actions/setup-go@v3
|
||||
with:
|
||||
go-version: 1.16
|
||||
go-version: 1.17
|
||||
- name: checkout
|
||||
uses: actions/checkout@v2
|
||||
uses: actions/checkout@v3
|
||||
- name: cache
|
||||
uses: actions/cache@v2
|
||||
uses: actions/cache@v3
|
||||
with:
|
||||
path: ~/go/pkg/mod
|
||||
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
|
||||
@ -31,9 +32,9 @@ jobs:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: checkout
|
||||
uses: actions/checkout@v2
|
||||
uses: actions/checkout@v3
|
||||
- name: lint
|
||||
uses: golangci/golangci-lint-action@v2
|
||||
uses: golangci/golangci-lint-action@v3.4.0
|
||||
continue-on-error: true
|
||||
with:
|
||||
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
|
||||
|
78
.github/workflows/codeql-analysis.yml
vendored
Normal file
78
.github/workflows/codeql-analysis.yml
vendored
Normal file
@ -0,0 +1,78 @@
|
||||
# For most projects, this workflow file will not need changing; you simply need
|
||||
# to commit it to your repository.
|
||||
#
|
||||
# You may wish to alter this file to override the set of languages analyzed,
|
||||
# or to provide custom queries or build logic.
|
||||
#
|
||||
# ******** NOTE ********
|
||||
# We have attempted to detect the languages in your repository. Please check
|
||||
# the `language` matrix defined below to confirm you have the correct set of
|
||||
# supported CodeQL languages.
|
||||
#
|
||||
name: "codeql"
|
||||
|
||||
on:
|
||||
workflow_run:
|
||||
workflows: ["prbuild"]
|
||||
types:
|
||||
- completed
|
||||
push:
|
||||
branches: [ master, v3 ]
|
||||
pull_request:
|
||||
# The branches below must be a subset of the branches above
|
||||
branches: [ master, v3 ]
|
||||
schedule:
|
||||
- cron: '34 1 * * 0'
|
||||
|
||||
jobs:
|
||||
analyze:
|
||||
name: analyze
|
||||
runs-on: ubuntu-latest
|
||||
permissions:
|
||||
actions: read
|
||||
contents: read
|
||||
security-events: write
|
||||
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
language: [ 'go' ]
|
||||
# CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python' ]
|
||||
# Learn more:
|
||||
# https://docs.github.com/en/free-pro-team@latest/github/finding-security-vulnerabilities-and-errors-in-your-code/configuring-code-scanning#changing-the-languages-that-are-analyzed
|
||||
|
||||
steps:
|
||||
- name: checkout
|
||||
uses: actions/checkout@v3
|
||||
- name: setup
|
||||
uses: actions/setup-go@v3
|
||||
with:
|
||||
go-version: 1.17
|
||||
# Initializes the CodeQL tools for scanning.
|
||||
- name: init
|
||||
uses: github/codeql-action/init@v2
|
||||
with:
|
||||
languages: ${{ matrix.language }}
|
||||
# If you wish to specify custom queries, you can do so here or in a config file.
|
||||
# By default, queries listed here will override any specified in a config file.
|
||||
# Prefix the list here with "+" to use these queries and those in the config file.
|
||||
# queries: ./path/to/local/query, your-org/your-repo/queries@main
|
||||
|
||||
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
|
||||
# If this step fails, then you should remove it and run the build manually (see below)
|
||||
- name: autobuild
|
||||
uses: github/codeql-action/autobuild@v2
|
||||
|
||||
# ℹ️ Command-line programs to run using the OS shell.
|
||||
# 📚 https://git.io/JvXDl
|
||||
|
||||
# ✏️ If the Autobuild fails above, remove it and uncomment the following three lines
|
||||
# and modify them (or add more) to build your code if your project
|
||||
# uses a compiled language
|
||||
|
||||
#- run: |
|
||||
# make bootstrap
|
||||
# make release
|
||||
|
||||
- name: analyze
|
||||
uses: github/codeql-action/analyze@v2
|
27
.github/workflows/dependabot-automerge.yml
vendored
Normal file
27
.github/workflows/dependabot-automerge.yml
vendored
Normal file
@ -0,0 +1,27 @@
|
||||
name: "dependabot-automerge"
|
||||
|
||||
on:
|
||||
pull_request_target:
|
||||
types: [assigned, opened, synchronize, reopened]
|
||||
|
||||
permissions:
|
||||
pull-requests: write
|
||||
contents: write
|
||||
|
||||
jobs:
|
||||
automerge:
|
||||
runs-on: ubuntu-latest
|
||||
if: github.actor == 'dependabot[bot]'
|
||||
steps:
|
||||
- name: metadata
|
||||
id: metadata
|
||||
uses: dependabot/fetch-metadata@v1.3.6
|
||||
with:
|
||||
github-token: "${{ secrets.TOKEN }}"
|
||||
- name: merge
|
||||
id: merge
|
||||
if: ${{contains(steps.metadata.outputs.dependency-names, 'go.unistack.org')}}
|
||||
run: gh pr merge --auto --merge "$PR_URL"
|
||||
env:
|
||||
PR_URL: ${{github.event.pull_request.html_url}}
|
||||
GITHUB_TOKEN: ${{secrets.TOKEN}}
|
13
.github/workflows/pr.yml
vendored
13
.github/workflows/pr.yml
vendored
@ -3,19 +3,20 @@ on:
|
||||
pull_request:
|
||||
branches:
|
||||
- master
|
||||
- v3
|
||||
jobs:
|
||||
test:
|
||||
name: test
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: setup
|
||||
uses: actions/setup-go@v2
|
||||
uses: actions/setup-go@v3
|
||||
with:
|
||||
go-version: 1.16
|
||||
go-version: 1.17
|
||||
- name: checkout
|
||||
uses: actions/checkout@v2
|
||||
uses: actions/checkout@v3
|
||||
- name: cache
|
||||
uses: actions/cache@v2
|
||||
uses: actions/cache@v3
|
||||
with:
|
||||
path: ~/go/pkg/mod
|
||||
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
|
||||
@ -31,9 +32,9 @@ jobs:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: checkout
|
||||
uses: actions/checkout@v2
|
||||
uses: actions/checkout@v3
|
||||
- name: lint
|
||||
uses: golangci/golangci-lint-action@v2
|
||||
uses: golangci/golangci-lint-action@v3.4.0
|
||||
continue-on-error: true
|
||||
with:
|
||||
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
|
||||
|
@ -3,8 +3,40 @@ package json
|
||||
import (
|
||||
"bytes"
|
||||
"testing"
|
||||
|
||||
"go.unistack.org/micro/v4/codec"
|
||||
)
|
||||
|
||||
func TestFrame(t *testing.T) {
|
||||
s := &codec.Frame{Data: []byte("test")}
|
||||
|
||||
buf, err := NewCodec().Marshal(s)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !bytes.Equal(buf, []byte(`test`)) {
|
||||
t.Fatalf("bytes not equal %s != %s", buf, `test`)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFrameFlatten(t *testing.T) {
|
||||
s := &struct {
|
||||
One string
|
||||
Name *codec.Frame `json:"name" codec:"flatten"`
|
||||
}{
|
||||
One: "xx",
|
||||
Name: &codec.Frame{Data: []byte("test")},
|
||||
}
|
||||
|
||||
buf, err := NewCodec().Marshal(s)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !bytes.Equal(buf, []byte(`test`)) {
|
||||
t.Fatalf("bytes not equal %s != %s", buf, `test`)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStructByTag(t *testing.T) {
|
||||
type Str struct {
|
||||
Name []string `json:"name" codec:"flatten"`
|
||||
@ -18,7 +50,7 @@ func TestStructByTag(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !bytes.Equal(buf, []byte(`["first","second"]`)) {
|
||||
t.Fatalf("invalid marshal: %s", buf)
|
||||
t.Fatalf("invalid marshal: %v != %v", buf, []byte(`["first","second"]`))
|
||||
}
|
||||
|
||||
err = c.Unmarshal([]byte(`["1","2"]`), val)
|
||||
@ -29,7 +61,6 @@ func TestStructByTag(t *testing.T) {
|
||||
if len(val.Name) != 2 {
|
||||
t.Fatalf("invalid unmarshal: %v", val)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestReadBody(t *testing.T) {
|
||||
|
11
go.mod
11
go.mod
@ -1,5 +1,10 @@
|
||||
module github.com/unistack-org/micro-codec-json/v3
|
||||
module go.unistack.org/micro-codec-json/v4
|
||||
|
||||
go 1.16
|
||||
go 1.19
|
||||
|
||||
require github.com/unistack-org/micro/v3 v3.3.19
|
||||
require (
|
||||
go.unistack.org/micro-proto/v4 v4.0.0
|
||||
go.unistack.org/micro/v4 v4.0.1
|
||||
)
|
||||
|
||||
require google.golang.org/protobuf v1.28.1 // indirect
|
||||
|
28
go.sum
28
go.sum
@ -1,16 +1,12 @@
|
||||
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
|
||||
github.com/ef-ds/deque v1.0.4/go.mod h1:gXDnTC3yqvBcHbq2lcExjtAcVrOnJCbMcZXmuj8Z4tg=
|
||||
github.com/google/uuid v1.2.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
github.com/imdario/mergo v0.3.12/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA=
|
||||
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
|
||||
github.com/silas/dag v0.0.0-20210121180416-41cf55125c34/go.mod h1:7RTUFBdIRC9nZ7/3RyRNH1bdqIShrDejd1YbLwgPS+I=
|
||||
github.com/unistack-org/micro/v3 v3.3.19 h1:x0bl4H+IZvdHHKfA8qMcox9W92+e7rtbqgRfQPoP79w=
|
||||
github.com/unistack-org/micro/v3 v3.3.19/go.mod h1:LXmPfbJnJNvL0kQs8HfnkV3Wya2Wb+C7keVq++RCZnk=
|
||||
golang.org/x/net v0.0.0-20210510120150-4163338589ed/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
|
||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
|
||||
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
|
||||
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||
go.unistack.org/micro-proto/v4 v4.0.0 h1:+TAwKSlx48eLsNO2E2cO0JLE8we1KU2AuWe9Q310FuU=
|
||||
go.unistack.org/micro-proto/v4 v4.0.0/go.mod h1:ArmK7o+uFvxSY3dbJhKBBX4Pm1rhWdLEFf3LxBrMtec=
|
||||
go.unistack.org/micro/v4 v4.0.1 h1:xo1IxbVfgh8i0eY0VeYa3cbb13u5n/Mxnp3FOgWD4Jo=
|
||||
go.unistack.org/micro/v4 v4.0.1/go.mod h1:p/J5UcSJjfHsWGT31uKoghQ5rUQZzQJBAFy+Z4+ZVMs=
|
||||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
|
||||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
|
||||
google.golang.org/protobuf v1.28.1 h1:d0NfwRgPtno5B1Wa6L2DAG+KivqkdutMf1UhdNx175w=
|
||||
google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
|
||||
|
147
json.go
147
json.go
@ -1,49 +1,124 @@
|
||||
// Package json provides a json codec
|
||||
package json
|
||||
package json // import "go.unistack.org/micro-codec-json/v4"
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"io"
|
||||
|
||||
"github.com/unistack-org/micro/v3/codec"
|
||||
rutil "github.com/unistack-org/micro/v3/util/reflect"
|
||||
pb "go.unistack.org/micro-proto/v4/codec"
|
||||
"go.unistack.org/micro/v4/codec"
|
||||
rutil "go.unistack.org/micro/v4/util/reflect"
|
||||
)
|
||||
|
||||
type jsonCodec struct{}
|
||||
var (
|
||||
DefaultMarshalOptions = JsonMarshalOptions{
|
||||
EscapeHTML: true,
|
||||
}
|
||||
|
||||
DefaultUnmarshalOptions = JsonUnmarshalOptions{
|
||||
DisallowUnknownFields: false,
|
||||
UseNumber: false,
|
||||
}
|
||||
)
|
||||
|
||||
type JsonMarshalOptions struct {
|
||||
EscapeHTML bool
|
||||
}
|
||||
|
||||
type JsonUnmarshalOptions struct {
|
||||
DisallowUnknownFields bool
|
||||
UseNumber bool
|
||||
}
|
||||
|
||||
type jsonCodec struct {
|
||||
opts codec.Options
|
||||
}
|
||||
|
||||
const (
|
||||
flattenTag = "flatten"
|
||||
)
|
||||
|
||||
func (c *jsonCodec) Marshal(v interface{}) ([]byte, error) {
|
||||
switch m := v.(type) {
|
||||
case nil:
|
||||
func (c *jsonCodec) Marshal(v interface{}, opts ...codec.Option) ([]byte, error) {
|
||||
if v == nil {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
options := c.opts
|
||||
for _, o := range opts {
|
||||
o(&options)
|
||||
}
|
||||
|
||||
if nv, err := rutil.StructFieldByTag(v, options.TagName, flattenTag); err == nil {
|
||||
v = nv
|
||||
}
|
||||
|
||||
switch m := v.(type) {
|
||||
case *codec.Frame:
|
||||
return m.Data, nil
|
||||
case *pb.Frame:
|
||||
return m.Data, nil
|
||||
}
|
||||
|
||||
if nv, err := rutil.StructFieldByTag(v, codec.DefaultTagName, flattenTag); err == nil {
|
||||
return json.Marshal(nv)
|
||||
marshalOptions := DefaultMarshalOptions
|
||||
if options.Context != nil {
|
||||
if f, ok := options.Context.Value(marshalOptionsKey{}).(JsonMarshalOptions); ok {
|
||||
marshalOptions = f
|
||||
}
|
||||
}
|
||||
|
||||
if !marshalOptions.EscapeHTML {
|
||||
w := bytes.NewBuffer(nil)
|
||||
enc := json.NewEncoder(w)
|
||||
enc.SetEscapeHTML(marshalOptions.EscapeHTML)
|
||||
err := enc.Encode(v)
|
||||
buf := w.Bytes()
|
||||
return buf[:len(buf)-1], err
|
||||
}
|
||||
|
||||
return json.Marshal(v)
|
||||
}
|
||||
|
||||
func (c *jsonCodec) Unmarshal(b []byte, v interface{}) error {
|
||||
if len(b) == 0 {
|
||||
func (c *jsonCodec) Unmarshal(b []byte, v interface{}, opts ...codec.Option) error {
|
||||
if len(b) == 0 || v == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
options := c.opts
|
||||
for _, o := range opts {
|
||||
o(&options)
|
||||
}
|
||||
|
||||
if nv, err := rutil.StructFieldByTag(v, options.TagName, flattenTag); err == nil {
|
||||
v = nv
|
||||
}
|
||||
|
||||
switch m := v.(type) {
|
||||
case nil:
|
||||
return nil
|
||||
case *codec.Frame:
|
||||
m.Data = b
|
||||
return nil
|
||||
case *pb.Frame:
|
||||
m.Data = b
|
||||
return nil
|
||||
}
|
||||
|
||||
if nv, err := rutil.StructFieldByTag(v, codec.DefaultTagName, flattenTag); err == nil {
|
||||
return json.Unmarshal(b, nv)
|
||||
unmarshalOptions := DefaultUnmarshalOptions
|
||||
if options.Context != nil {
|
||||
if f, ok := options.Context.Value(unmarshalOptionsKey{}).(JsonUnmarshalOptions); ok {
|
||||
unmarshalOptions = f
|
||||
}
|
||||
}
|
||||
|
||||
if unmarshalOptions.DisallowUnknownFields || unmarshalOptions.UseNumber {
|
||||
dec := json.NewDecoder(bytes.NewBuffer(b))
|
||||
if unmarshalOptions.DisallowUnknownFields {
|
||||
dec.DisallowUnknownFields()
|
||||
}
|
||||
if unmarshalOptions.UseNumber {
|
||||
dec.UseNumber()
|
||||
}
|
||||
|
||||
return dec.Decode(v)
|
||||
}
|
||||
|
||||
return json.Unmarshal(b, v)
|
||||
@ -54,50 +129,34 @@ func (c *jsonCodec) ReadHeader(conn io.Reader, m *codec.Message, t codec.Message
|
||||
}
|
||||
|
||||
func (c *jsonCodec) ReadBody(conn io.Reader, v interface{}) error {
|
||||
switch m := v.(type) {
|
||||
case nil:
|
||||
if v == nil {
|
||||
return nil
|
||||
case *codec.Frame:
|
||||
}
|
||||
|
||||
buf, err := io.ReadAll(conn)
|
||||
if err != nil {
|
||||
return err
|
||||
} else if len(buf) == 0 {
|
||||
return nil
|
||||
}
|
||||
m.Data = buf
|
||||
return nil
|
||||
}
|
||||
|
||||
var err error
|
||||
if nv, nerr := rutil.StructFieldByTag(v, codec.DefaultTagName, flattenTag); nerr == nil {
|
||||
err = json.NewDecoder(conn).Decode(nv)
|
||||
} else {
|
||||
err = json.NewDecoder(conn).Decode(v)
|
||||
v = nv
|
||||
}
|
||||
|
||||
if err == io.EOF {
|
||||
return nil
|
||||
}
|
||||
|
||||
return err
|
||||
return c.Unmarshal(buf, v)
|
||||
}
|
||||
|
||||
func (c *jsonCodec) Write(conn io.Writer, m *codec.Message, v interface{}) error {
|
||||
switch m := v.(type) {
|
||||
case nil:
|
||||
if v == nil {
|
||||
return nil
|
||||
case *codec.Frame:
|
||||
_, err := conn.Write(m.Data)
|
||||
}
|
||||
|
||||
buf, err := c.Marshal(v)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var err error
|
||||
if nv, nerr := rutil.StructFieldByTag(v, codec.DefaultTagName, flattenTag); nerr == nil {
|
||||
err = json.NewEncoder(conn).Encode(nv)
|
||||
} else {
|
||||
err = json.NewEncoder(conn).Encode(v)
|
||||
}
|
||||
|
||||
_, err = conn.Write(buf)
|
||||
return err
|
||||
}
|
||||
|
||||
@ -105,6 +164,6 @@ func (c *jsonCodec) String() string {
|
||||
return "json"
|
||||
}
|
||||
|
||||
func NewCodec() codec.Codec {
|
||||
return &jsonCodec{}
|
||||
func NewCodec(opts ...codec.Option) codec.Codec {
|
||||
return &jsonCodec{opts: codec.NewOptions(opts...)}
|
||||
}
|
||||
|
17
options.go
Normal file
17
options.go
Normal file
@ -0,0 +1,17 @@
|
||||
package json
|
||||
|
||||
import (
|
||||
codec "go.unistack.org/micro/v4/codec"
|
||||
)
|
||||
|
||||
type unmarshalOptionsKey struct{}
|
||||
|
||||
func UnmarshalOptions(o JsonUnmarshalOptions) codec.Option {
|
||||
return codec.SetOption(unmarshalOptionsKey{}, o)
|
||||
}
|
||||
|
||||
type marshalOptionsKey struct{}
|
||||
|
||||
func MarshalOptions(o JsonMarshalOptions) codec.Option {
|
||||
return codec.SetOption(marshalOptionsKey{}, o)
|
||||
}
|
Loading…
Reference in New Issue
Block a user