Compare commits
133 Commits
Author | SHA1 | Date | |
---|---|---|---|
bd3dcf3637 | |||
b2419514b7 | |||
8a90c1c723 | |||
0226eaf0ad | |||
|
0e73a2fbbc | ||
730763626e | |||
|
1885e287a2 | ||
5d8a0b71cb | |||
|
30c0ac38fd | ||
80fc830813 | |||
|
4877efdfbd | ||
fe98e12d77 | |||
|
4d06b44ac6 | ||
2af93f2cac | |||
|
ea6e7ad950 | ||
02c600caa1 | |||
|
42c1ad2799 | ||
|
6a76cedfcb | ||
|
2d23ebf3ae | ||
c377ddb186 | |||
|
239dcce3c2 | ||
0a84782c65 | |||
|
835852c097 | ||
4cc2c22f2b | |||
|
be8780182a | ||
85e4499703 | |||
|
b1f457d5e6 | ||
6a5cd1c381 | |||
|
124533c3e4 | ||
5cf66d3011 | |||
|
c9dc6095af | ||
12a40ace74 | |||
|
5dbf9ace1c | ||
|
8c8037964f | ||
03e6c7269f | |||
|
6f3e59c05c | ||
|
febd26c20f | ||
|
04d6f0628a | ||
|
b1976d5e12 | ||
|
729b00c538 | ||
|
783d1bed5f | ||
|
fb9d25a389 | ||
6f0ead5fd9 | |||
|
231686300d | ||
44e0d981b3 | |||
|
fa41fa1550 | ||
|
439b694ac4 | ||
62bd04416a | |||
|
757a8f8e66 | ||
|
9bac8070a6 | ||
|
6976a6cc84 | ||
|
34c79eeed5 | ||
6e7eff8531 | |||
|
a27c8767f0 | ||
6101dd3163 | |||
|
35c8bd15f2 | ||
19f7cbce19 | |||
|
85e166e090 | ||
ae6a4f0e65 | |||
|
737ea77c92 | ||
7f4cc90129 | |||
|
17b09a39fa | ||
54f8f645c8 | |||
|
21d04de170 | ||
f6b284cf57 | |||
|
a99d303b9b | ||
60e3dc97ed | |||
|
6d86b96242 | ||
bce2ec0701 | |||
ead95fbf9b | |||
983eac96b3 | |||
|
d16335438c | ||
7a9caac6ab | |||
|
6ac370caee | ||
c41d17a809 | |||
|
341851aca9 | ||
b119f09274 | |||
|
ee49abd2b6 | ||
e83a1c2ded | |||
|
bada52c03a | ||
96f94fd693 | |||
|
37f81cf0d6 | ||
ade1c6c5c1 | |||
|
18424a0c5c | ||
3b2b12835a | |||
|
00a33000d9 | ||
ecca6bc35c | |||
|
fddcac940d | ||
12ac317e40 | |||
|
d614f9c9ac | ||
687fc307b1 | |||
|
98d5a448e8 | ||
fb9e6a8979 | |||
|
9904fd9f09 | ||
05df9387e2 | |||
|
55c0ad4cb4 | ||
57b3d02eb4 | |||
|
efbdb8f766 | ||
2e3574ce83 | |||
|
7c8be81c11 | ||
06108a561e | |||
|
30f4a6ca4e | ||
e0b9b0d7ca | |||
|
bb573dd070 | ||
aa7e44c086 | |||
43d12db10d | |||
|
96c51073b4 | ||
0803f500f9 | |||
2fe69ffcb8 | |||
dc15319a6d | |||
12e4c80d1a | |||
|
5ebe248b19 | ||
|
e7d372062c | ||
3895c6b703 | |||
a0580da649 | |||
|
cafc7a83cc | ||
|
58007eafeb | ||
|
f30e61d84f | ||
|
c43d91461e | ||
c93f2fdf3e | |||
dda116e30b | |||
|
1be19b6e8b | ||
|
9d0e5abde7 | ||
|
30550965ef | ||
fec43d41dc | |||
f546c4398f | |||
b4721030eb | |||
|
f9fca87e92 | ||
|
c6db5d6e32 | ||
|
c92cfdd4ae | ||
|
6a26e26bae | ||
8ecea8221f | |||
adbd069875 |
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"
|
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
|
|
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:
|
push:
|
||||||
branches:
|
branches:
|
||||||
- master
|
- master
|
||||||
|
- v3
|
||||||
jobs:
|
jobs:
|
||||||
test:
|
test:
|
||||||
name: test
|
name: test
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- name: setup
|
- name: setup
|
||||||
uses: actions/setup-go@v2
|
uses: actions/setup-go@v3
|
||||||
with:
|
with:
|
||||||
go-version: 1.16
|
go-version: 1.17
|
||||||
- name: checkout
|
- name: checkout
|
||||||
uses: actions/checkout@v2
|
uses: actions/checkout@v3
|
||||||
- name: cache
|
- name: cache
|
||||||
uses: actions/cache@v2
|
uses: actions/cache@v3
|
||||||
with:
|
with:
|
||||||
path: ~/go/pkg/mod
|
path: ~/go/pkg/mod
|
||||||
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
|
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
|
||||||
@ -31,9 +32,9 @@ jobs:
|
|||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- name: checkout
|
- name: checkout
|
||||||
uses: actions/checkout@v2
|
uses: actions/checkout@v3
|
||||||
- name: lint
|
- name: lint
|
||||||
uses: golangci/golangci-lint-action@v2
|
uses: golangci/golangci-lint-action@v3.4.0
|
||||||
continue-on-error: true
|
continue-on-error: true
|
||||||
with:
|
with:
|
||||||
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
|
# 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:
|
pull_request:
|
||||||
branches:
|
branches:
|
||||||
- master
|
- master
|
||||||
|
- v3
|
||||||
jobs:
|
jobs:
|
||||||
test:
|
test:
|
||||||
name: test
|
name: test
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- name: setup
|
- name: setup
|
||||||
uses: actions/setup-go@v2
|
uses: actions/setup-go@v3
|
||||||
with:
|
with:
|
||||||
go-version: 1.16
|
go-version: 1.17
|
||||||
- name: checkout
|
- name: checkout
|
||||||
uses: actions/checkout@v2
|
uses: actions/checkout@v3
|
||||||
- name: cache
|
- name: cache
|
||||||
uses: actions/cache@v2
|
uses: actions/cache@v3
|
||||||
with:
|
with:
|
||||||
path: ~/go/pkg/mod
|
path: ~/go/pkg/mod
|
||||||
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
|
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
|
||||||
@ -31,9 +32,9 @@ jobs:
|
|||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- name: checkout
|
- name: checkout
|
||||||
uses: actions/checkout@v2
|
uses: actions/checkout@v3
|
||||||
- name: lint
|
- name: lint
|
||||||
uses: golangci/golangci-lint-action@v2
|
uses: golangci/golangci-lint-action@v3.4.0
|
||||||
continue-on-error: true
|
continue-on-error: true
|
||||||
with:
|
with:
|
||||||
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
|
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
|
||||||
|
19
go.mod
19
go.mod
@ -1,11 +1,16 @@
|
|||||||
module github.com/unistack-org/micro-codec-segmentio/v3
|
module go.unistack.org/micro-codec-segmentio/v4
|
||||||
|
|
||||||
go 1.16
|
go 1.19
|
||||||
|
|
||||||
// github.com/oxtoacart/bpool v0.0.0-20190530202638-03653db5a59c
|
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/segmentio/encoding v0.2.17
|
github.com/segmentio/encoding v0.3.6
|
||||||
github.com/unistack-org/micro/v3 v3.3.13
|
go.unistack.org/micro-proto/v4 v4.0.0
|
||||||
google.golang.org/protobuf v1.26.0
|
go.unistack.org/micro/v4 v4.0.1
|
||||||
|
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,28 +1,21 @@
|
|||||||
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/uuid v1.2.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
github.com/google/go-cmp v0.5.7 h1:81/ik6ipDQS2aGcBfIN5dHDB36BwrStyeAQquSYCV4o=
|
||||||
github.com/imdario/mergo v0.3.12/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA=
|
github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE=
|
||||||
github.com/klauspost/cpuid/v2 v2.0.5 h1:qnfhwbFriwDIX51QncuNU5mEMf+6KE3t7O8V2KQl3Dg=
|
github.com/segmentio/asm v1.1.3/go.mod h1:Ld3L4ZXGNcSLRg4JBsZ3//1+f/TjYl0Mzen/DQy1EJg=
|
||||||
github.com/klauspost/cpuid/v2 v2.0.5/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
|
github.com/segmentio/asm v1.2.0 h1:9BQrFxC+YOHJlTlHGkTrFWf59nbL3XnCoFLTwDCI7ys=
|
||||||
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
|
github.com/segmentio/asm v1.2.0/go.mod h1:BqMnlJP91P8d+4ibuonYZw9mfnzI9HfxselHZr5aAcs=
|
||||||
github.com/segmentio/encoding v0.2.17 h1:cgfmPc44u1po1lz5bSgF00gLCROBjDNc7h+H7I20zpc=
|
github.com/segmentio/encoding v0.3.6 h1:E6lVLyDPseWEulBmCmAKPanDd3jiyGDo5gMcugCRwZQ=
|
||||||
github.com/segmentio/encoding v0.2.17/go.mod h1:7E68jTSWMnNoYhHi1JbLd7NBSB6XfE4vzqhR88hDBQc=
|
github.com/segmentio/encoding v0.3.6/go.mod h1:n0JeuIqEQrQoPDGsjo8UNd1iA0U8d8+oHAA4E3G3OxM=
|
||||||
github.com/silas/dag v0.0.0-20210121180416-41cf55125c34/go.mod h1:7RTUFBdIRC9nZ7/3RyRNH1bdqIShrDejd1YbLwgPS+I=
|
go.unistack.org/micro-proto/v4 v4.0.0 h1:+TAwKSlx48eLsNO2E2cO0JLE8we1KU2AuWe9Q310FuU=
|
||||||
github.com/unistack-org/micro/v3 v3.3.13 h1:y4bDDkbwnjgOckrhFkC6D/o42tr75X33UbrB+Ko0M68=
|
go.unistack.org/micro-proto/v4 v4.0.0/go.mod h1:ArmK7o+uFvxSY3dbJhKBBX4Pm1rhWdLEFf3LxBrMtec=
|
||||||
github.com/unistack-org/micro/v3 v3.3.13/go.mod h1:98hNcMXp/WyWJwLwCuwrhN1Jm7aCWaRNsMfRjK8Fq+Y=
|
go.unistack.org/micro/v4 v4.0.1 h1:xo1IxbVfgh8i0eY0VeYa3cbb13u5n/Mxnp3FOgWD4Jo=
|
||||||
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
|
go.unistack.org/micro/v4 v4.0.1/go.mod h1:p/J5UcSJjfHsWGT31uKoghQ5rUQZzQJBAFy+Z4+ZVMs=
|
||||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20211110154304-99a53858aa08/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ=
|
||||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
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.26.0 h1:bxAC2xTBsZGibn2RTntX0oH50xLsqy1OxA9tTL3p/lk=
|
google.golang.org/protobuf v1.28.1 h1:d0NfwRgPtno5B1Wa6L2DAG+KivqkdutMf1UhdNx175w=
|
||||||
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
|
google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
|
||||||
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=
|
|
||||||
|
50
json/codec_test.go
Normal file
50
json/codec_test.go
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
218
json/json.go
218
json/json.go
@ -1,29 +1,39 @@
|
|||||||
// Package json provides a json codec
|
// Package json provides a json codec
|
||||||
package json
|
package json // import "go.unistack.org/micro-codec-segmentio/v4/json"
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
|
||||||
|
|
||||||
"github.com/segmentio/encoding/json"
|
"github.com/segmentio/encoding/json"
|
||||||
"github.com/unistack-org/micro/v3/codec"
|
pb "go.unistack.org/micro-proto/v4/codec"
|
||||||
|
"go.unistack.org/micro/v4/codec"
|
||||||
|
rutil "go.unistack.org/micro/v4/util/reflect"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
JsonMarshaler = &Marshaler{}
|
DefaultMarshalOptions = JsonMarshalOptions{
|
||||||
|
EscapeHTML: true,
|
||||||
|
SortMapKeys: true,
|
||||||
|
}
|
||||||
|
|
||||||
JsonUnmarshaler = &Unmarshaler{
|
DefaultUnmarshalOptions = JsonUnmarshalOptions{
|
||||||
ZeroCopy: true,
|
ZeroCopy: true,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
type Marshaler struct {
|
var _ codec.Codec = &jsonCodec{}
|
||||||
|
|
||||||
|
const (
|
||||||
|
flattenTag = "flatten"
|
||||||
|
)
|
||||||
|
|
||||||
|
type JsonMarshalOptions struct {
|
||||||
EscapeHTML bool
|
EscapeHTML bool
|
||||||
SortMapKeys bool
|
SortMapKeys bool
|
||||||
TrustRawMessage bool
|
TrustRawMessage bool
|
||||||
}
|
}
|
||||||
|
|
||||||
type Unmarshaler struct {
|
type JsonUnmarshalOptions struct {
|
||||||
DisallowUnknownFields bool
|
DisallowUnknownFields bool
|
||||||
DontCopyNumber bool
|
DontCopyNumber bool
|
||||||
DontCopyRawMessage bool
|
DontCopyRawMessage bool
|
||||||
@ -34,37 +44,128 @@ type Unmarshaler struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type jsonCodec struct {
|
type jsonCodec struct {
|
||||||
|
opts codec.Options
|
||||||
encodeFlags json.AppendFlags
|
encodeFlags json.AppendFlags
|
||||||
decodeFlags json.ParseFlags
|
decodeFlags json.ParseFlags
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *jsonCodec) Marshal(b interface{}) ([]byte, error) {
|
func getMarshalFlags(o JsonMarshalOptions) json.AppendFlags {
|
||||||
switch m := b.(type) {
|
var encodeFlags json.AppendFlags
|
||||||
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, b, c.encodeFlags)
|
buf, err = json.Append(buf, v, getMarshalFlags(marshalOptions))
|
||||||
return buf, err
|
return buf, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *jsonCodec) Unmarshal(b []byte, v interface{}) error {
|
func (c *jsonCodec) Unmarshal(b []byte, v interface{}, opts ...codec.Option) error {
|
||||||
if b == nil {
|
if len(b) == 0 || v == 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
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err := json.Parse(b, v, c.decodeFlags)
|
unmarshalOptions := DefaultUnmarshalOptions
|
||||||
|
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
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -72,85 +173,40 @@ func (c *jsonCodec) ReadHeader(conn io.Reader, m *codec.Message, t codec.Message
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *jsonCodec) ReadBody(conn io.Reader, b interface{}) error {
|
func (c *jsonCodec) ReadBody(conn io.Reader, v interface{}) error {
|
||||||
switch m := b.(type) {
|
if v == nil {
|
||||||
case nil:
|
|
||||||
return nil
|
|
||||||
case *codec.Frame:
|
|
||||||
buf, err := ioutil.ReadAll(conn)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
m.Data = buf
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
err := json.NewDecoder(conn).Decode(b)
|
buf, err := io.ReadAll(conn)
|
||||||
if err == io.EOF {
|
if err != nil {
|
||||||
|
return err
|
||||||
|
} else if len(buf) == 0 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return err
|
return c.Unmarshal(buf, v)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *jsonCodec) Write(conn io.Writer, m *codec.Message, b interface{}) error {
|
func (c *jsonCodec) Write(conn io.Writer, m *codec.Message, v interface{}) error {
|
||||||
switch m := b.(type) {
|
if v == nil {
|
||||||
case nil:
|
|
||||||
return nil
|
return nil
|
||||||
case *codec.Frame:
|
|
||||||
_, err := conn.Write(m.Data)
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return json.NewEncoder(conn).Encode(b)
|
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 *jsonCodec) String() string {
|
func (c *jsonCodec) String() string {
|
||||||
return "json"
|
return "json"
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewCodec() codec.Codec {
|
func NewCodec(opts ...codec.Option) *jsonCodec {
|
||||||
var encodeFlags json.AppendFlags
|
return &jsonCodec{opts: codec.NewOptions(opts...)}
|
||||||
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}
|
|
||||||
}
|
}
|
||||||
|
17
json/options.go
Normal file
17
json/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)
|
||||||
|
}
|
51
proto/codec_test.go
Normal file
51
proto/codec_test.go
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
138
proto/proto.go
138
proto/proto.go
@ -1,96 +1,128 @@
|
|||||||
// Package proto provides a proto codec
|
// Package proto provides a proto codec
|
||||||
package proto
|
package proto // import "go.unistack.org/micro-codec-segmentio/v4/proto"
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
|
||||||
|
|
||||||
"github.com/segmentio/encoding/proto"
|
"github.com/segmentio/encoding/proto"
|
||||||
"github.com/unistack-org/micro/v3/codec"
|
pb "go.unistack.org/micro-proto/v4/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
|
||||||
|
}
|
||||||
|
|
||||||
func (c *protoCodec) Marshal(v interface{}) ([]byte, error) {
|
var _ codec.Codec = &protoCodec{}
|
||||||
switch m := v.(type) {
|
|
||||||
case nil:
|
const (
|
||||||
|
flattenTag = "flatten"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (c *protoCodec) 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, 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 newproto.Message, proto.Message:
|
case *pb.Frame:
|
||||||
|
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{}) error {
|
func (c *protoCodec) Unmarshal(d []byte, v interface{}, opts ...codec.Option) error {
|
||||||
var err error
|
if v == nil || len(d) == 0 {
|
||||||
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
|
||||||
case newproto.Message, proto.Message:
|
return nil
|
||||||
err = proto.Unmarshal(d, m)
|
case *pb.Frame:
|
||||||
default:
|
m.Data = d
|
||||||
err = codec.ErrInvalidMessage
|
return nil
|
||||||
}
|
}
|
||||||
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, b interface{}) error {
|
func (c *protoCodec) ReadBody(conn io.Reader, v interface{}) error {
|
||||||
switch m := b.(type) {
|
if v == nil {
|
||||||
case nil:
|
|
||||||
return nil
|
return nil
|
||||||
case *codec.Frame:
|
|
||||||
buf, err := ioutil.ReadAll(conn)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
m.Data = buf
|
|
||||||
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 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 *protoCodec) Write(conn io.Writer, m *codec.Message, b interface{}) error {
|
func (c *protoCodec) Write(conn io.Writer, m *codec.Message, v interface{}) error {
|
||||||
switch m := b.(type) {
|
if v == nil {
|
||||||
case nil:
|
|
||||||
return nil
|
return nil
|
||||||
case *codec.Frame:
|
|
||||||
_, err := conn.Write(m.Data)
|
|
||||||
return err
|
|
||||||
case newproto.Message, proto.Message:
|
|
||||||
buf, err := proto.Marshal(m)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
_, 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 *protoCodec) String() string {
|
func (c *protoCodec) String() string {
|
||||||
return "proto"
|
return "proto"
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewCodec() codec.Codec {
|
func NewCodec(opts ...codec.Option) *protoCodec {
|
||||||
return &protoCodec{}
|
return &protoCodec{opts: codec.NewOptions(opts...)}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user