23 Commits

Author SHA1 Message Date
12e4c80d1a fix proto codec
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2021-09-27 15:50:02 +03:00
github-actions[bot]
5ebe248b19 Merge pull request #50 from unistack-org/dependabot/github_actions/actions/github-script-5
Bump actions/github-script from 4 to 5
2021-09-24 18:20:13 +00:00
dependabot[bot]
e7d372062c Bump actions/github-script from 4 to 5
Bumps [actions/github-script](https://github.com/actions/github-script) from 4 to 5.
- [Release notes](https://github.com/actions/github-script/releases)
- [Commits](https://github.com/actions/github-script/compare/v4...v5)

---
updated-dependencies:
- dependency-name: actions/github-script
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>
2021-09-24 18:19:20 +00:00
3895c6b703 fix codec.Frame in case of flatten struct tag
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2021-09-24 00:45:52 +03:00
a0580da649 update workflow
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2021-09-21 21:46:55 +03:00
github-actions[bot]
cafc7a83cc Merge pull request #48 from unistack-org/dependabot/go_modules/github.com/segmentio/encoding-0.2.21
Bump github.com/segmentio/encoding from 0.2.20 to 0.2.21
2021-09-20 18:21:33 +00:00
dependabot[bot]
58007eafeb Bump github.com/segmentio/encoding from 0.2.20 to 0.2.21
Bumps [github.com/segmentio/encoding](https://github.com/segmentio/encoding) from 0.2.20 to 0.2.21.
- [Release notes](https://github.com/segmentio/encoding/releases)
- [Commits](https://github.com/segmentio/encoding/compare/v0.2.20...v0.2.21)

---
updated-dependencies:
- dependency-name: github.com/segmentio/encoding
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
2021-09-20 18:19:18 +00:00
dependabot[bot]
f30e61d84f Bump github.com/unistack-org/micro/v3 from 3.7.0 to 3.7.1 (#47)
Bumps [github.com/unistack-org/micro/v3](https://github.com/unistack-org/micro) from 3.7.0 to 3.7.1.
- [Release notes](https://github.com/unistack-org/micro/releases)
- [Commits](https://github.com/unistack-org/micro/compare/v3.7.0...v3.7.1)

---
updated-dependencies:
- dependency-name: github.com/unistack-org/micro/v3
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2021-09-17 09:19:20 +03:00
dependabot[bot]
c43d91461e Bump github.com/segmentio/encoding from 0.2.19 to 0.2.20 (#46)
Bumps [github.com/segmentio/encoding](https://github.com/segmentio/encoding) from 0.2.19 to 0.2.20.
- [Release notes](https://github.com/segmentio/encoding/releases)
- [Commits](https://github.com/segmentio/encoding/compare/v0.2.19...v0.2.20)

---
updated-dependencies:
- dependency-name: github.com/segmentio/encoding
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2021-09-17 08:31:42 +03:00
c93f2fdf3e update workflows
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2021-09-17 07:48:05 +03:00
dda116e30b enable automerge
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2021-09-16 11:04:24 +03:00
dependabot[bot]
1be19b6e8b Bump github.com/unistack-org/micro/v3 from 3.3.19 to 3.7.0 (#45) 2021-09-06 15:30:52 +00:00
dependabot[bot]
9d0e5abde7 Bump github.com/segmentio/encoding from 0.2.17 to 0.2.19 (#44) 2021-09-06 15:27:36 +00:00
dependabot[bot]
30550965ef Bump google.golang.org/protobuf from 1.26.0 to 1.27.1 (#43) 2021-09-06 15:24:55 +00:00
fec43d41dc add github stuff
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2021-09-06 15:26:31 +03:00
f546c4398f add github stuff
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2021-09-06 15:08:01 +03:00
b4721030eb simplify
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2021-05-26 00:58:47 +03:00
Renovate Bot
f9fca87e92 Update module github.com/unistack-org/micro/v3 to v3.3.17 2021-04-27 09:43:49 +00:00
Renovate Bot
c6db5d6e32 Update module github.com/unistack-org/micro/v3 to v3.3.16 2021-04-20 13:21:58 +00:00
Renovate Bot
c92cfdd4ae Update module github.com/unistack-org/micro/v3 to v3.3.15 2021-04-19 14:41:45 +00:00
Renovate Bot
6a26e26bae Update module github.com/unistack-org/micro/v3 to v3.3.14 2021-04-19 03:05:12 +00:00
8ecea8221f fix nil buf check
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2021-04-12 19:17:01 +03:00
adbd069875 add test file
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2021-04-12 17:15:45 +03:00
12 changed files with 505 additions and 187 deletions

19
.github/dependabot.yml vendored Normal file
View 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
View File

@@ -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
View File

@@ -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

75
.github/workflows/codeql-analysis.yml vendored Normal file
View File

@@ -0,0 +1,75 @@
# 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 ]
pull_request:
# The branches below must be a subset of the branches above
branches: [ master ]
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 repository
uses: actions/checkout@v2
# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@v1
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@v1
# 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: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v1

View File

@@ -0,0 +1,66 @@
name: "prautomerge"
on:
workflow_run:
workflows: ["prbuild"]
types:
- completed
permissions:
contents: write
pull-requests: write
jobs:
Dependabot-Automerge:
runs-on: ubuntu-latest
# Contains workaround to execute if dependabot updates the PR by checking for the base branch in the linked PR
# The the github.event.workflow_run.event value is 'push' and not 'pull_request'
# dont work with multiple workflows when last returns success
if: >-
github.event.workflow_run.conclusion == 'success'
&& github.actor == 'dependabot[bot]'
&& github.event.sender.login == 'dependabot[bot]'
&& github.event.sender.type == 'Bot'
&& (github.event.workflow_run.event == 'pull_request'
|| (github.event.workflow_run.event == 'push' && github.event.workflow_run.pull_requests[0].base.ref == github.event.repository.default_branch ))
steps:
- name: Approve Changes and Merge changes if label 'dependencies' is set
uses: actions/github-script@v5
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
console.log(context.payload.workflow_run);
var labelNames = await github.paginate(
github.issues.listLabelsOnIssue,
{
repo: context.repo.repo,
owner: context.repo.owner,
issue_number: context.payload.workflow_run.pull_requests[0].number,
},
(response) => response.data.map(
(label) => label.name
)
);
console.log(labelNames);
if (labelNames.includes('dependencies')) {
console.log('Found label');
await github.pulls.createReview({
repo: context.repo.repo,
owner: context.repo.owner,
pull_number: context.payload.workflow_run.pull_requests[0].number,
event: 'APPROVE'
});
console.log('Approved PR');
await github.pulls.merge({
repo: context.repo.repo,
owner: context.repo.owner,
pull_number: context.payload.workflow_run.pull_requests[0].number,
});
console.log('Merged PR');
}

8
go.mod
View File

@@ -2,10 +2,8 @@ module github.com/unistack-org/micro-codec-segmentio/v3
go 1.16
// github.com/oxtoacart/bpool v0.0.0-20190530202638-03653db5a59c
require (
github.com/segmentio/encoding v0.2.17
github.com/unistack-org/micro/v3 v3.3.13
google.golang.org/protobuf v1.26.0
github.com/segmentio/encoding v0.2.21
github.com/unistack-org/micro/v3 v3.7.4
google.golang.org/protobuf v1.27.1
)

29
go.sum
View File

@@ -1,28 +1,31 @@
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-jwt/jwt/v4 v4.0.0/go.mod h1:/xlHOz8bRuivTWchD4jCa+NbatV+wEUSzwAxVc6locg=
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/uuid v1.2.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/imdario/mergo v0.3.12/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA=
github.com/klauspost/cpuid/v2 v2.0.5 h1:qnfhwbFriwDIX51QncuNU5mEMf+6KE3t7O8V2KQl3Dg=
github.com/klauspost/cpuid/v2 v2.0.5/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
github.com/klauspost/cpuid/v2 v2.0.6 h1:dQ5ueTiftKxp0gyjKSx5+8BtPWkyQbd95m8Gys/RarI=
github.com/klauspost/cpuid/v2 v2.0.6/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
github.com/segmentio/encoding v0.2.17 h1:cgfmPc44u1po1lz5bSgF00gLCROBjDNc7h+H7I20zpc=
github.com/segmentio/encoding v0.2.17/go.mod h1:7E68jTSWMnNoYhHi1JbLd7NBSB6XfE4vzqhR88hDBQc=
github.com/segmentio/asm v1.0.1 h1:g9VK62hXylgXI4yJV+dLTu/1j7kTxG9bkUSYBxL9dpg=
github.com/segmentio/asm v1.0.1/go.mod h1:4EUJGaKsB8ImLUwOGORVsNd9vTRDeh44JGsY4aKp5I4=
github.com/segmentio/encoding v0.2.21 h1:hlRQz3Pv+/mBj+jqr46TVqqv6AuTwvP5aAxQ0Usd4gY=
github.com/segmentio/encoding v0.2.21/go.mod h1:kF1db1oBuYxMvLR3RXrZJchRdBKrS+1J/hL63p5hekI=
github.com/silas/dag v0.0.0-20210121180416-41cf55125c34/go.mod h1:7RTUFBdIRC9nZ7/3RyRNH1bdqIShrDejd1YbLwgPS+I=
github.com/unistack-org/micro/v3 v3.3.13 h1:y4bDDkbwnjgOckrhFkC6D/o42tr75X33UbrB+Ko0M68=
github.com/unistack-org/micro/v3 v3.3.13/go.mod h1:98hNcMXp/WyWJwLwCuwrhN1Jm7aCWaRNsMfRjK8Fq+Y=
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
github.com/unistack-org/micro-proto v0.0.9 h1:KrWLS4FUX7UAWNAilQf70uad6ZPf/0EudeddCXllRVc=
github.com/unistack-org/micro-proto v0.0.9/go.mod h1:Cckwmzd89gvS7ThxzZp9kQR/EOdksFQcsTAtDDyKwrg=
github.com/unistack-org/micro/v3 v3.7.4 h1:nRk7f58v0VsiW7aKL0UQaFCQxFoursHQLoWdeZ1RYbg=
github.com/unistack-org/micro/v3 v3.7.4/go.mod h1:78vy7ggElJD+ayx8ruhJsY+SkGUupfR32LXrg1UObvg=
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-20210330210617-4fbd30eecc44/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.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
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=
google.golang.org/protobuf v1.27.1 h1:SnqbnDw1V7RiZcXPx5MEeqPv2s79L9i7BJUlG/+RurQ=
google.golang.org/protobuf v1.27.1/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=

50
json/codec_test.go Normal file
View File

@@ -0,0 +1,50 @@
package json
import (
"bytes"
"testing"
"github.com/unistack-org/micro/v3/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)
}
}

View File

@@ -3,27 +3,36 @@ package json
import (
"io"
"io/ioutil"
"github.com/segmentio/encoding/json"
"github.com/unistack-org/micro/v3/codec"
rutil "github.com/unistack-org/micro/v3/util/reflect"
)
var (
JsonMarshaler = &Marshaler{}
DefaultMarshalOptions = JsonMarshalOptions{
EscapeHTML: true,
SortMapKeys: true,
}
JsonUnmarshaler = &Unmarshaler{
DefaultUnmarshalOptions = JsonUnmarshalOptions{
ZeroCopy: true,
}
)
type Marshaler struct {
var _ codec.Codec = &jsonCodec{}
const (
flattenTag = "flatten"
)
type JsonMarshalOptions struct {
EscapeHTML bool
SortMapKeys bool
TrustRawMessage bool
}
type Unmarshaler struct {
type JsonUnmarshalOptions struct {
DisallowUnknownFields bool
DontCopyNumber bool
DontCopyRawMessage bool
@@ -34,37 +43,121 @@ type Unmarshaler struct {
}
type jsonCodec struct {
opts codec.Options
encodeFlags json.AppendFlags
decodeFlags json.ParseFlags
}
func (c *jsonCodec) Marshal(b interface{}) ([]byte, error) {
switch m := b.(type) {
case nil:
func getMarshalFlags(o JsonMarshalOptions) json.AppendFlags {
var encodeFlags json.AppendFlags
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
case *codec.Frame:
}
options := c.opts
for _, o := range opts {
o(&options)
}
if nv, err := rutil.StructFieldByTag(v, options.TagName, flattenTag); err == nil {
v = nv
}
if m, ok := v.(*codec.Frame); ok {
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 buf []byte
buf, err = json.Append(buf, b, c.encodeFlags)
buf, err = json.Append(buf, v, getMarshalFlags(marshalOptions))
return buf, err
}
func (c *jsonCodec) Unmarshal(b []byte, v interface{}) error {
if b == nil {
func (c *jsonCodec) Unmarshal(b []byte, v interface{}, opts ...codec.Option) error {
if len(b) == 0 || v == nil {
return nil
}
switch m := v.(type) {
case nil:
return nil
case *codec.Frame:
options := c.opts
for _, o := range opts {
o(&options)
}
if nv, err := rutil.StructFieldByTag(v, options.TagName, flattenTag); err == nil {
v = nv
}
if m, ok := v.(*codec.Frame); ok {
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
}
@@ -72,85 +165,40 @@ func (c *jsonCodec) ReadHeader(conn io.Reader, m *codec.Message, t codec.Message
return nil
}
func (c *jsonCodec) ReadBody(conn io.Reader, b interface{}) error {
switch m := b.(type) {
case nil:
func (c *jsonCodec) ReadBody(conn io.Reader, v interface{}) error {
if v == nil {
return nil
case *codec.Frame:
buf, err := ioutil.ReadAll(conn)
}
buf, err := io.ReadAll(conn)
if err != nil {
return err
} else if len(buf) == 0 {
return nil
}
m.Data = buf
return c.Unmarshal(buf, v)
}
func (c *jsonCodec) Write(conn io.Writer, m *codec.Message, v interface{}) error {
if v == nil {
return nil
}
err := json.NewDecoder(conn).Decode(b)
if err == io.EOF {
return nil
}
buf, err := c.Marshal(v)
if err != nil {
return err
} else if len(buf) == 0 {
return codec.ErrInvalidMessage
}
func (c *jsonCodec) Write(conn io.Writer, m *codec.Message, b interface{}) error {
switch m := b.(type) {
case nil:
return nil
case *codec.Frame:
_, err := conn.Write(m.Data)
_, err = conn.Write(buf)
return err
}
return json.NewEncoder(conn).Encode(b)
}
func (c *jsonCodec) String() string {
return "json"
}
func NewCodec() codec.Codec {
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}
func NewCodec(opts ...codec.Option) *jsonCodec {
return &jsonCodec{opts: codec.NewOptions(opts...)}
}

17
json/options.go Normal file
View File

@@ -0,0 +1,17 @@
package json
import (
codec "github.com/unistack-org/micro/v3/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
View File

@@ -0,0 +1,51 @@
package proto
import (
"bytes"
"testing"
"github.com/unistack-org/micro/v3/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)
}
}

View File

@@ -3,94 +3,118 @@ package proto
import (
"io"
"io/ioutil"
"github.com/segmentio/encoding/proto"
"github.com/unistack-org/micro/v3/codec"
rutil "github.com/unistack-org/micro/v3/util/reflect"
newproto "google.golang.org/protobuf/proto"
)
type protoCodec struct{}
type protoCodec struct {
opts codec.Options
}
func (c *protoCodec) Marshal(v interface{}) ([]byte, error) {
switch m := v.(type) {
case nil:
var _ codec.Codec = &protoCodec{}
const (
flattenTag = "flatten"
)
func (c *protoCodec) Marshal(v interface{}, opts ...codec.Option) ([]byte, error) {
if v == nil {
return nil, nil
case *codec.Frame:
}
options := c.opts
for _, o := range opts {
o(&options)
}
if nv, nerr := rutil.StructFieldByTag(v, options.TagName, flattenTag); nerr == nil {
v = nv
}
if m, ok := v.(*codec.Frame); ok {
return m.Data, nil
case newproto.Message, proto.Message:
}
switch m := v.(type) {
case proto.Message:
return proto.Marshal(m)
case newproto.Message:
return proto.Marshal(m)
}
return nil, codec.ErrInvalidMessage
}
func (c *protoCodec) Unmarshal(d []byte, v interface{}) error {
var err error
if d == nil {
func (c *protoCodec) Unmarshal(d []byte, v interface{}, opts ...codec.Option) error {
if v == nil || len(d) == 0 {
return nil
}
switch m := v.(type) {
case nil:
return nil
case *codec.Frame:
options := c.opts
for _, o := range opts {
o(&options)
}
if nv, nerr := rutil.StructFieldByTag(v, options.TagName, flattenTag); nerr == nil {
v = nv
}
if m, ok := v.(*codec.Frame); ok {
m.Data = d
case newproto.Message, proto.Message:
err = proto.Unmarshal(d, m)
default:
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 {
return nil
}
func (c *protoCodec) ReadBody(conn io.Reader, b interface{}) error {
switch m := b.(type) {
case nil:
func (c *protoCodec) ReadBody(conn io.Reader, v interface{}) error {
if v == nil {
return nil
case *codec.Frame:
buf, err := ioutil.ReadAll(conn)
}
buf, err := io.ReadAll(conn)
if err != nil {
return err
}
m.Data = buf
} else if len(buf) == 0 {
return nil
case newproto.Message, proto.Message:
buf, err := ioutil.ReadAll(conn)
}
return c.Unmarshal(buf, v)
}
func (c *protoCodec) Write(conn io.Writer, m *codec.Message, v interface{}) error {
if v == nil {
return nil
}
buf, err := c.Marshal(v)
if err != nil {
return err
} else if buf == nil {
return nil
}
return proto.Unmarshal(buf, m)
}
} else if len(buf) == 0 {
return codec.ErrInvalidMessage
}
func (c *protoCodec) Write(conn io.Writer, m *codec.Message, b interface{}) error {
switch m := b.(type) {
case 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
}
func (c *protoCodec) String() string {
return "proto"
}
func NewCodec() codec.Codec {
return &protoCodec{}
func NewCodec(opts ...codec.Option) *protoCodec {
return &protoCodec{opts: codec.NewOptions(opts...)}
}