Compare commits
No commits in common. "master" and "v3.2.1" have entirely different histories.
@ -1,29 +0,0 @@
|
|||||||
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'
|
|
@ -1,34 +0,0 @@
|
|||||||
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 ./...
|
|
@ -1,53 +0,0 @@
|
|||||||
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
Normal file
20
.github/renovate.json
vendored
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
{
|
||||||
|
"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
Executable file
13
.github/stale.sh
vendored
Executable file
@ -0,0 +1,13 @@
|
|||||||
|
#!/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
Normal file
46
.github/workflows/build.yml
vendored
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
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
Normal file
46
.github/workflows/pr.yml
vendored
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
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
|
@ -1,9 +1,38 @@
|
|||||||
run:
|
run:
|
||||||
concurrency: 8
|
|
||||||
deadline: 5m
|
deadline: 5m
|
||||||
issues-exit-code: 1
|
|
||||||
tests: true
|
|
||||||
modules-download-mode: readonly
|
modules-download-mode: readonly
|
||||||
skip-files:
|
skip-files:
|
||||||
- ".*\\.pb\\.go$"
|
- ".*\\.pb\\.go$"
|
||||||
- ".*\\.pb\\.micro\\.go$"
|
- ".*\\.pb\\.micro\\.go$"
|
||||||
|
linters:
|
||||||
|
disable-all: false
|
||||||
|
enable-all: false
|
||||||
|
enable:
|
||||||
|
- megacheck
|
||||||
|
- staticcheck
|
||||||
|
- deadcode
|
||||||
|
- varcheck
|
||||||
|
- gosimple
|
||||||
|
- unused
|
||||||
|
- prealloc
|
||||||
|
- scopelint
|
||||||
|
- gocritic
|
||||||
|
- goimports
|
||||||
|
- unconvert
|
||||||
|
- govet
|
||||||
|
- nakedret
|
||||||
|
- structcheck
|
||||||
|
- gosec
|
||||||
|
disable:
|
||||||
|
- maligned
|
||||||
|
- interfacer
|
||||||
|
- typecheck
|
||||||
|
- dupl
|
||||||
|
output:
|
||||||
|
format: colored-line-number
|
||||||
|
# print lines of code with issue, default is true
|
||||||
|
print-issued-lines: true
|
||||||
|
# print linter name in the end of issue text, default is true
|
||||||
|
print-linter-name: true
|
||||||
|
# make issues output unique by line, default is true
|
||||||
|
uniq-by-line: true
|
||||||
|
19
go.mod
19
go.mod
@ -1,16 +1,11 @@
|
|||||||
module go.unistack.org/micro-codec-segmentio/v4
|
module github.com/unistack-org/micro-codec-segmentio/v3
|
||||||
|
|
||||||
go 1.19
|
go 1.16
|
||||||
|
|
||||||
|
// github.com/oxtoacart/bpool v0.0.0-20190530202638-03653db5a59c
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/segmentio/encoding v0.3.6
|
github.com/segmentio/encoding v0.2.17
|
||||||
go.unistack.org/micro-proto/v4 v4.0.0
|
github.com/unistack-org/micro/v3 v3.3.13
|
||||||
go.unistack.org/micro/v4 v4.0.1
|
google.golang.org/protobuf v1.26.0
|
||||||
google.golang.org/protobuf v1.28.1
|
|
||||||
)
|
|
||||||
|
|
||||||
require (
|
|
||||||
github.com/google/go-cmp v0.5.7 // indirect
|
|
||||||
github.com/segmentio/asm v1.2.0 // indirect
|
|
||||||
golang.org/x/sys v0.6.0 // indirect
|
|
||||||
)
|
)
|
||||||
|
39
go.sum
39
go.sum
@ -1,21 +1,28 @@
|
|||||||
|
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/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
|
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=
|
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||||
github.com/google/go-cmp v0.5.7 h1:81/ik6ipDQS2aGcBfIN5dHDB36BwrStyeAQquSYCV4o=
|
github.com/google/uuid v1.2.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||||
github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE=
|
github.com/imdario/mergo v0.3.12/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA=
|
||||||
github.com/segmentio/asm v1.1.3/go.mod h1:Ld3L4ZXGNcSLRg4JBsZ3//1+f/TjYl0Mzen/DQy1EJg=
|
github.com/klauspost/cpuid/v2 v2.0.5 h1:qnfhwbFriwDIX51QncuNU5mEMf+6KE3t7O8V2KQl3Dg=
|
||||||
github.com/segmentio/asm v1.2.0 h1:9BQrFxC+YOHJlTlHGkTrFWf59nbL3XnCoFLTwDCI7ys=
|
github.com/klauspost/cpuid/v2 v2.0.5/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
|
||||||
github.com/segmentio/asm v1.2.0/go.mod h1:BqMnlJP91P8d+4ibuonYZw9mfnzI9HfxselHZr5aAcs=
|
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
|
||||||
github.com/segmentio/encoding v0.3.6 h1:E6lVLyDPseWEulBmCmAKPanDd3jiyGDo5gMcugCRwZQ=
|
github.com/segmentio/encoding v0.2.17 h1:cgfmPc44u1po1lz5bSgF00gLCROBjDNc7h+H7I20zpc=
|
||||||
github.com/segmentio/encoding v0.3.6/go.mod h1:n0JeuIqEQrQoPDGsjo8UNd1iA0U8d8+oHAA4E3G3OxM=
|
github.com/segmentio/encoding v0.2.17/go.mod h1:7E68jTSWMnNoYhHi1JbLd7NBSB6XfE4vzqhR88hDBQc=
|
||||||
go.unistack.org/micro-proto/v4 v4.0.0 h1:+TAwKSlx48eLsNO2E2cO0JLE8we1KU2AuWe9Q310FuU=
|
github.com/silas/dag v0.0.0-20210121180416-41cf55125c34/go.mod h1:7RTUFBdIRC9nZ7/3RyRNH1bdqIShrDejd1YbLwgPS+I=
|
||||||
go.unistack.org/micro-proto/v4 v4.0.0/go.mod h1:ArmK7o+uFvxSY3dbJhKBBX4Pm1rhWdLEFf3LxBrMtec=
|
github.com/unistack-org/micro/v3 v3.3.13 h1:y4bDDkbwnjgOckrhFkC6D/o42tr75X33UbrB+Ko0M68=
|
||||||
go.unistack.org/micro/v4 v4.0.1 h1:xo1IxbVfgh8i0eY0VeYa3cbb13u5n/Mxnp3FOgWD4Jo=
|
github.com/unistack-org/micro/v3 v3.3.13/go.mod h1:98hNcMXp/WyWJwLwCuwrhN1Jm7aCWaRNsMfRjK8Fq+Y=
|
||||||
go.unistack.org/micro/v4 v4.0.1/go.mod h1:p/J5UcSJjfHsWGT31uKoghQ5rUQZzQJBAFy+Z4+ZVMs=
|
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
|
||||||
golang.org/x/sys v0.0.0-20211110154304-99a53858aa08/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ=
|
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||||
|
golang.org/x/text v0.3.3/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 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
|
||||||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
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-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
|
||||||
google.golang.org/protobuf v1.28.1 h1:d0NfwRgPtno5B1Wa6L2DAG+KivqkdutMf1UhdNx175w=
|
google.golang.org/protobuf v1.26.0 h1:bxAC2xTBsZGibn2RTntX0oH50xLsqy1OxA9tTL3p/lk=
|
||||||
google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
|
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=
|
||||||
|
@ -1,50 +0,0 @@
|
|||||||
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 TestReadBody(t *testing.T) {
|
|
||||||
s := &struct {
|
|
||||||
Name string
|
|
||||||
}{}
|
|
||||||
c := NewCodec()
|
|
||||||
b := bytes.NewReader(nil)
|
|
||||||
err := c.ReadBody(b, s)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
}
|
|
216
json/json.go
216
json/json.go
@ -1,39 +1,29 @@
|
|||||||
// Package json provides a json codec
|
// Package json provides a json codec
|
||||||
package json // import "go.unistack.org/micro-codec-segmentio/v4/json"
|
package json
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io"
|
"io"
|
||||||
|
"io/ioutil"
|
||||||
|
|
||||||
"github.com/segmentio/encoding/json"
|
"github.com/segmentio/encoding/json"
|
||||||
pb "go.unistack.org/micro-proto/v4/codec"
|
"github.com/unistack-org/micro/v3/codec"
|
||||||
"go.unistack.org/micro/v4/codec"
|
|
||||||
rutil "go.unistack.org/micro/v4/util/reflect"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
DefaultMarshalOptions = JsonMarshalOptions{
|
JsonMarshaler = &Marshaler{}
|
||||||
EscapeHTML: true,
|
|
||||||
SortMapKeys: true,
|
|
||||||
}
|
|
||||||
|
|
||||||
DefaultUnmarshalOptions = JsonUnmarshalOptions{
|
JsonUnmarshaler = &Unmarshaler{
|
||||||
ZeroCopy: true,
|
ZeroCopy: true,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
var _ codec.Codec = &jsonCodec{}
|
type Marshaler struct {
|
||||||
|
|
||||||
const (
|
|
||||||
flattenTag = "flatten"
|
|
||||||
)
|
|
||||||
|
|
||||||
type JsonMarshalOptions struct {
|
|
||||||
EscapeHTML bool
|
EscapeHTML bool
|
||||||
SortMapKeys bool
|
SortMapKeys bool
|
||||||
TrustRawMessage bool
|
TrustRawMessage bool
|
||||||
}
|
}
|
||||||
|
|
||||||
type JsonUnmarshalOptions struct {
|
type Unmarshaler struct {
|
||||||
DisallowUnknownFields bool
|
DisallowUnknownFields bool
|
||||||
DontCopyNumber bool
|
DontCopyNumber bool
|
||||||
DontCopyRawMessage bool
|
DontCopyRawMessage bool
|
||||||
@ -44,128 +34,37 @@ type JsonUnmarshalOptions struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type jsonCodec struct {
|
type jsonCodec struct {
|
||||||
opts codec.Options
|
|
||||||
encodeFlags json.AppendFlags
|
encodeFlags json.AppendFlags
|
||||||
decodeFlags json.ParseFlags
|
decodeFlags json.ParseFlags
|
||||||
}
|
}
|
||||||
|
|
||||||
func getMarshalFlags(o JsonMarshalOptions) json.AppendFlags {
|
func (c *jsonCodec) Marshal(b interface{}) ([]byte, error) {
|
||||||
var encodeFlags json.AppendFlags
|
switch m := b.(type) {
|
||||||
|
case nil:
|
||||||
if o.EscapeHTML {
|
|
||||||
encodeFlags |= json.EscapeHTML
|
|
||||||
}
|
|
||||||
|
|
||||||
if o.SortMapKeys {
|
|
||||||
encodeFlags |= json.SortMapKeys
|
|
||||||
}
|
|
||||||
|
|
||||||
if o.TrustRawMessage {
|
|
||||||
encodeFlags |= json.TrustRawMessage
|
|
||||||
}
|
|
||||||
|
|
||||||
return encodeFlags
|
|
||||||
}
|
|
||||||
|
|
||||||
func getUnmarshalFlags(o JsonUnmarshalOptions) json.ParseFlags {
|
|
||||||
var decodeFlags json.ParseFlags
|
|
||||||
|
|
||||||
if o.DisallowUnknownFields {
|
|
||||||
decodeFlags |= json.DisallowUnknownFields
|
|
||||||
}
|
|
||||||
|
|
||||||
if o.DontCopyNumber {
|
|
||||||
decodeFlags |= json.DontCopyNumber
|
|
||||||
}
|
|
||||||
|
|
||||||
if o.DontCopyRawMessage {
|
|
||||||
decodeFlags |= json.DontCopyRawMessage
|
|
||||||
}
|
|
||||||
|
|
||||||
if o.DontCopyString {
|
|
||||||
decodeFlags |= json.DontCopyString
|
|
||||||
}
|
|
||||||
|
|
||||||
if o.DontMatchCaseInsensitiveStructFields {
|
|
||||||
decodeFlags |= json.DontMatchCaseInsensitiveStructFields
|
|
||||||
}
|
|
||||||
|
|
||||||
if o.UseNumber {
|
|
||||||
decodeFlags |= json.UseNumber
|
|
||||||
}
|
|
||||||
|
|
||||||
if o.ZeroCopy {
|
|
||||||
decodeFlags |= json.ZeroCopy
|
|
||||||
}
|
|
||||||
|
|
||||||
return decodeFlags
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *jsonCodec) Marshal(v interface{}, opts ...codec.Option) ([]byte, error) {
|
|
||||||
if v == nil {
|
|
||||||
return nil, 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:
|
case *codec.Frame:
|
||||||
return m.Data, nil
|
return m.Data, nil
|
||||||
case *pb.Frame:
|
|
||||||
return m.Data, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
marshalOptions := DefaultMarshalOptions
|
|
||||||
if options.Context != nil {
|
|
||||||
if f, ok := options.Context.Value(marshalOptionsKey{}).(JsonMarshalOptions); ok {
|
|
||||||
marshalOptions = f
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
var buf []byte
|
var buf []byte
|
||||||
buf, err = json.Append(buf, v, getMarshalFlags(marshalOptions))
|
buf, err = json.Append(buf, b, c.encodeFlags)
|
||||||
return buf, err
|
return buf, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *jsonCodec) Unmarshal(b []byte, v interface{}, opts ...codec.Option) error {
|
func (c *jsonCodec) Unmarshal(b []byte, v interface{}) error {
|
||||||
if len(b) == 0 || v == nil {
|
if b == nil {
|
||||||
return 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) {
|
switch m := v.(type) {
|
||||||
|
case nil:
|
||||||
|
return nil
|
||||||
case *codec.Frame:
|
case *codec.Frame:
|
||||||
m.Data = b
|
m.Data = b
|
||||||
return nil
|
return nil
|
||||||
case *pb.Frame:
|
|
||||||
m.Data = b
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
unmarshalOptions := DefaultUnmarshalOptions
|
_, err := json.Parse(b, v, c.decodeFlags)
|
||||||
if options.Context != nil {
|
|
||||||
if f, ok := options.Context.Value(unmarshalOptionsKey{}).(JsonUnmarshalOptions); ok {
|
|
||||||
unmarshalOptions = f
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err := json.Parse(b, v, getUnmarshalFlags(unmarshalOptions))
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -173,40 +72,85 @@ func (c *jsonCodec) ReadHeader(conn io.Reader, m *codec.Message, t codec.Message
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *jsonCodec) ReadBody(conn io.Reader, v interface{}) error {
|
func (c *jsonCodec) ReadBody(conn io.Reader, b interface{}) error {
|
||||||
if v == nil {
|
switch m := b.(type) {
|
||||||
|
case nil:
|
||||||
|
return nil
|
||||||
|
case *codec.Frame:
|
||||||
|
buf, err := ioutil.ReadAll(conn)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
m.Data = buf
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
buf, err := io.ReadAll(conn)
|
err := json.NewDecoder(conn).Decode(b)
|
||||||
if err != nil {
|
if err == io.EOF {
|
||||||
return err
|
|
||||||
} else if len(buf) == 0 {
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return c.Unmarshal(buf, v)
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *jsonCodec) Write(conn io.Writer, m *codec.Message, v interface{}) error {
|
func (c *jsonCodec) Write(conn io.Writer, m *codec.Message, b interface{}) error {
|
||||||
if v == nil {
|
switch m := b.(type) {
|
||||||
|
case nil:
|
||||||
return nil
|
return nil
|
||||||
}
|
case *codec.Frame:
|
||||||
|
_, err := conn.Write(m.Data)
|
||||||
buf, err := c.Marshal(v)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
return err
|
||||||
} else if len(buf) == 0 {
|
|
||||||
return codec.ErrInvalidMessage
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = conn.Write(buf)
|
return json.NewEncoder(conn).Encode(b)
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *jsonCodec) String() string {
|
func (c *jsonCodec) String() string {
|
||||||
return "json"
|
return "json"
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewCodec(opts ...codec.Option) *jsonCodec {
|
func NewCodec() codec.Codec {
|
||||||
return &jsonCodec{opts: codec.NewOptions(opts...)}
|
var encodeFlags json.AppendFlags
|
||||||
|
var decodeFlags json.ParseFlags
|
||||||
|
|
||||||
|
if JsonMarshaler.EscapeHTML {
|
||||||
|
encodeFlags |= json.EscapeHTML
|
||||||
|
}
|
||||||
|
|
||||||
|
if JsonMarshaler.SortMapKeys {
|
||||||
|
encodeFlags |= json.SortMapKeys
|
||||||
|
}
|
||||||
|
|
||||||
|
if JsonMarshaler.TrustRawMessage {
|
||||||
|
encodeFlags |= json.TrustRawMessage
|
||||||
|
}
|
||||||
|
|
||||||
|
if JsonUnmarshaler.DisallowUnknownFields {
|
||||||
|
decodeFlags |= json.DisallowUnknownFields
|
||||||
|
}
|
||||||
|
|
||||||
|
if JsonUnmarshaler.DontCopyNumber {
|
||||||
|
decodeFlags |= json.DontCopyNumber
|
||||||
|
}
|
||||||
|
|
||||||
|
if JsonUnmarshaler.DontCopyRawMessage {
|
||||||
|
decodeFlags |= json.DontCopyRawMessage
|
||||||
|
}
|
||||||
|
|
||||||
|
if JsonUnmarshaler.DontCopyString {
|
||||||
|
decodeFlags |= json.DontCopyString
|
||||||
|
}
|
||||||
|
|
||||||
|
if JsonUnmarshaler.DontMatchCaseInsensitiveStructFields {
|
||||||
|
decodeFlags |= json.DontMatchCaseInsensitiveStructFields
|
||||||
|
}
|
||||||
|
|
||||||
|
if JsonUnmarshaler.UseNumber {
|
||||||
|
decodeFlags |= json.UseNumber
|
||||||
|
}
|
||||||
|
|
||||||
|
if JsonUnmarshaler.ZeroCopy {
|
||||||
|
decodeFlags |= json.ZeroCopy
|
||||||
|
}
|
||||||
|
|
||||||
|
return &jsonCodec{encodeFlags: encodeFlags, decodeFlags: decodeFlags}
|
||||||
}
|
}
|
||||||
|
@ -1,17 +0,0 @@
|
|||||||
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)
|
|
||||||
}
|
|
@ -1,51 +0,0 @@
|
|||||||
package proto
|
|
||||||
|
|
||||||
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 TestReadBody(t *testing.T) {
|
|
||||||
t.Skip("no proto generated")
|
|
||||||
s := &struct {
|
|
||||||
Name string
|
|
||||||
}{}
|
|
||||||
c := NewCodec()
|
|
||||||
b := bytes.NewReader(nil)
|
|
||||||
err := c.ReadBody(b, s)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
}
|
|
134
proto/proto.go
134
proto/proto.go
@ -1,128 +1,96 @@
|
|||||||
// Package proto provides a proto codec
|
// Package proto provides a proto codec
|
||||||
package proto // import "go.unistack.org/micro-codec-segmentio/v4/proto"
|
package proto
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io"
|
"io"
|
||||||
|
"io/ioutil"
|
||||||
|
|
||||||
"github.com/segmentio/encoding/proto"
|
"github.com/segmentio/encoding/proto"
|
||||||
pb "go.unistack.org/micro-proto/v4/codec"
|
"github.com/unistack-org/micro/v3/codec"
|
||||||
"go.unistack.org/micro/v4/codec"
|
|
||||||
rutil "go.unistack.org/micro/v4/util/reflect"
|
|
||||||
newproto "google.golang.org/protobuf/proto"
|
newproto "google.golang.org/protobuf/proto"
|
||||||
)
|
)
|
||||||
|
|
||||||
type protoCodec struct {
|
type protoCodec struct{}
|
||||||
opts codec.Options
|
|
||||||
}
|
|
||||||
|
|
||||||
var _ codec.Codec = &protoCodec{}
|
|
||||||
|
|
||||||
const (
|
|
||||||
flattenTag = "flatten"
|
|
||||||
)
|
|
||||||
|
|
||||||
func (c *protoCodec) 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, nerr := rutil.StructFieldByTag(v, options.TagName, flattenTag); nerr == nil {
|
|
||||||
v = nv
|
|
||||||
}
|
|
||||||
|
|
||||||
|
func (c *protoCodec) Marshal(v interface{}) ([]byte, error) {
|
||||||
switch m := v.(type) {
|
switch m := v.(type) {
|
||||||
|
case nil:
|
||||||
|
return nil, nil
|
||||||
case *codec.Frame:
|
case *codec.Frame:
|
||||||
return m.Data, nil
|
return m.Data, nil
|
||||||
case *pb.Frame:
|
case newproto.Message, proto.Message:
|
||||||
return m.Data, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
switch m := v.(type) {
|
|
||||||
case proto.Message:
|
|
||||||
return proto.Marshal(m)
|
|
||||||
case newproto.Message:
|
|
||||||
return proto.Marshal(m)
|
return proto.Marshal(m)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil, codec.ErrInvalidMessage
|
return nil, codec.ErrInvalidMessage
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *protoCodec) Unmarshal(d []byte, v interface{}, opts ...codec.Option) error {
|
func (c *protoCodec) Unmarshal(d []byte, v interface{}) error {
|
||||||
if v == nil || len(d) == 0 {
|
var err error
|
||||||
|
if d == nil {
|
||||||
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
|
case newproto.Message, proto.Message:
|
||||||
case *pb.Frame:
|
err = proto.Unmarshal(d, m)
|
||||||
m.Data = d
|
default:
|
||||||
return nil
|
err = codec.ErrInvalidMessage
|
||||||
}
|
}
|
||||||
|
return err
|
||||||
switch m := v.(type) {
|
|
||||||
case proto.Message:
|
|
||||||
return proto.Unmarshal(d, m)
|
|
||||||
case newproto.Message:
|
|
||||||
return proto.Unmarshal(d, m)
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
return codec.ErrInvalidMessage
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *protoCodec) ReadHeader(conn io.Reader, m *codec.Message, t codec.MessageType) error {
|
func (c *protoCodec) ReadHeader(conn io.Reader, m *codec.Message, t codec.MessageType) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *protoCodec) ReadBody(conn io.Reader, v interface{}) error {
|
func (c *protoCodec) ReadBody(conn io.Reader, b interface{}) error {
|
||||||
if v == nil {
|
switch m := b.(type) {
|
||||||
|
case nil:
|
||||||
return nil
|
return nil
|
||||||
}
|
case *codec.Frame:
|
||||||
buf, err := io.ReadAll(conn)
|
buf, err := ioutil.ReadAll(conn)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
} else if len(buf) == 0 {
|
}
|
||||||
|
m.Data = buf
|
||||||
return nil
|
return nil
|
||||||
|
case newproto.Message, proto.Message:
|
||||||
|
buf, err := ioutil.ReadAll(conn)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
} else if buf == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return proto.Unmarshal(buf, m)
|
||||||
}
|
}
|
||||||
return c.Unmarshal(buf, v)
|
return codec.ErrInvalidMessage
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *protoCodec) Write(conn io.Writer, m *codec.Message, v interface{}) error {
|
func (c *protoCodec) Write(conn io.Writer, m *codec.Message, b interface{}) error {
|
||||||
if v == nil {
|
switch m := b.(type) {
|
||||||
|
case nil:
|
||||||
return nil
|
return nil
|
||||||
}
|
case *codec.Frame:
|
||||||
|
_, err := conn.Write(m.Data)
|
||||||
buf, err := c.Marshal(v)
|
return err
|
||||||
if err != nil {
|
case newproto.Message, proto.Message:
|
||||||
|
buf, err := proto.Marshal(m)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
_, err = conn.Write(buf)
|
||||||
return err
|
return err
|
||||||
} else if len(buf) == 0 {
|
|
||||||
return codec.ErrInvalidMessage
|
|
||||||
}
|
}
|
||||||
|
return codec.ErrInvalidMessage
|
||||||
_, err = conn.Write(buf)
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *protoCodec) String() string {
|
func (c *protoCodec) String() string {
|
||||||
return "proto"
|
return "proto"
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewCodec(opts ...codec.Option) *protoCodec {
|
func NewCodec() codec.Codec {
|
||||||
return &protoCodec{opts: codec.NewOptions(opts...)}
|
return &protoCodec{}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user