Compare commits
119 Commits
Author | SHA1 | Date | |
---|---|---|---|
b67dcb9b12 | |||
|
83f1ba8127 | ||
aec813b3cd | |||
0418530eb2 | |||
4629c378dc | |||
8fc8af418b | |||
86c6e2e006 | |||
185449b5b0 | |||
7b50d918cd | |||
|
d8179f0be1 | ||
76254c1ed3 | |||
|
854bcefd10 | ||
d1d9b4a326 | |||
|
c814d0d0f7 | ||
b6edce9800 | |||
|
95a31d6c40 | ||
84ee05886d | |||
|
7c276416b0 | ||
bdbc322132 | |||
|
5d1ea4a86f | ||
f119f97020 | |||
|
3e54b2c1ef | ||
|
7a64bedab4 | ||
|
4b5849ae5b | ||
8702cd6fd8 | |||
|
45ee75730c | ||
03cc0fa01f | |||
|
6f8b7d98a9 | ||
c4b74c301d | |||
|
6e774e5e9a | ||
298fb5e2b7 | |||
|
02041ee927 | ||
31e7fd2de0 | |||
|
b2f2c1ff67 | ||
b859dec684 | |||
|
07b176d757 | ||
bc4b60cf93 | |||
|
1524c4b5cc | ||
|
a8643e60bb | ||
399a408099 | |||
|
6457e00aa5 | ||
a7bc319f9e | |||
|
96d957afe1 | ||
|
11b962d6dd | ||
|
1f8fb20c2d | ||
|
96ae6cab79 | ||
|
8b92293d23 | ||
|
a109b5f091 | ||
57e65ad357 | |||
|
539c638b23 | ||
ec5d9018d3 | |||
|
168280923a | ||
|
e6a81fcd1c | ||
8a481a4807 | |||
|
534c481166 | ||
|
dea6a66f85 | ||
|
bdc01f3f97 | ||
512345ab76 | |||
|
5939dfe58e | ||
1ea4783e90 | |||
|
325f960bc3 | ||
b67e8c6678 | |||
|
6fc1caa836 | ||
8b89d788b6 | |||
|
5f8ebd3e63 | ||
ba4ff82b2e | |||
|
bb75aae8de | ||
fddcd6ed99 | |||
|
6aace39310 | ||
52a9c67634 | |||
|
7feab14caf | ||
d280f6c9cb | |||
5057e0193e | |||
e146cf31a7 | |||
|
a3966dadfa | ||
632be82ca3 | |||
|
4c7a46c69f | ||
ac08d033c7 | |||
|
a1294521f0 | ||
990f72a998 | |||
|
159205b5f3 | ||
7a252b48a4 | |||
|
0055a78179 | ||
823e5b5fbb | |||
|
daeff6e5bb | ||
2e63ea772b | |||
|
cc98e26597 | ||
0e8ccccfa1 | |||
|
616eb8f742 | ||
848e168897 | |||
|
61e541bba3 | ||
7b8e26998e | |||
|
3cb1b6672f | ||
a56c89647f | |||
|
e146cc590e | ||
a7e0059ba2 | |||
|
39be7d79f5 | ||
3ba5c31e66 | |||
|
ea135d1b0f | ||
639f687e7a | |||
27cc4bbec2 | |||
04d65d16e5 | |||
|
73944dbc44 | ||
|
b247a89b71 | ||
dd93a0da3b | |||
|
6ab7ec4b8f | ||
|
17d3ba3715 | ||
cfd576d197 | |||
047c791f3b | |||
d573acae5c | |||
b74a1be380 | |||
|
407da19f9a | ||
|
7e49a3a83f | ||
932ab847a0 | |||
|
7bfd0085dd | ||
|
268f11fc0e | ||
bbccaae75e | |||
6e8ac3c23e | |||
a04f4446ba |
29
.gitea/workflows/job_lint.yml
Normal file
29
.gitea/workflows/job_lint.yml
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
name: lint
|
||||||
|
|
||||||
|
on:
|
||||||
|
pull_request:
|
||||||
|
types: [opened, reopened, synchronize]
|
||||||
|
branches:
|
||||||
|
- master
|
||||||
|
- v3
|
||||||
|
- v4
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
lint:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- name: checkout code
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
with:
|
||||||
|
filter: 'blob:none'
|
||||||
|
- name: setup go
|
||||||
|
uses: actions/setup-go@v5
|
||||||
|
with:
|
||||||
|
cache-dependency-path: "**/*.sum"
|
||||||
|
go-version: 'stable'
|
||||||
|
- name: setup deps
|
||||||
|
run: go get -v ./...
|
||||||
|
- name: run lint
|
||||||
|
uses: https://github.com/golangci/golangci-lint-action@v6
|
||||||
|
with:
|
||||||
|
version: 'latest'
|
34
.gitea/workflows/job_test.yml
Normal file
34
.gitea/workflows/job_test.yml
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
name: test
|
||||||
|
|
||||||
|
on:
|
||||||
|
pull_request:
|
||||||
|
types: [opened, reopened, synchronize]
|
||||||
|
branches:
|
||||||
|
- master
|
||||||
|
- v3
|
||||||
|
- v4
|
||||||
|
push:
|
||||||
|
branches:
|
||||||
|
- master
|
||||||
|
- v3
|
||||||
|
- v4
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
test:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- name: checkout code
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
with:
|
||||||
|
filter: 'blob:none'
|
||||||
|
- name: setup go
|
||||||
|
uses: actions/setup-go@v5
|
||||||
|
with:
|
||||||
|
cache-dependency-path: "**/*.sum"
|
||||||
|
go-version: 'stable'
|
||||||
|
- name: setup deps
|
||||||
|
run: go get -v ./...
|
||||||
|
- name: run test
|
||||||
|
env:
|
||||||
|
INTEGRATION_TESTS: yes
|
||||||
|
run: go test -mod readonly -v ./...
|
53
.gitea/workflows/job_tests.yml
Normal file
53
.gitea/workflows/job_tests.yml
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
name: test
|
||||||
|
|
||||||
|
on:
|
||||||
|
pull_request:
|
||||||
|
types: [opened, reopened, synchronize]
|
||||||
|
branches:
|
||||||
|
- master
|
||||||
|
- v3
|
||||||
|
- v4
|
||||||
|
push:
|
||||||
|
branches:
|
||||||
|
- master
|
||||||
|
- v3
|
||||||
|
- v4
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
test:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- name: checkout code
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
with:
|
||||||
|
filter: 'blob:none'
|
||||||
|
- name: checkout tests
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
with:
|
||||||
|
ref: master
|
||||||
|
filter: 'blob:none'
|
||||||
|
repository: unistack-org/micro-tests
|
||||||
|
path: micro-tests
|
||||||
|
- name: setup go
|
||||||
|
uses: actions/setup-go@v5
|
||||||
|
with:
|
||||||
|
cache-dependency-path: "**/*.sum"
|
||||||
|
go-version: 'stable'
|
||||||
|
- name: setup go work
|
||||||
|
env:
|
||||||
|
GOWORK: /workspace/${{ github.repository_owner }}/go.work
|
||||||
|
run: |
|
||||||
|
go work init
|
||||||
|
go work use .
|
||||||
|
go work use micro-tests
|
||||||
|
- name: setup deps
|
||||||
|
env:
|
||||||
|
GOWORK: /workspace/${{ github.repository_owner }}/go.work
|
||||||
|
run: go get -v ./...
|
||||||
|
- name: run tests
|
||||||
|
env:
|
||||||
|
INTEGRATION_TESTS: yes
|
||||||
|
GOWORK: /workspace/${{ github.repository_owner }}/go.work
|
||||||
|
run: |
|
||||||
|
cd micro-tests
|
||||||
|
go test -mod readonly -v ./... || true
|
20
.github/renovate.json
vendored
20
.github/renovate.json
vendored
@ -1,20 +0,0 @@
|
|||||||
{
|
|
||||||
"extends": [
|
|
||||||
"config:base"
|
|
||||||
],
|
|
||||||
"postUpdateOptions": ["gomodTidy"],
|
|
||||||
"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
|
|
46
.github/workflows/build.yml
vendored
46
.github/workflows/build.yml
vendored
@ -1,46 +0,0 @@
|
|||||||
name: build
|
|
||||||
on:
|
|
||||||
push:
|
|
||||||
branches:
|
|
||||||
- master
|
|
||||||
jobs:
|
|
||||||
test:
|
|
||||||
name: test
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- name: setup
|
|
||||||
uses: actions/setup-go@v2
|
|
||||||
with:
|
|
||||||
go-version: 1.16
|
|
||||||
- name: checkout
|
|
||||||
uses: actions/checkout@v2
|
|
||||||
- name: cache
|
|
||||||
uses: actions/cache@v2
|
|
||||||
with:
|
|
||||||
path: ~/go/pkg/mod
|
|
||||||
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
|
|
||||||
restore-keys: ${{ runner.os }}-go-
|
|
||||||
- name: deps
|
|
||||||
run: go get -v -t -d ./...
|
|
||||||
- name: test
|
|
||||||
env:
|
|
||||||
INTEGRATION_TESTS: yes
|
|
||||||
run: go test -mod readonly -v ./...
|
|
||||||
lint:
|
|
||||||
name: lint
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- name: checkout
|
|
||||||
uses: actions/checkout@v2
|
|
||||||
- name: lint
|
|
||||||
uses: golangci/golangci-lint-action@v2
|
|
||||||
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.
|
|
||||||
version: v1.30
|
|
||||||
# Optional: working directory, useful for monorepos
|
|
||||||
# working-directory: somedir
|
|
||||||
# Optional: golangci-lint command line arguments.
|
|
||||||
# args: --issues-exit-code=0
|
|
||||||
# Optional: show only new issues if it's a pull request. The default value is `false`.
|
|
||||||
# only-new-issues: true
|
|
46
.github/workflows/pr.yml
vendored
46
.github/workflows/pr.yml
vendored
@ -1,46 +0,0 @@
|
|||||||
name: prbuild
|
|
||||||
on:
|
|
||||||
pull_request:
|
|
||||||
branches:
|
|
||||||
- master
|
|
||||||
jobs:
|
|
||||||
test:
|
|
||||||
name: test
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- name: setup
|
|
||||||
uses: actions/setup-go@v2
|
|
||||||
with:
|
|
||||||
go-version: 1.16
|
|
||||||
- name: checkout
|
|
||||||
uses: actions/checkout@v2
|
|
||||||
- name: cache
|
|
||||||
uses: actions/cache@v2
|
|
||||||
with:
|
|
||||||
path: ~/go/pkg/mod
|
|
||||||
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
|
|
||||||
restore-keys: ${{ runner.os }}-go-
|
|
||||||
- name: deps
|
|
||||||
run: go get -v -t -d ./...
|
|
||||||
- name: test
|
|
||||||
env:
|
|
||||||
INTEGRATION_TESTS: yes
|
|
||||||
run: go test -mod readonly -v ./...
|
|
||||||
lint:
|
|
||||||
name: lint
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- name: checkout
|
|
||||||
uses: actions/checkout@v2
|
|
||||||
- name: lint
|
|
||||||
uses: golangci/golangci-lint-action@v2
|
|
||||||
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.
|
|
||||||
version: v1.30
|
|
||||||
# Optional: working directory, useful for monorepos
|
|
||||||
# working-directory: somedir
|
|
||||||
# Optional: golangci-lint command line arguments.
|
|
||||||
# args: --issues-exit-code=0
|
|
||||||
# Optional: show only new issues if it's a pull request. The default value is `false`.
|
|
||||||
# only-new-issues: true
|
|
5
.golangci.yml
Normal file
5
.golangci.yml
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
run:
|
||||||
|
concurrency: 8
|
||||||
|
deadline: 5m
|
||||||
|
issues-exit-code: 1
|
||||||
|
tests: true
|
@ -3,8 +3,40 @@ package jsonpb
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"testing"
|
"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 TestReadBody(t *testing.T) {
|
func TestReadBody(t *testing.T) {
|
||||||
t.Skip("skip without proto message")
|
t.Skip("skip without proto message")
|
||||||
s := &struct {
|
s := &struct {
|
||||||
|
14
go.mod
14
go.mod
@ -1,8 +1,14 @@
|
|||||||
module github.com/unistack-org/micro-codec-jsonpb/v3
|
module go.unistack.org/micro-codec-jsonpb/v4
|
||||||
|
|
||||||
go 1.16
|
go 1.19
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/unistack-org/micro/v3 v3.3.19
|
go.unistack.org/micro-proto/v4 v4.0.1
|
||||||
google.golang.org/protobuf v1.26.0
|
go.unistack.org/micro/v4 v4.0.14
|
||||||
|
google.golang.org/protobuf v1.32.0
|
||||||
|
)
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/google/go-cmp v0.5.9 // indirect
|
||||||
|
golang.org/x/exp v0.0.0-20231219180239-dc181d75b848 // indirect
|
||||||
)
|
)
|
||||||
|
34
go.sum
34
go.sum
@ -1,24 +1,10 @@
|
|||||||
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
|
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
|
||||||
github.com/ef-ds/deque v1.0.4/go.mod h1:gXDnTC3yqvBcHbq2lcExjtAcVrOnJCbMcZXmuj8Z4tg=
|
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
||||||
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
|
go.unistack.org/micro-proto/v4 v4.0.1 h1:2RKHgtCOOcAFgKsnngGK5bqM/6MWXOjVCdw03dbuoF8=
|
||||||
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
|
go.unistack.org/micro-proto/v4 v4.0.1/go.mod h1:ArmK7o+uFvxSY3dbJhKBBX4Pm1rhWdLEFf3LxBrMtec=
|
||||||
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
go.unistack.org/micro/v4 v4.0.14 h1:zN+aVfTceei3TPzuA7f2wAXLYHoSHWpx2XgW8yqaTIA=
|
||||||
github.com/google/uuid v1.2.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
go.unistack.org/micro/v4 v4.0.14/go.mod h1:ZDgU9931vm2l7X6RN/6UuwRIVp24GRdmQ7dKmegArk4=
|
||||||
github.com/imdario/mergo v0.3.12/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA=
|
golang.org/x/exp v0.0.0-20231219180239-dc181d75b848 h1:+iq7lrkxmFNBM7xx+Rae2W6uyPfhPeDWD+n+JgppptE=
|
||||||
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
|
golang.org/x/exp v0.0.0-20231219180239-dc181d75b848/go.mod h1:iRJReGqOEeBhDZGkGbynYwcHlctCvnjTYIamk7uXpHI=
|
||||||
github.com/silas/dag v0.0.0-20210121180416-41cf55125c34/go.mod h1:7RTUFBdIRC9nZ7/3RyRNH1bdqIShrDejd1YbLwgPS+I=
|
google.golang.org/protobuf v1.32.0 h1:pPC6BG5ex8PDFnkbrGU3EixyhKcQ2aDuBS36lqK/C7I=
|
||||||
github.com/unistack-org/micro/v3 v3.3.19 h1:x0bl4H+IZvdHHKfA8qMcox9W92+e7rtbqgRfQPoP79w=
|
google.golang.org/protobuf v1.32.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
|
||||||
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=
|
|
||||||
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.26.0 h1:bxAC2xTBsZGibn2RTntX0oH50xLsqy1OxA9tTL3p/lk=
|
|
||||||
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
|
|
||||||
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=
|
|
||||||
|
165
jsonpb.go
165
jsonpb.go
@ -1,71 +1,108 @@
|
|||||||
// Package jsonpb provides a json codec
|
// Package jsonpb provides a json codec
|
||||||
package jsonpb
|
package jsonpb // import "go.unistack.org/micro-codec-jsonpb/v4"
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io"
|
"io"
|
||||||
|
|
||||||
"github.com/unistack-org/micro/v3/codec"
|
pb "go.unistack.org/micro-proto/v4/codec"
|
||||||
rutil "github.com/unistack-org/micro/v3/util/reflect"
|
"go.unistack.org/micro/v4/codec"
|
||||||
|
rutil "go.unistack.org/micro/v4/util/reflect"
|
||||||
jsonpb "google.golang.org/protobuf/encoding/protojson"
|
jsonpb "google.golang.org/protobuf/encoding/protojson"
|
||||||
"google.golang.org/protobuf/proto"
|
"google.golang.org/protobuf/proto"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
JsonpbMarshaler = jsonpb.MarshalOptions{
|
DefaultMarshalOptions = jsonpb.MarshalOptions{
|
||||||
UseEnumNumbers: false,
|
UseEnumNumbers: false,
|
||||||
EmitUnpopulated: false,
|
EmitUnpopulated: false,
|
||||||
UseProtoNames: true,
|
UseProtoNames: false,
|
||||||
AllowPartial: false,
|
AllowPartial: false,
|
||||||
}
|
}
|
||||||
|
|
||||||
JsonpbUnmarshaler = jsonpb.UnmarshalOptions{
|
DefaultUnmarshalOptions = jsonpb.UnmarshalOptions{
|
||||||
DiscardUnknown: false,
|
DiscardUnknown: false,
|
||||||
AllowPartial: false,
|
AllowPartial: false,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
type jsonpbCodec struct{}
|
type jsonpbCodec struct {
|
||||||
|
opts codec.Options
|
||||||
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
flattenTag = "flatten"
|
flattenTag = "flatten"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (c *jsonpbCodec) Marshal(v interface{}) ([]byte, error) {
|
func (c *jsonpbCodec) Marshal(v interface{}, opts ...codec.Option) ([]byte, error) {
|
||||||
switch m := v.(type) {
|
if v == nil {
|
||||||
case nil:
|
|
||||||
return nil, nil
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
options := c.opts
|
||||||
|
for _, o := range opts {
|
||||||
|
o(&options)
|
||||||
|
}
|
||||||
|
|
||||||
|
if nv, nerr := rutil.StructFieldByTag(v, options.TagName, flattenTag); nerr == nil {
|
||||||
|
v = nv
|
||||||
|
}
|
||||||
|
|
||||||
|
switch m := v.(type) {
|
||||||
case *codec.Frame:
|
case *codec.Frame:
|
||||||
return m.Data, nil
|
return m.Data, nil
|
||||||
case proto.Message:
|
case *pb.Frame:
|
||||||
if nv, nerr := rutil.StructFieldByTag(m, codec.DefaultTagName, flattenTag); nerr == nil {
|
return m.Data, nil
|
||||||
if nm, ok := nv.(proto.Message); ok {
|
|
||||||
m = nm
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return JsonpbMarshaler.Marshal(m)
|
|
||||||
}
|
}
|
||||||
return nil, codec.ErrInvalidMessage
|
|
||||||
|
if _, ok := v.(proto.Message); !ok {
|
||||||
|
return nil, codec.ErrInvalidMessage
|
||||||
|
}
|
||||||
|
|
||||||
|
marshalOptions := DefaultMarshalOptions
|
||||||
|
if options.Context != nil {
|
||||||
|
if f, ok := options.Context.Value(marshalOptionsKey{}).(jsonpb.MarshalOptions); ok {
|
||||||
|
marshalOptions = f
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return marshalOptions.Marshal(v.(proto.Message))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *jsonpbCodec) Unmarshal(d []byte, v interface{}) error {
|
func (c *jsonpbCodec) Unmarshal(d []byte, v interface{}, opts ...codec.Option) error {
|
||||||
if len(d) == 0 {
|
if v == nil || len(d) == 0 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
options := c.opts
|
||||||
|
for _, o := range opts {
|
||||||
|
o(&options)
|
||||||
|
}
|
||||||
|
|
||||||
|
if nv, nerr := rutil.StructFieldByTag(v, options.TagName, flattenTag); nerr == nil {
|
||||||
|
v = nv
|
||||||
|
}
|
||||||
|
|
||||||
switch m := v.(type) {
|
switch m := v.(type) {
|
||||||
case nil:
|
|
||||||
return nil
|
|
||||||
case *codec.Frame:
|
case *codec.Frame:
|
||||||
m.Data = d
|
m.Data = d
|
||||||
return nil
|
return nil
|
||||||
case proto.Message:
|
case *pb.Frame:
|
||||||
if nv, nerr := rutil.StructFieldByTag(m, codec.DefaultTagName, flattenTag); nerr == nil {
|
m.Data = d
|
||||||
if nm, ok := nv.(proto.Message); ok {
|
return nil
|
||||||
m = nm
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return JsonpbUnmarshaler.Unmarshal(d, m)
|
|
||||||
}
|
}
|
||||||
return codec.ErrInvalidMessage
|
|
||||||
|
if _, ok := v.(proto.Message); !ok {
|
||||||
|
return codec.ErrInvalidMessage
|
||||||
|
}
|
||||||
|
|
||||||
|
unmarshalOptions := DefaultUnmarshalOptions
|
||||||
|
if options.Context != nil {
|
||||||
|
if f, ok := options.Context.Value(unmarshalOptionsKey{}).(jsonpb.UnmarshalOptions); ok {
|
||||||
|
unmarshalOptions = f
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return unmarshalOptions.Unmarshal(d, v.(proto.Message))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *jsonpbCodec) ReadHeader(conn io.Reader, m *codec.Message, t codec.MessageType) error {
|
func (c *jsonpbCodec) ReadHeader(conn io.Reader, m *codec.Message, t codec.MessageType) error {
|
||||||
@ -73,65 +110,37 @@ func (c *jsonpbCodec) ReadHeader(conn io.Reader, m *codec.Message, t codec.Messa
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *jsonpbCodec) ReadBody(conn io.Reader, v interface{}) error {
|
func (c *jsonpbCodec) ReadBody(conn io.Reader, v interface{}) error {
|
||||||
switch m := v.(type) {
|
if v == nil {
|
||||||
case nil:
|
|
||||||
return 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
|
|
||||||
case proto.Message:
|
|
||||||
buf, err := io.ReadAll(conn)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
} else if len(buf) == 0 {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
if nv, nerr := rutil.StructFieldByTag(m, codec.DefaultTagName, flattenTag); nerr == nil {
|
|
||||||
if nm, ok := nv.(proto.Message); ok {
|
|
||||||
m = nm
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return JsonpbUnmarshaler.Unmarshal(buf, m)
|
|
||||||
}
|
}
|
||||||
return codec.ErrInvalidMessage
|
buf, err := io.ReadAll(conn)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
} else if len(buf) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return c.Unmarshal(buf, v)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *jsonpbCodec) Write(conn io.Writer, m *codec.Message, v interface{}) error {
|
func (c *jsonpbCodec) Write(conn io.Writer, m *codec.Message, v interface{}) error {
|
||||||
switch m := v.(type) {
|
if v == nil {
|
||||||
case nil:
|
|
||||||
return nil
|
return nil
|
||||||
case *codec.Frame:
|
|
||||||
_, err := conn.Write(m.Data)
|
|
||||||
return err
|
|
||||||
case proto.Message:
|
|
||||||
if nv, nerr := rutil.StructFieldByTag(m, codec.DefaultTagName, flattenTag); nerr == nil {
|
|
||||||
if nm, ok := nv.(proto.Message); ok {
|
|
||||||
m = nm
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
buf, err := JsonpbMarshaler.Marshal(m)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
} else if len(buf) == 0 {
|
|
||||||
return codec.ErrInvalidMessage
|
|
||||||
}
|
|
||||||
_, err = conn.Write(buf)
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
return codec.ErrInvalidMessage
|
|
||||||
|
buf, err := c.Marshal(v)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
} else if len(buf) == 0 {
|
||||||
|
return codec.ErrInvalidMessage
|
||||||
|
}
|
||||||
|
_, err = conn.Write(buf)
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *jsonpbCodec) String() string {
|
func (c *jsonpbCodec) String() string {
|
||||||
return "jsonpb"
|
return "jsonpb"
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewCodec() codec.Codec {
|
func NewCodec(opts ...codec.Option) codec.Codec {
|
||||||
return &jsonpbCodec{}
|
return &jsonpbCodec{opts: codec.NewOptions(opts...)}
|
||||||
}
|
}
|
||||||
|
18
options.go
Normal file
18
options.go
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
package jsonpb
|
||||||
|
|
||||||
|
import (
|
||||||
|
codec "go.unistack.org/micro/v4/codec"
|
||||||
|
jsonpb "google.golang.org/protobuf/encoding/protojson"
|
||||||
|
)
|
||||||
|
|
||||||
|
type unmarshalOptionsKey struct{}
|
||||||
|
|
||||||
|
func UnmarshalOptions(o jsonpb.UnmarshalOptions) codec.Option {
|
||||||
|
return codec.SetOption(unmarshalOptionsKey{}, o)
|
||||||
|
}
|
||||||
|
|
||||||
|
type marshalOptionsKey struct{}
|
||||||
|
|
||||||
|
func MarshalOptions(o jsonpb.MarshalOptions) codec.Option {
|
||||||
|
return codec.SetOption(marshalOptionsKey{}, o)
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user