smartpb

package module
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 23, 2023 License: Apache-2.0 Imports: 15 Imported by: 0

README

smart-pb

智能数据传输协议,当pb变更后,无需通知使用方,可自动感知当前message的格式

主要原理是:

  • 1: 传输的真正包是Payload
  • 2: Payload有两个字段,data用于存储消息内容,pmd用于存储消息格式
  • 3: 使用Marshal方法把整个Payload变为Message对象
  • 4: 对Message再使用Unmarshal方法把包序列化发送给使用方
  • 5: 使用方收到包后,可以使用Sink方法把整个包反序列化为Message对象
  • 6: 使用方可以使用GetXXX系列方法,可以读取Message中的数据值
// 消息载体
message payload {
  bytes pmd = 1;  // proto message desc
  bytes data = 2; // message 内容
}

2、主要方法介绍

具体使用方式可以见 message_test.go 中单元测试

2.1、proto.Message -> Message
message := Marshal(msg) // proto.Message -> Message

buf := Unmarshal(message) // Message -> []byte

message := Sink(buf) // []byte -> Message
2.2、Get方法
var api = &Api4{
    Users: []*Api2{
        {Id: 100, Info: &Api1{Name: "aa", Email: "aa@t.com"}},
        {Id: 101, Info: &Api1{Name: "bb", Email: "bb@t.com"}},
        {Id: 102, Info: &Api1{Name: "cc", Email: "cc@t.com"}},
    },
    Admin: &Api4Api6{
        V2: &Api2{
            Id: 12345,
            Info: &Api1{
                Name:  "tx",
                Email: "k@te.com",
            },
        },
    },
}

msg := Marshal(api)
Convey("Get\n", t, func() {
    var adminName string
    err := msg.Get("admin.v2.info.name", &adminName)
    So(err, ShouldBeNil)
    So(adminName, ShouldEqual, "tx")
})

Convey("Get_pb_array\n", t, func() {
    var users []*Api2
    err := msg.Get("users", &users)
    So(err, ShouldBeNil)
    So(users, ShouldHaveLength, 3)
    t.Logf("users:%+v", users)
})
2.3、GetXXX方法
var api = &Api4{
    Users: []*Api2{
        {Id: 100, Info: &Api1{Name: "aa", Email: "aa@t.com"}},
        {Id: 101, Info: &Api1{Name: "bb", Email: "bb@t.com"}},
        {Id: 102, Info: &Api1{Name: "cc", Email: "cc@t.com"}},
    },
    Admin: &Api4Api6{
        V2: &Api2{
            Id: 12345,
            Info: &Api1{
                Name:  "tx",
                Email: "k@te.com",
            },
        },
    },
}

msg := Marshal(api)
Convey("GetInt64\n", t, func() {
    id, err := msg.GetInt64("users[1].id")
    So(err, ShouldBeNil)
    So(id, ShouldEqual, 101)
})

Convey("GetInt64_nested\n", t, func() {
    adminId, err := msg.GetInt64("admin.v2.id")
    So(err, ShouldBeNil)
    So(adminId, ShouldEqual, 12345)
})

Convey("GetString_array\n", t, func() {
    email, err := msg.GetString("users[2].info.email")
    So(err, ShouldBeNil)
    So(email, ShouldEqual, "cc@t.com")
})

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	Level_name = map[int32]string{
		0: "Easy",
		1: "Middle",
		2: "Hard",
	}
	Level_value = map[string]int32{
		"Easy":   0,
		"Middle": 1,
		"Hard":   2,
	}
)

Enum value maps for Level.

View Source
var File_payload_proto protoreflect.FileDescriptor

Functions

func Unmarshal

func Unmarshal(m *Message) []byte

Unmarshal PBMD -> Message

Types

type Api1

type Api1 struct {
	Name  string `protobuf:"bytes,1,opt,name=name,proto3" json:"name,omitempty"`
	Email string `protobuf:"bytes,2,opt,name=email,proto3" json:"email,omitempty"`
	V3    *Api3  `protobuf:"bytes,3,opt,name=v3,proto3" json:"v3,omitempty"`
	Lv    Level  `protobuf:"varint,4,opt,name=lv,proto3,enum=protojson.Level" json:"lv,omitempty"`
	// contains filtered or unexported fields
}

func (*Api1) Descriptor deprecated

func (*Api1) Descriptor() ([]byte, []int)

Deprecated: Use Api1.ProtoReflect.Descriptor instead.

func (*Api1) GetEmail

func (x *Api1) GetEmail() string

func (*Api1) GetLv

func (x *Api1) GetLv() Level

func (*Api1) GetName

func (x *Api1) GetName() string

func (*Api1) GetV3

func (x *Api1) GetV3() *Api3

func (*Api1) ProtoMessage

