Compare commits
8 Commits
Author | SHA1 | Date | |
---|---|---|---|
08b68d26c7 | |||
8e6ab14dd4 | |||
62db549d9a | |||
|
44e349c061 | ||
f250aeb50d | |||
b19b8813b6 | |||
0476b2dd5c | |||
47feb209a4 |
@@ -1,6 +1,6 @@
|
|||||||
---
|
---
|
||||||
name: Bug report
|
name: Bug report
|
||||||
about: For reporting bugs in go-micro
|
about: For reporting bugs in micro
|
||||||
title: "[BUG]"
|
title: "[BUG]"
|
||||||
labels: ''
|
labels: ''
|
||||||
assignees: ''
|
assignees: ''
|
||||||
@@ -16,9 +16,3 @@ assignees: ''
|
|||||||
**How to reproduce the bug:**
|
**How to reproduce the bug:**
|
||||||
|
|
||||||
If possible, please include a minimal code snippet here.
|
If possible, please include a minimal code snippet here.
|
||||||
|
|
||||||
**Environment:**
|
|
||||||
Go Version: please paste `go version` output here
|
|
||||||
```
|
|
||||||
please paste `go env` output here
|
|
||||||
```
|
|
||||||
|
@@ -1,6 +1,6 @@
|
|||||||
---
|
---
|
||||||
name: Feature request / Enhancement
|
name: Feature request / Enhancement
|
||||||
about: If you have a need not served by go-micro
|
about: If you have a need not served by micro
|
||||||
title: "[FEATURE]"
|
title: "[FEATURE]"
|
||||||
labels: ''
|
labels: ''
|
||||||
assignees: ''
|
assignees: ''
|
||||||
|
@@ -1,14 +1,8 @@
|
|||||||
---
|
---
|
||||||
name: Question
|
name: Question
|
||||||
about: Ask a question about go-micro
|
about: Ask a question about micro
|
||||||
title: ''
|
title: ''
|
||||||
labels: ''
|
labels: ''
|
||||||
assignees: ''
|
assignees: ''
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
Before asking, please check if your question has already been answered:
|
|
||||||
|
|
||||||
1. Check the documentation - https://micro.mu/docs/
|
|
||||||
2. Check the examples and plugins - https://github.com/micro/examples & https://github.com/micro/go-plugins
|
|
||||||
3. Search existing issues
|
|
||||||
|
28
.gitea/autoapprove.yml
Normal file
28
.gitea/autoapprove.yml
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
name: "autoapprove"
|
||||||
|
|
||||||
|
on:
|
||||||
|
pull_request_target:
|
||||||
|
types: [assigned, opened, synchronize, reopened]
|
||||||
|
workflow_run:
|
||||||
|
workflows: ["prbuild"]
|
||||||
|
types:
|
||||||
|
- completed
|
||||||
|
|
||||||
|
permissions:
|
||||||
|
pull-requests: write
|
||||||
|
contents: write
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
autoapprove:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- name: approve
|
||||||
|
run: [ "curl -o tea https://dl.gitea.com/tea/main/tea-main-linux-amd64",
|
||||||
|
"chmod +x ./tea",
|
||||||
|
"./tea login add --name unistack --token ${{ secrets.GITHUB_TOKEN }} --url https://git.unistack.org",
|
||||||
|
"./tea pr --repo ${{ github.event.repository.name }}"
|
||||||
|
]
|
||||||
|
if: github.actor == 'vtolstov'
|
||||||
|
id: approve
|
||||||
|
with:
|
||||||
|
github-token: ${{ secrets.GITHUB_TOKEN }}
|
21
go.mod
21
go.mod
@@ -1,16 +1,23 @@
|
|||||||
module go.unistack.org/micro-codec-segmentio/v4
|
module go.unistack.org/micro-codec-segmentio/v4
|
||||||
|
|
||||||
go 1.19
|
go 1.23.0
|
||||||
|
|
||||||
|
toolchain go1.24.1
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/segmentio/encoding v0.3.6
|
github.com/segmentio/encoding v0.4.1
|
||||||
go.unistack.org/micro-proto/v4 v4.0.0
|
go.unistack.org/micro-proto/v4 v4.1.0
|
||||||
go.unistack.org/micro/v4 v4.0.1
|
go.unistack.org/micro/v4 v4.1.2
|
||||||
google.golang.org/protobuf v1.28.1
|
google.golang.org/protobuf v1.36.5
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/google/go-cmp v0.5.7 // indirect
|
github.com/ash3in/uuidv8 v1.2.0 // indirect
|
||||||
|
github.com/google/go-cmp v0.6.0 // indirect
|
||||||
|
github.com/google/uuid v1.6.0 // indirect
|
||||||
|
github.com/kr/pretty v0.3.1 // indirect
|
||||||
|
github.com/matoous/go-nanoid v1.5.1 // indirect
|
||||||
github.com/segmentio/asm v1.2.0 // indirect
|
github.com/segmentio/asm v1.2.0 // indirect
|
||||||
golang.org/x/sys v0.6.0 // indirect
|
golang.org/x/sys v0.31.0 // indirect
|
||||||
|
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||||
)
|
)
|
||||||
|
53
go.sum
53
go.sum
@@ -1,21 +1,36 @@
|
|||||||
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
|
github.com/DATA-DOG/go-sqlmock v1.5.2 h1:OcvFkGmslmlZibjAjaHm3L//6LiuBgolP7OputlJIzU=
|
||||||
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
github.com/DATA-DOG/go-sqlmock v1.5.2/go.mod h1:88MAG/4G7SMwSE3CeA0ZKzrT5CiOU3OJ+JlNzwDqpNU=
|
||||||
github.com/google/go-cmp v0.5.7 h1:81/ik6ipDQS2aGcBfIN5dHDB36BwrStyeAQquSYCV4o=
|
github.com/ash3in/uuidv8 v1.2.0 h1:2oogGdtCPwaVtyvPPGin4TfZLtOGE5F+W++E880G6SI=
|
||||||
github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE=
|
github.com/ash3in/uuidv8 v1.2.0/go.mod h1:BnU0wJBxnzdEKmVg4xckBkD+VZuecTFTUP3M0dWgyY4=
|
||||||
github.com/segmentio/asm v1.1.3/go.mod h1:Ld3L4ZXGNcSLRg4JBsZ3//1+f/TjYl0Mzen/DQy1EJg=
|
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
|
||||||
|
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
|
||||||
|
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
||||||
|
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
||||||
|
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||||
|
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
|
||||||
|
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
|
||||||
|
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
|
||||||
|
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
|
||||||
|
github.com/matoous/go-nanoid v1.5.1 h1:aCjdvTyO9LLnTIi0fgdXhOPPvOHjpXN6Ik9DaNjIct4=
|
||||||
|
github.com/matoous/go-nanoid v1.5.1/go.mod h1:zyD2a71IubI24efhpvkJz+ZwfwagzgSO6UNiFsZKN7U=
|
||||||
|
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
|
||||||
|
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
|
||||||
|
github.com/rogpeppe/go-internal v1.13.1 h1:KvO1DLK/DRN07sQ1LQKScxyZJuNnedQ5/wKSR38lUII=
|
||||||
|
github.com/rogpeppe/go-internal v1.13.1/go.mod h1:uMEvuHeurkdAXX61udpOXGD/AzZDWNMNyH2VO9fmH0o=
|
||||||
github.com/segmentio/asm v1.2.0 h1:9BQrFxC+YOHJlTlHGkTrFWf59nbL3XnCoFLTwDCI7ys=
|
github.com/segmentio/asm v1.2.0 h1:9BQrFxC+YOHJlTlHGkTrFWf59nbL3XnCoFLTwDCI7ys=
|
||||||
github.com/segmentio/asm v1.2.0/go.mod h1:BqMnlJP91P8d+4ibuonYZw9mfnzI9HfxselHZr5aAcs=
|
github.com/segmentio/asm v1.2.0/go.mod h1:BqMnlJP91P8d+4ibuonYZw9mfnzI9HfxselHZr5aAcs=
|
||||||
github.com/segmentio/encoding v0.3.6 h1:E6lVLyDPseWEulBmCmAKPanDd3jiyGDo5gMcugCRwZQ=
|
github.com/segmentio/encoding v0.4.1 h1:KLGaLSW0jrmhB58Nn4+98spfvPvmo4Ci1P/WIQ9wn7w=
|
||||||
github.com/segmentio/encoding v0.3.6/go.mod h1:n0JeuIqEQrQoPDGsjo8UNd1iA0U8d8+oHAA4E3G3OxM=
|
github.com/segmentio/encoding v0.4.1/go.mod h1:/d03Cd8PoaDeceuhUUUQWjU0KhWjrmYrWPgtJHYZSnI=
|
||||||
go.unistack.org/micro-proto/v4 v4.0.0 h1:+TAwKSlx48eLsNO2E2cO0JLE8we1KU2AuWe9Q310FuU=
|
go.unistack.org/micro-proto/v4 v4.1.0 h1:qPwL2n/oqh9RE3RTTDgt28XK3QzV597VugQPaw9lKUk=
|
||||||
go.unistack.org/micro-proto/v4 v4.0.0/go.mod h1:ArmK7o+uFvxSY3dbJhKBBX4Pm1rhWdLEFf3LxBrMtec=
|
go.unistack.org/micro-proto/v4 v4.1.0/go.mod h1:ArmK7o+uFvxSY3dbJhKBBX4Pm1rhWdLEFf3LxBrMtec=
|
||||||
go.unistack.org/micro/v4 v4.0.1 h1:xo1IxbVfgh8i0eY0VeYa3cbb13u5n/Mxnp3FOgWD4Jo=
|
go.unistack.org/micro/v4 v4.1.2 h1:9SOlPYyPNNFpg1A7BsvhDyQm3gysLH1AhWbDCp1hyoY=
|
||||||
go.unistack.org/micro/v4 v4.0.1/go.mod h1:p/J5UcSJjfHsWGT31uKoghQ5rUQZzQJBAFy+Z4+ZVMs=
|
go.unistack.org/micro/v4 v4.1.2/go.mod h1:lr3oYED8Ay1vjK68QqRw30QOtdk/ffpZqMFDasOUhKw=
|
||||||
golang.org/x/sys v0.0.0-20211110154304-99a53858aa08/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
golang.org/x/sys v0.31.0 h1:ioabZlmFYtWhL+TRYpcnNlLwhyxaM9kWTDEmfnprqik=
|
||||||
golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ=
|
golang.org/x/sys v0.31.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
|
||||||
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
google.golang.org/protobuf v1.36.5 h1:tPhr+woSbjfYvY6/GPufUoYizxw1cF/yFoxJ2fmpwlM=
|
||||||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
|
google.golang.org/protobuf v1.36.5/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
|
||||||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
|
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
|
||||||
google.golang.org/protobuf v1.28.1 h1:d0NfwRgPtno5B1Wa6L2DAG+KivqkdutMf1UhdNx175w=
|
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
|
||||||
google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
|
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||||
|
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
@@ -28,7 +28,7 @@ func TestFrameFlatten(t *testing.T) {
|
|||||||
Name: &codec.Frame{Data: []byte("test")},
|
Name: &codec.Frame{Data: []byte("test")},
|
||||||
}
|
}
|
||||||
|
|
||||||
buf, err := NewCodec().Marshal(s)
|
buf, err := NewCodec(codec.Flatten(true)).Marshal(s)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@@ -36,15 +36,3 @@ func TestFrameFlatten(t *testing.T) {
|
|||||||
t.Fatalf("bytes not equal %s != %s", buf, `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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
154
json/json.go
154
json/json.go
@@ -1,9 +1,7 @@
|
|||||||
// 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"
|
|
||||||
|
|
||||||
"github.com/segmentio/encoding/json"
|
"github.com/segmentio/encoding/json"
|
||||||
pb "go.unistack.org/micro-proto/v4/codec"
|
pb "go.unistack.org/micro-proto/v4/codec"
|
||||||
"go.unistack.org/micro/v4/codec"
|
"go.unistack.org/micro/v4/codec"
|
||||||
@@ -21,10 +19,9 @@ var (
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
var _ codec.Codec = &jsonCodec{}
|
var (
|
||||||
|
_ codec.Codec = (*jsonCodec)(nil)
|
||||||
const (
|
_ codec.CodecV2 = (*jsonCodecV2)(nil)
|
||||||
flattenTag = "flatten"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type JsonMarshalOptions struct {
|
type JsonMarshalOptions struct {
|
||||||
@@ -49,6 +46,12 @@ type jsonCodec struct {
|
|||||||
decodeFlags json.ParseFlags
|
decodeFlags json.ParseFlags
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type jsonCodecV2 struct {
|
||||||
|
opts codec.Options
|
||||||
|
encodeFlags json.AppendFlags
|
||||||
|
decodeFlags json.ParseFlags
|
||||||
|
}
|
||||||
|
|
||||||
func getMarshalFlags(o JsonMarshalOptions) json.AppendFlags {
|
func getMarshalFlags(o JsonMarshalOptions) json.AppendFlags {
|
||||||
var encodeFlags json.AppendFlags
|
var encodeFlags json.AppendFlags
|
||||||
|
|
||||||
@@ -111,8 +114,10 @@ func (c *jsonCodec) Marshal(v interface{}, opts ...codec.Option) ([]byte, error)
|
|||||||
o(&options)
|
o(&options)
|
||||||
}
|
}
|
||||||
|
|
||||||
if nv, err := rutil.StructFieldByTag(v, options.TagName, flattenTag); err == nil {
|
if options.Flatten {
|
||||||
v = nv
|
if nv, err := rutil.StructFieldByTag(v, options.TagName, "flatten"); err == nil {
|
||||||
|
v = nv
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
switch m := v.(type) {
|
switch m := v.(type) {
|
||||||
@@ -120,6 +125,10 @@ func (c *jsonCodec) Marshal(v interface{}, opts ...codec.Option) ([]byte, error)
|
|||||||
return m.Data, nil
|
return m.Data, nil
|
||||||
case *pb.Frame:
|
case *pb.Frame:
|
||||||
return m.Data, nil
|
return m.Data, nil
|
||||||
|
case codec.RawMessage:
|
||||||
|
return []byte(m), nil
|
||||||
|
case *codec.RawMessage:
|
||||||
|
return []byte(*m), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
marshalOptions := DefaultMarshalOptions
|
marshalOptions := DefaultMarshalOptions
|
||||||
@@ -145,8 +154,91 @@ func (c *jsonCodec) Unmarshal(b []byte, v interface{}, opts ...codec.Option) err
|
|||||||
o(&options)
|
o(&options)
|
||||||
}
|
}
|
||||||
|
|
||||||
if nv, err := rutil.StructFieldByTag(v, options.TagName, flattenTag); err == nil {
|
if options.Flatten {
|
||||||
v = nv
|
if nv, err := rutil.StructFieldByTag(v, options.TagName, "flatten"); err == nil {
|
||||||
|
v = nv
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
switch m := v.(type) {
|
||||||
|
case *codec.Frame:
|
||||||
|
m.Data = b
|
||||||
|
return nil
|
||||||
|
case *pb.Frame:
|
||||||
|
m.Data = b
|
||||||
|
return nil
|
||||||
|
case *codec.RawMessage:
|
||||||
|
*m = append((*m)[0:0], b...)
|
||||||
|
return nil
|
||||||
|
case codec.RawMessage:
|
||||||
|
copy(m, b)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *jsonCodec) String() string {
|
||||||
|
return "json"
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *jsonCodecV2) Marshal(b []byte, 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, err := rutil.StructFieldByTag(v, options.TagName, "flatten"); err == nil {
|
||||||
|
v = nv
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
switch m := v.(type) {
|
||||||
|
case *codec.Frame:
|
||||||
|
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
|
||||||
|
b, err = json.Append(b[:0], v, getMarshalFlags(marshalOptions))
|
||||||
|
return b, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *jsonCodecV2) 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, err := rutil.StructFieldByTag(v, options.TagName, "flatten"); err == nil {
|
||||||
|
v = nv
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
switch m := v.(type) {
|
switch m := v.(type) {
|
||||||
@@ -169,44 +261,14 @@ func (c *jsonCodec) Unmarshal(b []byte, v interface{}, opts ...codec.Option) err
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *jsonCodec) ReadHeader(conn io.Reader, m *codec.Message, t codec.MessageType) error {
|
func (c *jsonCodecV2) String() string {
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *jsonCodec) ReadBody(conn io.Reader, v interface{}) error {
|
|
||||||
if v == nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
buf, err := io.ReadAll(conn)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
} else if len(buf) == 0 {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
return c.Unmarshal(buf, v)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *jsonCodec) 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 len(buf) == 0 {
|
|
||||||
return codec.ErrInvalidMessage
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err = conn.Write(buf)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *jsonCodec) String() string {
|
|
||||||
return "json"
|
return "json"
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewCodec(opts ...codec.Option) *jsonCodec {
|
func NewCodec(opts ...codec.Option) *jsonCodec {
|
||||||
return &jsonCodec{opts: codec.NewOptions(opts...)}
|
return &jsonCodec{opts: codec.NewOptions(opts...)}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func NewCodecV2(opts ...codec.Option) *jsonCodecV2 {
|
||||||
|
return &jsonCodecV2{opts: codec.NewOptions(opts...)}
|
||||||
|
}
|
||||||
|
@@ -28,7 +28,7 @@ func TestFrameFlatten(t *testing.T) {
|
|||||||
Name: &codec.Frame{Data: []byte("test")},
|
Name: &codec.Frame{Data: []byte("test")},
|
||||||
}
|
}
|
||||||
|
|
||||||
buf, err := NewCodec().Marshal(s)
|
buf, err := NewCodec(codec.Flatten(true)).Marshal(s)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@@ -36,16 +36,3 @@ func TestFrameFlatten(t *testing.T) {
|
|||||||
t.Fatalf("bytes not equal %s != %s", buf, `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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
@@ -1,9 +1,7 @@
|
|||||||
// 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"
|
|
||||||
|
|
||||||
"github.com/segmentio/encoding/proto"
|
"github.com/segmentio/encoding/proto"
|
||||||
pb "go.unistack.org/micro-proto/v4/codec"
|
pb "go.unistack.org/micro-proto/v4/codec"
|
||||||
"go.unistack.org/micro/v4/codec"
|
"go.unistack.org/micro/v4/codec"
|
||||||
@@ -17,10 +15,6 @@ type protoCodec struct {
|
|||||||
|
|
||||||
var _ codec.Codec = &protoCodec{}
|
var _ codec.Codec = &protoCodec{}
|
||||||
|
|
||||||
const (
|
|
||||||
flattenTag = "flatten"
|
|
||||||
)
|
|
||||||
|
|
||||||
func (c *protoCodec) Marshal(v interface{}, opts ...codec.Option) ([]byte, error) {
|
func (c *protoCodec) Marshal(v interface{}, opts ...codec.Option) ([]byte, error) {
|
||||||
if v == nil {
|
if v == nil {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
@@ -31,8 +25,10 @@ func (c *protoCodec) Marshal(v interface{}, opts ...codec.Option) ([]byte, error
|
|||||||
o(&options)
|
o(&options)
|
||||||
}
|
}
|
||||||
|
|
||||||
if nv, nerr := rutil.StructFieldByTag(v, options.TagName, flattenTag); nerr == nil {
|
if options.Flatten {
|
||||||
v = nv
|
if nv, nerr := rutil.StructFieldByTag(v, options.TagName, "flatten"); nerr == nil {
|
||||||
|
v = nv
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
switch m := v.(type) {
|
switch m := v.(type) {
|
||||||
@@ -47,9 +43,13 @@ func (c *protoCodec) Marshal(v interface{}, opts ...codec.Option) ([]byte, error
|
|||||||
return proto.Marshal(m)
|
return proto.Marshal(m)
|
||||||
case newproto.Message:
|
case newproto.Message:
|
||||||
return proto.Marshal(m)
|
return proto.Marshal(m)
|
||||||
|
case codec.RawMessage:
|
||||||
|
return []byte(m), nil
|
||||||
|
case *codec.RawMessage:
|
||||||
|
return []byte(*m), nil
|
||||||
|
default:
|
||||||
|
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{}, opts ...codec.Option) error {
|
||||||
@@ -62,8 +62,10 @@ func (c *protoCodec) Unmarshal(d []byte, v interface{}, opts ...codec.Option) er
|
|||||||
o(&options)
|
o(&options)
|
||||||
}
|
}
|
||||||
|
|
||||||
if nv, nerr := rutil.StructFieldByTag(v, options.TagName, flattenTag); nerr == nil {
|
if options.Flatten {
|
||||||
v = nv
|
if nv, nerr := rutil.StructFieldByTag(v, options.TagName, "flatten"); nerr == nil {
|
||||||
|
v = nv
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
switch m := v.(type) {
|
switch m := v.(type) {
|
||||||
@@ -73,6 +75,12 @@ func (c *protoCodec) Unmarshal(d []byte, v interface{}, opts ...codec.Option) er
|
|||||||
case *pb.Frame:
|
case *pb.Frame:
|
||||||
m.Data = d
|
m.Data = d
|
||||||
return nil
|
return nil
|
||||||
|
case *codec.RawMessage:
|
||||||
|
*m = append((*m)[0:0], d...)
|
||||||
|
return nil
|
||||||
|
case codec.RawMessage:
|
||||||
|
copy(m, d)
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
switch m := v.(type) {
|
switch m := v.(type) {
|
||||||
@@ -80,43 +88,9 @@ func (c *protoCodec) Unmarshal(d []byte, v interface{}, opts ...codec.Option) er
|
|||||||
return proto.Unmarshal(d, m)
|
return proto.Unmarshal(d, m)
|
||||||
case newproto.Message:
|
case newproto.Message:
|
||||||
return proto.Unmarshal(d, m)
|
return proto.Unmarshal(d, m)
|
||||||
|
default:
|
||||||
}
|
|
||||||
|
|
||||||
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, v interface{}) error {
|
|
||||||
if v == nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
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, v interface{}) error {
|
|
||||||
if v == nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
buf, err := c.Marshal(v)
|
|
||||||
if err != nil {
|
|
||||||
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 {
|
||||||
|
Reference in New Issue
Block a user