oneofproto

package module
v0.0.0-...-77b7c8a Latest Latest
Warning

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

Go to latest
Published: Oct 7, 2023 License: BSD-2-Clause Imports: 5 Imported by: 0

README

oneofproto

Library experimenting with an alternative to long switch type clauses for protobuf oneof fields, so that new oneof options can be supported by simply bumping the protobuf dependency, thus not requiring new code.

Benchmarks seem to indicate little difference in speed with a large set of oneof options.

go test -bench=. -benchmem -benchtime 20s
goos: linux
goarch: amd64 pkg: github.com/merlincox/oneofproto
cpu: Intel(R) Core(TM) i7-7500U CPU @ 2.70GHz

Benchmark Iterations ns/op B/op allocs/op
BenchmarkToStructpb100-4 7993056 2998 ns/op 1128 B/op 29 allocs/op
BenchmarkToStructpbDefer100-4 7796260 3050 ns/op 1128 B/op 29 allocs/op
BenchmarkToStructpbUnsafe100-4 7876123 3124 ns/op 1128 B/op 29 allocs/op
BenchmarkSwitched100-4 8142153 2960 ns/op 1128 B/op 29 allocs/op
BenchmarkToStructpb1-4 7894995 3020 ns/op 1128 B/op 29 allocs/op
BenchmarkToStructpbDefer1-4 7945716 3011 ns/op 1128 B/op 29 allocs/op
BenchmarkToStructpbUnsafe1-4 8090305 3005 ns/op 1128 B/op 29 allocs/op
BenchmarkSwitched1-4 8136547 2982 ns/op 1128 B/op 29 allocs/op

where the tests ending in 100 use a oneof with a hundred possible values, while those ending in 1 use a oneof with only one value.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ToStructpb

func ToStructpb(oneof any) (protostruct *structpb.Struct, typeName string, err error)

ToStructpb converts a protobuf oneof value into a JSON-compatible *structpb.Struct, or returns an error if the conversion is impossible. It also returns a string representation of the underlying type.

Example:

type TestProto struct {
	state         protoimpl.MessageState
	sizeCache     protoimpl.SizeCache
	unknownFields protoimpl.UnknownFields

	Uuid string `protobuf:"bytes,1,opt,name=uuid,proto3" json:"uuid,omitempty"`
	Data isTestProto_Data `protobuf_oneof:"data"`
}

msg := &testapi.TestRequest{
	Test: &testapi.TestProto{
		Uuid: "uuid",
		Data: &testapi.TestProto_Assignable100{Assignable100: &testapi.Assignable100{Data100: "test100"}},
	},
}

protostruct, typeName, err := oneofproto.ToStructpb(msg.Test.Data)

where protostruct is a *structpb.Struct JSON compatible with &testapi.Assignable100{Data100: "test100"}
and typeName is "*testapi.Assignable100"

func ToStructpbDefer

func ToStructpbDefer(oneof any) (protostruct *structpb.Struct, typeName string, err error)

ToStructpbDefer mirrors the functionality of ToStructpb but uses a panic-recovery mechanism instead of kind checking.

func ToStructpbUnsafe

func ToStructpbUnsafe(oneof any) (protostruct *structpb.Struct, typeName string, err error)

ToStructpbUnsafe mirrors the functionality of ToStructpb but uses no kind checks or recovery, so is unsafe.

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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