j2p

package
v0.2.2-dep3 Latest Latest
Warning

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

Go to latest
Published: Apr 18, 2024 License: Apache-2.0 Imports: 14 Imported by: 1

README

j2p

import "github.com/cloudwego/dynamicgo/conv/j2p"

Index

type BinaryConv

BinaryConv is a converter from json to protobuf binary

type BinaryConv struct {
    // contains filtered or unexported fields
}

func NewBinaryConv
func NewBinaryConv(opts conv.Options) BinaryConv

NewBinaryConv returns a new BinaryConv

func (*BinaryConv) Do
func (self *BinaryConv) Do(ctx context.Context, desc *proto.TypeDescriptor, jbytes []byte) (tbytes []byte, err error)

Do converts json bytes (jbytes) to protobuf binary (tbytes) desc is the protobuf type descriptor of the protobuf binary, usually it the request Message type

Example

package main

import (
	"context"
	"encoding/json"
	"reflect"

	"github.com/cloudwego/dynamicgo/conv"
	"github.com/cloudwego/dynamicgo/testdata/kitex_gen/pb/example2"
	"google.golang.org/protobuf/encoding/protowire"
)

var opts = conv.Options{}

func main() {
	// get descriptor and data
	desc := getExampleDesc()
	data := getExampleData()

	// make BinaryConv
	cv := NewBinaryConv(opts)

	// do conversion
	out, err := cv.Do(context.Background(), desc, data)
	if err != nil {
		panic(err)
	}

	// validate result
	exp := &example2.ExampleReq{}
	err = json.Unmarshal(data, exp)
	if err != nil {
		panic(err)
	}
	act := &example2.ExampleReq{}
	l := 0
	dataLen := len(out)
	// fastRead to get target struct
	for l < dataLen {
		id, wtyp, tagLen := protowire.ConsumeTag(out)
		if tagLen < 0 {
			panic("parseTag failed")
		}
		l += tagLen
		out = out[tagLen:]
		offset, err := act.FastRead(out, int8(wtyp), int32(id))
		if err != nil {
			panic(err)
		}
		out = out[offset:]
		l += offset
	}
	if !reflect.DeepEqual(exp, act) {
		panic("not equal")
	}
}

func (*BinaryConv) DoInto
func (self *BinaryConv) DoInto(ctx context.Context, desc *proto.TypeDescriptor, jbytes []byte, buf *[]byte) error

DoInto behaves like Do, but it writes the result to buffer directly instead of returning a new buffer

Generated by gomarkdoc

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BinaryConv

type BinaryConv struct {
	// contains filtered or unexported fields
}

BinaryConv is a converter from json to protobuf binary

func NewBinaryConv

func NewBinaryConv(opts conv.Options) BinaryConv

NewBinaryConv returns a new BinaryConv

func (*BinaryConv) Do

func (self *BinaryConv) Do(ctx context.Context, desc *proto.TypeDescriptor, jbytes []byte) (tbytes []byte, err error)

Do converts json bytes (jbytes) to protobuf binary (tbytes) desc is the protobuf type descriptor of the protobuf binary, usually it the request Message type

Example
package main

import (
	"context"
	"encoding/json"
	"reflect"

	"github.com/cloudwego/dynamicgo/conv"
	"github.com/cloudwego/dynamicgo/testdata/kitex_gen/pb/example2"
	"google.golang.org/protobuf/encoding/protowire"
)

var opts = conv.Options{}

func main() {
	// get descriptor and data
	desc := getExampleDesc()
	data := getExampleData()

	// make BinaryConv
	cv := NewBinaryConv(opts)

	// do conversion
	out, err := cv.Do(context.Background(), desc, data)
	if err != nil {
		panic(err)
	}

	// validate result
	exp := &example2.ExampleReq{}
	err = json.Unmarshal(data, exp)
	if err != nil {
		panic(err)
	}
	act := &example2.ExampleReq{}
	l := 0
	dataLen := len(out)
	// fastRead to get target struct
	for l < dataLen {
		id, wtyp, tagLen := protowire.ConsumeTag(out)
		if tagLen < 0 {
			panic("parseTag failed")
		}
		l += tagLen
		out = out[tagLen:]
		offset, err := act.FastRead(out, int8(wtyp), int32(id))
		if err != nil {
			panic(err)
		}
		out = out[offset:]
		l += offset
	}
	if !reflect.DeepEqual(exp, act) {
		panic("not equal")
	}
}
Output:

func (*BinaryConv) DoInto

func (self *BinaryConv) DoInto(ctx context.Context, desc *proto.TypeDescriptor, jbytes []byte, buf *[]byte) error

DoInto behaves like Do, but it writes the result to buffer directly instead of returning a new buffer

Jump to

Keyboard shortcuts

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