p2j

package
v0.9.2 Latest Latest
Warning

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

Go to latest
Published: Aug 21, 2026 License: Apache-2.0 Imports: 10 Imported by: 1 module, 1 package

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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
}

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, pbytes []byte) (json []byte, err error)

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

Example
package main

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

	"github.com/cloudwego/dynamicgo/conv"
	"github.com/cloudwego/dynamicgo/internal/util_test"
	"github.com/cloudwego/dynamicgo/proto"
	"github.com/cloudwego/dynamicgo/testdata/kitex_gen/pb/example2"
	goproto "google.golang.org/protobuf/proto"
)

var opts = conv.Options{}

func main() {
	// get descriptor and data
	includeDirs := util_test.MustGitPath("testdata/idl/") // includeDirs is used to find the include files.
	desc := proto.FnRequest(proto.GetFnDescFromFile(exampleIDLPath, "ExampleMethod", proto.Options{}, includeDirs))

	// make BinaryConv
	cv := NewBinaryConv(conv.Options{})
	in := readExampleReqProtoBufData()

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

	// use proto.Unmarshal to check proto data validity
	err = goproto.Unmarshal(readExampleReqProtoBufData(), &exp)
	if err != nil {
		panic(err)
	}

	// validate result
	var act example2.ExampleReq
	err = json.Unmarshal([]byte(out), &act)
	if err != nil {
		panic(err)
	}
	if !reflect.DeepEqual(exp, act) {
		panic("not equal")
	}
}

func (*BinaryConv) DoInto

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

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

func (*BinaryConv) SetOptions

func (self *BinaryConv) SetOptions(opts conv.Options)

SetOptions sets options

Jump to

Keyboard shortcuts

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