func (*Api1) ProtoMessage()

func (*Api1) ProtoReflect

func (x *Api1) ProtoReflect() protoreflect.Message

func (*Api1) Reset

func (x *Api1) Reset()

func (*Api1) String

func (x *Api1) String() string

type Api2

type Api2 struct {
	Id   int64   `protobuf:"varint,1,opt,name=id,proto3" json:"id,omitempty"`
	Info *Api1   `protobuf:"bytes,2,opt,name=info,proto3" json:"info,omitempty"`
	Lvs  []Level `protobuf:"varint,3,rep,packed,name=lvs,proto3,enum=protojson.Level" json:"lvs,omitempty"`
	// contains filtered or unexported fields
}

func (*Api2) Descriptor deprecated

func (*Api2) Descriptor() ([]byte, []int)

Deprecated: Use Api2.ProtoReflect.Descriptor instead.

func (*Api2) GetId

func (x *Api2) GetId() int64

func (*Api2) GetInfo

func (x *Api2) GetInfo() *Api1

func (*Api2) GetLvs

func (x *Api2) GetLvs() []Level

func (*Api2) ProtoMessage

func (*Api2) ProtoMessage()

func (*Api2) ProtoReflect

func (x *Api2) ProtoReflect() protoreflect.Message

func (*Api2) Reset

func (x *Api2) Reset()

func (*Api2) String

func (x *Api2) String() string

type Api3

type Api3 struct {
	Uuid  string   `protobuf:"bytes,1,opt,name=uuid,proto3" json:"uuid,omitempty"`
	Likes []string `protobuf:"bytes,2,rep,name=likes,proto3" json:"likes,omitempty"`
	Times int32    `protobuf:"varint,3,opt,name=times,proto3" json:"times,omitempty"`
	V2    *Api2    `protobuf:"bytes,4,opt,name=v2,proto3" json:"v2,omitempty"`
	// contains filtered or unexported fields
}

func (*Api3) Descriptor deprecated

func (*Api3) Descriptor() ([]byte, []int)

Deprecated: Use Api3.ProtoReflect.Descriptor instead.

func (*Api3) GetLikes

func (x *Api3) GetLikes() []string

func (*Api3) GetTimes

func (x *Api3) GetTimes() int32

func (*Api3) GetUuid

func (x *Api3) GetUuid() string

func (*Api3) GetV2

func (x *Api3) GetV2() *Api2

func (*Api3) ProtoMessage

func (*Api3) ProtoMessage()

func (*Api3) ProtoReflect

func (x *Api3) ProtoReflect() protoreflect.Message

func (*Api3) Reset

func (x *Api3) Reset()

func (*Api3) String

func (x *Api3) String() string

type Api4

type Api4 struct {
	Users []*Api2   `protobuf:"bytes,1,rep,name=users,proto3" json:"users,omitempty"`
	Admin *Api4Api6 `protobuf:"bytes,2,opt,name=admin,proto3" json:"admin,omitempty"`
	// contains filtered or unexported fields
}

func (*Api4) Descriptor deprecated

func (*Api4) Descriptor() ([]byte, []int)

Deprecated: Use Api4.ProtoReflect.Descriptor instead.

func (*Api4) GetAdmin

func (x *Api4) GetAdmin() *Api4Api6

func (*Api4) GetUsers

func (x *Api4) GetUsers() []*Api2

func (*Api4) ProtoMessage

func (*Api4) ProtoMessage()

func (*Api4) ProtoReflect

func (x *Api4) ProtoReflect() protoreflect.Message

func (*Api4) Reset

func (x *Api4) Reset()

func (*Api4) String

func (x *Api4) String() string

type Api4Api6

type Api4Api6 struct {
	V2 *Api2 `protobuf:"bytes,1,opt,name=v2,proto3" json:"v2,omitempty"`
	// contains filtered or unexported fields
}

func (*Api4Api6) Descriptor deprecated

func (*Api4Api6) Descriptor() ([]byte, []int)

Deprecated: Use Api4Api6.ProtoReflect.Descriptor instead.

func (*Api4Api6) GetV2

func (x *Api4Api6) GetV2() *Api2

func (*Api4Api6) ProtoMessage

func (*Api4Api6) ProtoMessage()

func (*Api4Api6) ProtoReflect

func (x *Api4Api6) ProtoReflect() protoreflect.Message

func (*Api4Api6) Reset

func (x *Api4Api6) Reset()

func (*Api4Api6) String

func (x *Api4Api6) String() string

type Api5

type Api5 struct {
	V3 []*Api3 `protobuf:"bytes,1,rep,name=v3,proto3" json:"v3,omitempty"` // 对象数组
	V4 []*Api4 `protobuf:"bytes,2,rep,name=v4,proto3" json:"v4,omitempty"` // 对象数组的数组
	// contains filtered or unexported fields
}

