Git Product home page Git Product logo

dynproto's Introduction

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.

dynproto's People

Contributors

abbot avatar adg avatar carl-mastrangelo avatar davecheney avatar dsnet avatar dsymonds avatar erikmcc avatar hyangah avatar iamqizhao avatar jmhodges avatar lmmilewski avatar lorenzosim avatar lstoll avatar lukegb avatar matloob avatar mcos avatar menghanl avatar mkrautz avatar neild avatar nigeltao avatar okdave avatar robpike avatar rsc avatar tamird avatar tswast avatar vonhollen avatar xfxyjwf avatar zellyn avatar zmullett avatar zombiezen avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.