dynproto

module
v0.0.0-...-5ae847c Latest Latest
Warning

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

Go to latest
Published: Jan 23, 2017 License: BSD-3-Clause

README

dynproto

This is a fork of github.com/golang/protobuf to add schema parsing & marshaling/unmarshaling of blobs from dynamically parsed schemas.

Use this to parse protos when schemas aren't available at compile-time.

Example:

// careful: this is paraphrased from test suite. Never tested as-is.

schema, _ := proto.ParseSchema("syntax = \"proto3\"; message Hello {int32 a = 1; string b = 3;}")
dyn := proto.DynStruct(&schema)

val := reflect.New(dyn[0]) // dyn[0] i.e. the 0th message in the schema
val.Elem().FieldByName("A").SetInt(10)
val.Elem().FieldByName("B").SetString("ten")

buf := proto.Buffer{}
buf.Marshal(val.Interface())
val2 := reflect.New(dyn[0])
buf.Unmarshal(val2.Interface())

if val2.Elem().FieldByName("A").Int() != 10 || val.Elem().FieldByName("B").String() != "ten" {
	log.Panicf("%v\n", val2)
}

Changes from upstream:

  • use cgo to wrap schema parsing from libprotobuf.
  • added DynProto function which returns a reflect.Type (a dynamic struct)
  • Buffer.Marshal / Unmarshal takes interface{} instead of proto.Message
  • proto package copies parts of protoc-gen-go/generator.go.

Status & concerns:

  • working and non-working features:
    • scalars
    • strings
    • nested protos
    • repeated fields
    • enums
    • maps
    • including other proto files
  • I don't know how to test this project under all build constraints (appengine / JS). I assume it fails for the JS target because of cgo.
  • cgo piece (schemaparser.go) needs review from someone who understands cgo to check for memory leaks
  • haven't done any performance testing. Should be way slower than generated marshal/unmarshal functions.
  • security: I don't know if the schema parsing & ser/des code were written with user-provided input in mind. You should pay for a security audit before pointing this at the internet.

Future work:

  • I'm evaluating this for use in a larger project. If that moves forward, I plan to clean up this fork and ask the maintainers to merge back my changes.
  • Don't expect this repo to stay functional or up-to-date. If your organization wants to use these features, you should take over the project.

Directories

Path Synopsis
conformance implements the conformance test subprocess protocol as documented in conformance.proto.
conformance implements the conformance test subprocess protocol as documented in conformance.proto.
conformance_proto
Package conformance is a generated protocol buffer package.
Package conformance is a generated protocol buffer package.
Package descriptor provides functions for obtaining protocol buffer descriptors for generated Go types.
Package descriptor provides functions for obtaining protocol buffer descriptors for generated Go types.
Package jsonpb provides marshaling and unmarshaling between protocol buffers and JSON.
Package jsonpb provides marshaling and unmarshaling between protocol buffers and JSON.
jsonpb_test_proto
Package jsonpb is a generated protocol buffer package.
Package jsonpb is a generated protocol buffer package.
Package proto converts data structures to and from the wire format of protocol buffers.
Package proto converts data structures to and from the wire format of protocol buffers.
proto3_proto
Package proto3_proto is a generated protocol buffer package.
Package proto3_proto is a generated protocol buffer package.
protoc-gen-go
descriptor
Package descriptor is a generated protocol buffer package.
Package descriptor is a generated protocol buffer package.
generator
The code generator for the plugin for the Google protocol buffer compiler.
The code generator for the plugin for the Google protocol buffer compiler.
grpc
Package grpc outputs gRPC service descriptions in Go code.
Package grpc outputs gRPC service descriptions in Go code.
plugin
Package plugin_go is a generated protocol buffer package.
Package plugin_go is a generated protocol buffer package.
Package ptypes contains code for interacting with well-known types.
Package ptypes contains code for interacting with well-known types.
any
Package any is a generated protocol buffer package.
Package any is a generated protocol buffer package.
duration
Package duration is a generated protocol buffer package.
Package duration is a generated protocol buffer package.
empty
Package empty is a generated protocol buffer package.
Package empty is a generated protocol buffer package.
struct
Package structpb is a generated protocol buffer package.
Package structpb is a generated protocol buffer package.
timestamp
Package timestamp is a generated protocol buffer package.
Package timestamp is a generated protocol buffer package.
wrappers
Package wrappers is a generated protocol buffer package.
Package wrappers is a generated protocol buffer package.

Jump to

Keyboard shortcuts

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