func (*Api5) Descriptor deprecated

func (*Api5) Descriptor() ([]byte, []int)

Deprecated: Use Api5.ProtoReflect.Descriptor instead.

func (*Api5) GetV3

func (x *Api5) GetV3() []*Api3

func (*Api5) GetV4

func (x *Api5) GetV4() []*Api4

func (*Api5) ProtoMessage

func (*Api5) ProtoMessage()

func (*Api5) ProtoReflect

func (x *Api5) ProtoReflect() protoreflect.Message

func (*Api5) Reset

func (x *Api5) Reset()

func (*Api5) String

func (x *Api5) String() string

type Level

type Level int32
const (
	Level_Easy   Level = 0
	Level_Middle Level = 1
	Level_Hard   Level = 2
)

func (Level) Descriptor

func (Level) Descriptor() protoreflect.EnumDescriptor

func (Level) Enum

func (x Level) Enum() *Level

func (Level) EnumDescriptor deprecated

func (Level) EnumDescriptor() ([]byte, []int)

Deprecated: Use Level.Descriptor instead.

func (Level) Number

func (x Level) Number() protoreflect.EnumNumber

func (Level) String

func (x Level) String() string

func (Level) Type

func (Level) Type() protoreflect.EnumType

type Message

type Message struct {
	Data     []byte                  `json:"data"`
	Metadata []byte                  `json:"metadata"`
	DMD      *desc.MessageDescriptor `json:"dmd"`
	Msg      *dynamic.Message        `json:"msg"`
}

Message ...

func Marshal

func Marshal(msg proto.Message) *Message

Marshal Message -> PBMD

func Sink

func Sink(buf []byte) *Message

Sink 直接[]byte -> PBMD

func (Message) Get

func (m Message) Get(path string, holder interface{}) error

Get 从Message中按jsonpath风格取值

func (Message) GetBool

func (m Message) GetBool(path string) (bool, error)

GetBool 从Message中按jsonpath风格取值

func (Message) GetBytes

func (s Message) GetBytes(path string) ([]byte, error)

GetBytes 从Message中按jsonpath风格取值

func (Message) GetFloat32

func (m Message) GetFloat32(path string) (float32, error)

GetFloat32 从Message中按jsonpath风格取值

func (Message) GetFloat64

func (m Message) GetFloat64(path string) (float64, error)

GetFloat64 从Message中按jsonpath风格取值

func (Message) GetInt16

func (m Message) GetInt16(path string) (int16, error)

GetInt16 从Message中按jsonpath风格取值

func (Message) GetInt32

func (m Message) GetInt32(path string) (int32, error)

GetInt32 从Message中按jsonpath风格取值

func (Message) GetInt64

func (m Message) GetInt64(path string) (int64, error)

GetInt64 从Message中按jsonpath风格取值

func (Message) GetInt8

func (m Message) GetInt8(path string) (int8, error)

GetInt8 从Message中按jsonpath风格取值

func (Message) GetString

func (s Message) GetString(path string) (string, error)

GetString 从Message中按jsonpath风格取值

func (Message) GetUint16

func (m Message) GetUint16(path string) (uint16, error)

GetUint16 从Message中按jsonpath风格取值

func (Message) GetUint32

func (m Message) GetUint32(path string) (uint32, error)

GetUint32 从Message中按jsonpath风格取值

func (Message) GetUint64

func (m Message) GetUint64(path string) (uint64, error)

GetUint64 从Message中按jsonpath风格取值

func (Message) GetUint8

func (m Message) GetUint8(path string) (uint8, error)

GetUint8 从Message中按jsonpath风格取值

func (Message) Pretty

func (m Message) Pretty()

Pretty 直接格式化打印,用于调试

func (Message) String

func (m Message) String() string

String 序列化为json格式文本

type Payload

type Payload struct {
	Pmd  []byte `protobuf:"bytes,1,opt,name=pmd,proto3" json:"pmd,omitempty"`   // proto message desc
	Data []byte `protobuf:"bytes,2,opt,name=data,proto3" json:"data,omitempty"` // message 内容
	// contains filtered or unexported fields
}

消息载体

func (*Payload) Descriptor deprecated

func (*Payload) Descriptor() ([]byte, []int)

Deprecated: Use Payload.ProtoReflect.Descriptor instead.

func (*Payload) GetData

func (x *Payload) GetData() []byte

func (*Payload) GetPmd

func (x *Payload) GetPmd() []byte

func (*Payload) ProtoMessage

func (*Payload) ProtoMessage()

func (*Payload) ProtoReflect

func (x *Payload) ProtoReflect() protoreflect.Message

func (*Payload) Reset

func (x *Payload) Reset()

func (*Payload) String

func (x *Payload) String() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL