Compare commits
117 Commits
v3.2.0
...
e0d6f2552c
| Author | SHA1 | Date | |
|---|---|---|---|
| e0d6f2552c | |||
|
|
256979f163 | ||
| 31b7e7182e | |||
| 560c8d9704 | |||
| a1f8ab7806 | |||
| adb3922a87 | |||
| 1da599be18 | |||
|
|
bd1d927425 | ||
| 0c15424744 | |||
|
|
e9fa24abef | ||
| b1658acca3 | |||
|
|
693f3413c4 | ||
| 5dd6941054 | |||
|
|
2b426a632e | ||
| 97bc0c0cc4 | |||
|
|
b62f883443 | ||
| ce1fc938ce | |||
|
|
bb765e86f5 | ||
| cce2e66410 | |||
|
|
8c53df737e | ||
|
|
19e8e150d4 | ||
|
|
314c43d3da | ||
| 5682e07e33 | |||
|
|
f740ec3fc9 | ||
| 9f5c40bd81 | |||
|
|
587c6f334e | ||
| 6a8ceba0e3 | |||
|
|
b314a884ba | ||
| 97093818cb | |||
|
|
54385a6d5b | ||
| 17adf36537 | |||
|
|
2c8c75b01f | ||
|
|
6ec4729e61 | ||
| afa46fac82 | |||
|
|
12fb590a3d | ||
|
|
98a18053e1 | ||
| 3272039e80 | |||
|
|
76cdb0e86e | ||
| ab5902c009 | |||
|
|
ab9c8da1b1 | ||
|
|
1466ea03aa | ||
|
|
999be6d821 | ||
|
|
1c663f81bc | ||
|
|
93d8dbae1d | ||
| ac01124d8f | |||
|
|
4f31d16092 | ||
| 7975b61bb4 | |||
|
|
199042a8c2 | ||
|
|
db0bb2408d | ||
| ea263b0337 | |||
|
|
a6c2e49e30 | ||
|
|
2ba5a48137 | ||
|
|
1a101d1b22 | ||
| 112ae7ed38 | |||
|
|
e8dac419f6 | ||
| 8b3029a910 | |||
|
|
90c5362cc1 | ||
| 5867a12049 | |||
|
|
15df8eb771 | ||
| 4f69a911c7 | |||
|
|
67fbc8af2c | ||
| e8f1805a73 | |||
|
|
1f11aac0a6 | ||
| 729b62480e | |||
|
|
6a6d045f6d | ||
| 0a6205e982 | |||
|
|
4c57758283 | ||
| 7f216b6060 | |||
| fec92bbbe7 | |||
| 4aca7e9d64 | |||
|
|
fda3d7ba77 | ||
| 9bcdabc35a | |||
|
|
d3f220643e | ||
| cbbbd8bfa8 | |||
|
|
f2ee146d2c | ||
| 42eca6aa24 | |||
|
|
184d264c48 | ||
| 9e538f587b | |||
|
|
2c0c88d6ac | ||
| cd12f33bd4 | |||
|
|
a2f232532f | ||
| 28412e67c5 | |||
|
|
113a92656a | ||
| 7b3c60af04 | |||
|
|
e46d5d60d4 | ||
| e73cb3d98f | |||
|
|
81e6fc3351 | ||
| f99f861d5a | |||
| 6a133b6191 | |||
|
|
057c01565b | ||
| 37d1caca89 | |||
|
|
5d7aa1cba1 | ||
| a214d7aebb | |||
|
|
c7239fc6fb | ||
| f778707dbd | |||
|
|
cdf89a1446 | ||
| 3397fbf3b3 | |||
| f350043faf | |||
| 066f8b6e66 | |||
|
|
38b67e3b7e | ||
|
|
80de3ae2e4 | ||
| d930c2b246 | |||
| 40a459ea18 | |||
| 336350666e | |||
|
|
9788c35e68 | ||
|
|
2204b01e41 | ||
| 86ba879998 | |||
|
|
9d18efdb67 | ||
| 204499d8e5 | |||
| 6b599c27b2 | |||
| 6eaf4973cf | |||
|
|
c4a74bc93b | ||
|
|
4762d56776 | ||
|
|
bd11bed15e | ||
|
|
ef45584387 | ||
| b94e829776 | |||
| 03f0bc042c |
29
.gitea/workflows/job_lint.yml
Normal file
29
.gitea/workflows/job_lint.yml
Normal file
@@ -0,0 +1,29 @@
|
||||
name: lint
|
||||
|
||||
on:
|
||||
pull_request:
|
||||
types: [opened, reopened, synchronize]
|
||||
branches:
|
||||
- master
|
||||
- v3
|
||||
- v4
|
||||
|
||||
jobs:
|
||||
lint:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: checkout code
|
||||
uses: actions/checkout@v4
|
||||
with:
|
||||
filter: 'blob:none'
|
||||
- name: setup go
|
||||
uses: actions/setup-go@v5
|
||||
with:
|
||||
cache-dependency-path: "**/*.sum"
|
||||
go-version: 'stable'
|
||||
- name: setup deps
|
||||
run: go get -v ./...
|
||||
- name: run lint
|
||||
uses: https://github.com/golangci/golangci-lint-action@v6
|
||||
with:
|
||||
version: 'latest'
|
||||
34
.gitea/workflows/job_test.yml
Normal file
34
.gitea/workflows/job_test.yml
Normal file
@@ -0,0 +1,34 @@
|
||||
name: test
|
||||
|
||||
on:
|
||||
pull_request:
|
||||
types: [opened, reopened, synchronize]
|
||||
branches:
|
||||
- master
|
||||
- v3
|
||||
- v4
|
||||
push:
|
||||
branches:
|
||||
- master
|
||||
- v3
|
||||
- v4
|
||||
|
||||
jobs:
|
||||
test:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: checkout code
|
||||
uses: actions/checkout@v4
|
||||
with:
|
||||
filter: 'blob:none'
|
||||
- name: setup go
|
||||
uses: actions/setup-go@v5
|
||||
with:
|
||||
cache-dependency-path: "**/*.sum"
|
||||
go-version: 'stable'
|
||||
- name: setup deps
|
||||
run: go get -v ./...
|
||||
- name: run test
|
||||
env:
|
||||
INTEGRATION_TESTS: yes
|
||||
run: go test -mod readonly -v ./...
|
||||
53
.gitea/workflows/job_tests.yml
Normal file
53
.gitea/workflows/job_tests.yml
Normal file
@@ -0,0 +1,53 @@
|
||||
name: test
|
||||
|
||||
on:
|
||||
pull_request:
|
||||
types: [opened, reopened, synchronize]
|
||||
branches:
|
||||
- master
|
||||
- v3
|
||||
- v4
|
||||
push:
|
||||
branches:
|
||||
- master
|
||||
- v3
|
||||
- v4
|
||||
|
||||
jobs:
|
||||
test:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: checkout code
|
||||
uses: actions/checkout@v4
|
||||
with:
|
||||
filter: 'blob:none'
|
||||
- name: checkout tests
|
||||
uses: actions/checkout@v4
|
||||
with:
|
||||
ref: master
|
||||
filter: 'blob:none'
|
||||
repository: unistack-org/micro-tests
|
||||
path: micro-tests
|
||||
- name: setup go
|
||||
uses: actions/setup-go@v5
|
||||
with:
|
||||
cache-dependency-path: "**/*.sum"
|
||||
go-version: 'stable'
|
||||
- name: setup go work
|
||||
env:
|
||||
GOWORK: /workspace/${{ github.repository_owner }}/go.work
|
||||
run: |
|
||||
go work init
|
||||
go work use .
|
||||
go work use micro-tests
|
||||
- name: setup deps
|
||||
env:
|
||||
GOWORK: /workspace/${{ github.repository_owner }}/go.work
|
||||
run: go get -v ./...
|
||||
- name: run tests
|
||||
env:
|
||||
INTEGRATION_TESTS: yes
|
||||
GOWORK: /workspace/${{ github.repository_owner }}/go.work
|
||||
run: |
|
||||
cd micro-tests
|
||||
go test -mod readonly -v ./... || true
|
||||
19
.github/renovate.json
vendored
19
.github/renovate.json
vendored
@@ -1,19 +0,0 @@
|
||||
{
|
||||
"extends": [
|
||||
"config:base"
|
||||
],
|
||||
"packageRules": [
|
||||
{
|
||||
"matchUpdateTypes": ["minor", "patch", "pin", "digest"],
|
||||
"automerge": true
|
||||
},
|
||||
{
|
||||
"groupName": "all deps",
|
||||
"separateMajorMinor": true,
|
||||
"groupSlug": "all",
|
||||
"packagePatterns": [
|
||||
"*"
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
13
.github/stale.sh
vendored
13
.github/stale.sh
vendored
@@ -1,13 +0,0 @@
|
||||
#!/bin/bash -ex
|
||||
|
||||
export PATH=$PATH:$(pwd)/bin
|
||||
export GO111MODULE=on
|
||||
export GOBIN=$(pwd)/bin
|
||||
|
||||
#go get github.com/rvflash/goup@v0.4.1
|
||||
|
||||
#goup -v ./...
|
||||
#go get github.com/psampaz/go-mod-outdated@v0.6.0
|
||||
go list -u -m -mod=mod -json all | go-mod-outdated -update -direct -ci || true
|
||||
|
||||
#go list -u -m -json all | go-mod-outdated -update
|
||||
46
.github/workflows/build.yml
vendored
46
.github/workflows/build.yml
vendored
@@ -1,46 +0,0 @@
|
||||
name: build
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- master
|
||||
jobs:
|
||||
test:
|
||||
name: test
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: setup
|
||||
uses: actions/setup-go@v2
|
||||
with:
|
||||
go-version: 1.16
|
||||
- name: checkout
|
||||
uses: actions/checkout@v2
|
||||
- name: cache
|
||||
uses: actions/cache@v2
|
||||
with:
|
||||
path: ~/go/pkg/mod
|
||||
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
|
||||
restore-keys: ${{ runner.os }}-go-
|
||||
- name: deps
|
||||
run: go get -v -t -d ./...
|
||||
- name: test
|
||||
env:
|
||||
INTEGRATION_TESTS: yes
|
||||
run: go test -mod readonly -v ./...
|
||||
lint:
|
||||
name: lint
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: checkout
|
||||
uses: actions/checkout@v2
|
||||
- name: lint
|
||||
uses: golangci/golangci-lint-action@v2
|
||||
continue-on-error: true
|
||||
with:
|
||||
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
|
||||
version: v1.30
|
||||
# Optional: working directory, useful for monorepos
|
||||
# working-directory: somedir
|
||||
# Optional: golangci-lint command line arguments.
|
||||
# args: --issues-exit-code=0
|
||||
# Optional: show only new issues if it's a pull request. The default value is `false`.
|
||||
# only-new-issues: true
|
||||
46
.github/workflows/pr.yml
vendored
46
.github/workflows/pr.yml
vendored
@@ -1,46 +0,0 @@
|
||||
name: prbuild
|
||||
on:
|
||||
pull_request:
|
||||
branches:
|
||||
- master
|
||||
jobs:
|
||||
test:
|
||||
name: test
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: setup
|
||||
uses: actions/setup-go@v2
|
||||
with:
|
||||
go-version: 1.16
|
||||
- name: checkout
|
||||
uses: actions/checkout@v2
|
||||
- name: cache
|
||||
uses: actions/cache@v2
|
||||
with:
|
||||
path: ~/go/pkg/mod
|
||||
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
|
||||
restore-keys: ${{ runner.os }}-go-
|
||||
- name: deps
|
||||
run: go get -v -t -d ./...
|
||||
- name: test
|
||||
env:
|
||||
INTEGRATION_TESTS: yes
|
||||
run: go test -mod readonly -v ./...
|
||||
lint:
|
||||
name: lint
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: checkout
|
||||
uses: actions/checkout@v2
|
||||
- name: lint
|
||||
uses: golangci/golangci-lint-action@v2
|
||||
continue-on-error: true
|
||||
with:
|
||||
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
|
||||
version: v1.30
|
||||
# Optional: working directory, useful for monorepos
|
||||
# working-directory: somedir
|
||||
# Optional: golangci-lint command line arguments.
|
||||
# args: --issues-exit-code=0
|
||||
# Optional: show only new issues if it's a pull request. The default value is `false`.
|
||||
# only-new-issues: true
|
||||
5
.golangci.yml
Normal file
5
.golangci.yml
Normal file
@@ -0,0 +1,5 @@
|
||||
run:
|
||||
concurrency: 8
|
||||
deadline: 5m
|
||||
issues-exit-code: 1
|
||||
tests: true
|
||||
54
codec_test.go
Normal file
54
codec_test.go
Normal file
@@ -0,0 +1,54 @@
|
||||
package yaml
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"testing"
|
||||
|
||||
"go.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 `yaml:"name" codec:"flatten"`
|
||||
}{
|
||||
One: "xx",
|
||||
Name: &codec.Frame{Data: []byte("test")},
|
||||
}
|
||||
|
||||
buf, err := NewCodec(codec.Flatten(true)).Marshal(s)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !bytes.Equal(buf, []byte(`test`)) {
|
||||
t.Fatalf("bytes not equal %s != %s", buf, `test`)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNativeYamlTags(t *testing.T) {
|
||||
s := &struct {
|
||||
One string `yaml:"first"`
|
||||
}{
|
||||
One: "",
|
||||
}
|
||||
|
||||
err := NewCodec().Unmarshal([]byte(`first: "val"`), s)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if s.One != "val" {
|
||||
t.Fatalf("XXX %#+v\n", s)
|
||||
}
|
||||
}
|
||||
10
go.mod
10
go.mod
@@ -1,8 +1,10 @@
|
||||
module github.com/unistack-org/micro-codec-yaml/v3
|
||||
module go.unistack.org/micro-codec-yaml/v3
|
||||
|
||||
go 1.15
|
||||
go 1.16
|
||||
|
||||
require (
|
||||
github.com/ghodss/yaml v1.0.0
|
||||
github.com/unistack-org/micro/v3 v3.3.13
|
||||
go.unistack.org/micro-proto/v3 v3.4.1
|
||||
go.unistack.org/micro/v3 v3.10.94
|
||||
google.golang.org/protobuf v1.34.2 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1
|
||||
)
|
||||
|
||||
112
yaml.go
112
yaml.go
@@ -1,93 +1,77 @@
|
||||
// Package yaml provides a yaml codec
|
||||
package yaml
|
||||
package yaml // import "go.unistack.org/micro-codec-yaml/v3"
|
||||
|
||||
import (
|
||||
"io"
|
||||
"io/ioutil"
|
||||
|
||||
"github.com/ghodss/yaml"
|
||||
"github.com/unistack-org/micro/v3/codec"
|
||||
pb "go.unistack.org/micro-proto/v3/codec"
|
||||
"go.unistack.org/micro/v3/codec"
|
||||
rutil "go.unistack.org/micro/v3/util/reflect"
|
||||
yaml "gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
type yamlCodec struct{}
|
||||
type yamlCodec struct {
|
||||
opts codec.Options
|
||||
}
|
||||
|
||||
func (c *yamlCodec) Marshal(b interface{}) ([]byte, error) {
|
||||
switch m := b.(type) {
|
||||
case nil:
|
||||
var _ codec.Codec = &yamlCodec{}
|
||||
|
||||
func (c *yamlCodec) Marshal(v interface{}, opts ...codec.Option) ([]byte, error) {
|
||||
if v == nil {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
options := c.opts
|
||||
for _, o := range opts {
|
||||
o(&options)
|
||||
}
|
||||
|
||||
if options.Flatten {
|
||||
if nv, nerr := rutil.StructFieldByTag(v, options.TagName, "flatten"); nerr == nil {
|
||||
v = nv
|
||||
}
|
||||
}
|
||||
|
||||
switch m := v.(type) {
|
||||
case *codec.Frame:
|
||||
return m.Data, nil
|
||||
case *pb.Frame:
|
||||
return m.Data, nil
|
||||
}
|
||||
|
||||
return yaml.Marshal(b)
|
||||
return yaml.Marshal(v)
|
||||
}
|
||||
|
||||
func (c *yamlCodec) Unmarshal(b []byte, v interface{}) error {
|
||||
if b == nil {
|
||||
func (c *yamlCodec) Unmarshal(b []byte, v interface{}, opts ...codec.Option) error {
|
||||
if len(b) == 0 || v == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
options := c.opts
|
||||
for _, o := range opts {
|
||||
o(&options)
|
||||
}
|
||||
|
||||
if options.Flatten {
|
||||
if nv, nerr := rutil.StructFieldByTag(v, options.TagName, "flatten"); nerr == nil {
|
||||
v = nv
|
||||
}
|
||||
}
|
||||
|
||||
switch m := v.(type) {
|
||||
case nil:
|
||||
return nil
|
||||
case *codec.Frame:
|
||||
m.Data = b
|
||||
return nil
|
||||
case *pb.Frame:
|
||||
m.Data = b
|
||||
return nil
|
||||
}
|
||||
|
||||
return yaml.Unmarshal(b, v)
|
||||
}
|
||||
|
||||
func (c *yamlCodec) ReadHeader(conn io.Reader, m *codec.Message, t codec.MessageType) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *yamlCodec) ReadBody(conn io.Reader, b interface{}) error {
|
||||
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
|
||||
}
|
||||
|
||||
buf, err := ioutil.ReadAll(conn)
|
||||
if err != nil {
|
||||
return err
|
||||
} else if buf == nil {
|
||||
// not needed but similar changes in all codecs
|
||||
return nil
|
||||
}
|
||||
|
||||
return yaml.Unmarshal(buf, b)
|
||||
}
|
||||
|
||||
func (c *yamlCodec) 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
|
||||
}
|
||||
|
||||
buf, err := yaml.Marshal(b)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = conn.Write(buf)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func (c *yamlCodec) String() string {
|
||||
return "yaml"
|
||||
}
|
||||
|
||||
func NewCodec() codec.Codec {
|
||||
return &yamlCodec{}
|
||||
func NewCodec(opts ...codec.Option) *yamlCodec {
|
||||
return &yamlCodec{opts: codec.NewOptions(opts...)}
|
||||
}
|
||||
|
||||
18
yaml_test.go
18
yaml_test.go
@@ -1,18 +0,0 @@
|
||||
package yaml
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"testing"
|
||||
)
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user