• Stars
    star
    67
  • Rank 446,673 (Top 10 %)
  • Language
    Go
  • License
    MIT License
  • Created over 9 years ago
  • Updated over 7 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

auto-generate capnproto schema from your golang source files. Depends on go-capnproto-1.0 at https://github.com/glycerine/go-capnproto

bambam: auto-generate capnproto schema from your golang source files.

Adding capnproto serialization to an existing Go project used to mean writing a lot of boilerplate.

Not anymore.

Given a set of golang (Go) source files, bambam will generate a capnproto schema. Even better: bambam will also generate translation functions to readily convert between your golang structs and the new capnproto structs.

prereqs

You'll need a recent (up-to-date) version of go-capnproto. If you installed go-capnproto before, you'll want to update it [>= f9f239fc7f5ad9611cf4e88b10080a4b47c3951d / 16 Nov 2014].

Capnproto and go-capnproto should both be installed and on your PATH.

to install: run make. This lets us record the git commit in LASTGITCOMMITHASH to provide accurate version info. Otherwise you'll get an 'undefined: LASTGITCOMMITHASH' failure.

# be sure go-capnproto and capnpc are installed first.

$ go get -t github.com/glycerine/bambam  # the -t pulls in the test dependencies.

# ignore the initial compile error about 'undefined: LASTGITCOMMITHASH'. `make` will fix that.
$ cd $GOPATH/src/github.com/glycerine/bambam
$ make  # runs tests, build if all successful
$ go install

use

use: bambam -o outdir -p package myGoSourceFile.go myGoSourceFile2.go ...
     # Bambam makes it easy to use Capnproto serialization[1] from Go.
     # Bambam reads .go files and writes a .capnp schema and Go bindings.
     # options:
     #   -o="odir" specifies the directory to write to (created if need be).
     #   -p="main" specifies the package header to write (e.g. main, mypkg).
     #   -X exports private fields of Go structs. Default only maps public fields.
     #   -version   shows build version with git commit hash
     #   -OVERWRITE modify .go files in-place, adding capid tags (write to -o dir by default).
     # required: at least one .go source file for struct definitions. Must be last, after options.
     #
     # [1] https://github.com/glycerine/go-capnproto 

demo

See rw.go.txt. To see all the files compiled together in one project: (a) comment out the defer in the rw_test.go file; (b) run go test; (c) then cd testdir_* and look at the sample project files there. (d). run go build in the testdir_ to rebuild the binary. Notice that you will need all three .go files to successfully build. The two .capnp files should be kept so you can read your data from any capnp-supported language. Here's what is what in that example directory:

rw.go             # your original go source file (in this test)
translateCapn.go  # generated by bambam after reading rw.go
schema.capnp      # generated by bambam after reading rw.go
schema.capnp.go   # generated by `capnpc -ogo schema.capnp` <- you have to do this yourself or in your Makefile.
go.capnp          # always necessary boilerplate to let capnpc work, just copy it from bambam/go.capnp to your build dir.

example:

jaten@c03:~/go/src/github.com/glycerine/bambam:master$ cd testdir_884497362/
jaten@c03:~/go/src/github.com/glycerine/bambam/testdir_884497362:master$ go build
jaten@c03:~/go/src/github.com/glycerine/bambam/testdir_884497362:master$ ls
go.capnp  rw.go  rw.go.txt  schema.capnp  schema.capnp.go  testdir_884497362  translateCapn.go
jaten@c03:~/go/src/github.com/glycerine/bambam/testdir_884497362:master$ ./testdir_884497362
Load() data matched Saved() data.
jaten@c03:~/go/src/github.com/glycerine/bambam/testdir_884497362:master$ # run was successful

Here is what it looks like to use the Save()/Load() methods. You end up with a Save() and Load() function for each of your structs. Simple.

package main

import (
    "bytes"
)

//
// By default bambam will add the `capid` tags
// to a copy of your source in the output directory.
// Use bambam -OVERWRITE to modify files directly in-place.
// The capid tags control the @0, @1, field numbering 
// in the generated capnproto schema. If you change
// your go structs, the capid tags let your schema
// stay backwards compatible with prior serializations.
//
type MyStruct struct {
	Hello    []string  `capid:"0"`
	World    []int     `capid:"1"`
}

func main() {

	rw := MyStruct{
		Hello:    []string{"one", "two", "three"},
		World:    []int{1, 2, 3},
	}

    // any io.ReadWriter will work here (os.File, etc)
	var o bytes.Buffer

	rw.Save(&o)
    // now we have saved!


    rw2 := &MyStruct{}
	rw2.Load(&o)
    // now we have restored!

}

what Go types does bambam recognize?

Supported: structs, slices, and primitive/scalar types are supported. Structs that contain structs are supported. You have both slices of scalars (e.g. []int) and slices of structs (e.g. []MyStruct) available.

We handle [][]T, but not [][][]T, where T is a struct or primitive type. The need for triply nested slices is expected to be rare. Interpose a struct after two slices if you need to go deeper.

Currently unsupported (pull requests welcome): Go maps.

Also: pointers to structs to be serialized work, but pointers in the inner-most struct do not. This is not a big limitation, as it is rarely meaningful to pass a pointer value to a different process.

capid tags on go structs

When you run bambam, it will generate a modified copy of your go source files in the output directory.

These new versions include capid tags on all public fields of structs. You should inspect the copy of the source file in the output directory, and then replace your original source with the tagged version. You can also manually add capid tags to fields, if you need to manually specify a field number (e.g. you are matching an pre-existing capnproto definition).

If you are feeling especially bold, bambam -OVERWRITE my.go will replace my.go with the capid tagged version. For safety, only do this on backed-up and version controlled source files.

By default only public fields (with a Capital first letter in their name) are tagged. The -X flag ignores the public/private distinction, and tags all fields.

The capid tags allow the capnproto schema evolution to function properly as you add new fields to structs. If you don't include the capid tags, your serialization code won't be backwards compatible as you change your structs.

Deleting fields from your go structs isn't (currently) particularly well-supported. We could potentially allow fields to be // commented out in the go source and yet still parse the comments and use that parse to keep the schema correct, but that's not a trivial bit of work.

example of capid annotion use

type Job struct { 
   C int `capid:"2"`  // we added C later, thus it is numbered higher.
   A int `capid:"0"`
   B int `capid:"1"` 
}

other tags

Also available tags: capid:"skip" or capid:"-1" (any negative number): this field will be skipped and not serialized or written to the schema.

// capname:"Counter"
type number struct {
   A int
}

The above struct will be mapped into capnproto as:

struct Counter {
  a @0: Int64;
}

Without the // capname:"Counter" comment, you would get:

struct NumberCapn {
  a @0: Int64;
}

Explanation: Using a // capname:"newName" comment on the line right before a struct definition will cause bambam to use 'newName' as the name for the corresponding struct in the capnproto schema. Otherwise the corresponding struct will simply uppercase the first letter of the orignal Go struct, and append "Capn". For example: a Go struct called number would induce a parallel generated capnp struct called NumberCapn.

windows build script

see build.cmd. Thanks to Klaus Post (http://klauspost.com) for contributing this.



Copyright (c) 2015, Jason E. Aten, Ph.D.

More Repositories

1

zygomys

Zygo is a Lisp interpreter written in 100% Go. Central use case: dynamically compose Go struct trees in a zygo script, then invoke compiled Go functions on those trees. Makes Go reflection easy.
Go
1,636
star
2

offheap

an off-heap hash-table in Go. Used to be called go-offheap-hashtable, but we shortened it.
Go
349
star
3

rbuf

a small circular ring buffer library in go / golang
Go
182
star
4

sshego

golang/Go library for ssh tunneling (secure port forwarding)
Go
171
star
5

zebrapack

ZebraPack format is like gobs version 2: serialization in Go, *but* extremely fast and friendly to other languages. Use Go as your schema. Strong typing. Well documented (and msgpack2 compatible) format so other languages can be readily supported. See also https://github.com/glycerine/greenpack for a more recent alternative. Docs:
Go
168
star
6

greenpack

Cross-language serialization for Golang: greenpack adds versioning, stronger typing, and optional schema atop msgpack2. `greenpack -msgpack2` produces classic msgpack2, and handles nils. Cousin to ZebraPack (https://github.com/glycerine/zebrapack), greenpack's advantage is fully self-describing data. Oh, and faster than protobufs.
Go
113
star
7

goq

goq: a job queuing system written in go (golang). "Pronounced Go-Queue. Don't Gawk at this!"
Go
86
star
8

sofia-ml

Automatically exported from code.google.com/p/sofia-ml
C++
61
star
9

go-sliding-window

a library for reliable and flow-controlled nats sessions using the sliding window protocol. Written in golang.
Go
58
star
10

golang-thrift-minimal-example

the apache thrift starter tutorial for golang, as a standalone repo
Go
44
star
11

gozbus

nanocap based messaging system
Go
43
star
12

grpc-demo

code to stream arbitrarily large files between hosts using gRPC and golang
Go
39
star
13

fast-elliptic-curve-p256

repackage the golang elliptic library enhancements by Vlad Krasnov and Shay Gueron as a stand alone library. Works with Go 1.4 or Go 1.5.
Go
35
star
14

tmframe

TMFRAME, pronounced "time frame", is a binary standard for compactly encoding time series data
Go
28
star
15

xcryptossh

golang.org/x/crypto/ssh the next generation: provide idle timeouts, avoid memory leaks, and gracefully cancel connections
Go
27
star
16

rmq

R package providing msgpack and websockets; demonstrates how to utilize Go libraries from R.
Go
24
star
17

libzipfs

Ship a zip file of media resources inside your golang web-app for complete standalone one-binary deployment
Go
21
star
18

golang-embed-julia

simple example of calling julia from Go
Go
20
star
19

thinkgo

Think Go. Pointers and resources for learning Go. Go (golang) is an elegant, fast, and rapid development language.
19
star
20

nack-oriented-reliable-multicast

NACK-Oriented Reliable Multicast (NORM): http://www.nrl.navy.mil/itd/ncs/products/norm
C++
15
star
21

hello_gio

hello world for Gio graphics for Golang. Gio runs on macOS, Windows, Wayland (linux), X11, WebASM, iOS, and Android. All in Go, no bridge C/Java to write.
Go
14
star
22

arogue

A Go-Repl using R underneath
Go
9
star
23

go-unsnap-stream

small golang library for decoding the snappy streaming format https://github.com/google/snappy/blob/master/framing_format.txt
Go
9
star
24

truepack

like https://github.com/glycerine/greenpack, but no integer compression based on the int's value
Go
8
star
25

lush2

Lush2 sources from svn 908, starting point -- https://lush.svn.sourceforge.net/svnroot/lush -- Last Changed Date: 2011-03-20 16:29:13 -0500 (Sun, 20 Mar 2011). Now patched for OSX 10.6 and up.
C
7
star
26

PrattParserInC

An implementation of Pratt Parsing (Vaughn Pratt 1973 "Top Down Operator Precedence") in C/C++
7
star
27

ruid

ruid: a really unique id
Go
6
star
28

webiperf

webiperf is a web-app that makes it easy to generate iperf commands
JavaScript
6
star
29

rustxi

rust + transactions + interpreter = rustxi. rustxi is a transactional jit-compilation-based REPL for the Rust language.
C
5
star
30

liblmdb

git clone -b mdb.master git://git.openldap.org/openldap.git # on June 11, 2014 / 17c09fa476a7dbd49aca5e4caf0384cb1c3d244a
C
5
star
31

monotime

Go library for monotonic time source on platforms where one is available to the Go runtime.
Go
4
star
32

low-level-lush

(LLL) : Low-Level-Lush with Lisp is a combination of Lush2.0.1 and LLVM2.8 to bring out the best in both.
C++
4
star
33

swig-cpp-to-cffi-for-common-lisp-enhancements

forking from SWIG svn 12570 to generate better common lisp bindings from c++
C++
4
star
34

gopass

Go
4
star
35

idem

idem.Halter: a pattern for halting goroutines in Go
Go
4
star
36

muse

golang code to convert from go/types.Type to reflect.Type
Go
3
star
37

crack-language

Crack programming language
C++
3
star
38

buzz

broadcasting channels in Go
Go
3
star
39

json2msgpack

convert from newline delimited json to size-header based msgpack frames
Go
3
star
40

shore-mt

shore-mt (Scalable Heterogeneous Object REpository - MultiThreaded version), import of the 6.0.2 release of 03-Jan-2012 http://research.cs.wisc.edu/shore-mt/ ( differs from the DIAS version from http://diaswww.epfl.ch/shore-mt/ )
C++
3
star
41

configs-in-golang

demonstrate pattern for command line flag handling that allows library configuration/reuse and testing
Go
3
star
42

golang-fisher-exact

Fisher's exact test for 2x2 contingency tables, in Golang
Go
2
star
43

spread-src-4.4.0

An open source implementation of virtual synchrony, package spread-src-4.4.0, from Spread Concepts LLC. Details at http://www.spread.org See also https://github.com/glycerine/spread-src-5.0.1
C
2
star
44

vprint

debug Go faster with this simple print library. prints show timestamp and file location
Go
2
star
45

selfie

generate and validate self-signed public keys
Go
2
star
46

nanomsgardvark

R bindings for nanomsg
C
2
star
47

justinjuddeasyssh

Go
2
star
48

golang-hex-dumper

Go
2
star
49

xml2csv

Parse an XML file on stdin, write csv to stdout. No schema, no structs required.
Go
2
star
50

gossainterpdemo

demonstration of the go ssa interpreter
Go
2
star
51

pingbuf

a minimal ring buffer
Go
2
star
52

libcmm

Ralf Juengling's libcmm: a C Memory Management Library, last commit f980fd1715780c99f54ebad8b49dbc10befe192d from Thu Nov 12 13:35:24 2009. Obtained from the sourceforge git repos on 28 April 2011 by: git clone git://libcmm.git.sourceforge.net/gitroot/libcmm/libcmm
C++
2
star
53

ugorji-go-codec

Go
1
star
54

latch

Go
1
star
55

spread-src-5.0.1

see motivation https://github.com/glycerine/spread-src-4.4.0
C
1
star
56

dyg

Go
1
star
57

rogue

use R as a repl and worker under golang master
Go
1
star
58

basic-bolt

basic boltdb example
Go
1
star
59

avfs

a virtual filesystem. mirror from http://avf.sourceforge.net/ and http://sourceforge.net/projects/avf/
C
1
star
60

vpython-emacs-mode

my vpython.el mode for ipython
Emacs Lisp
1
star
61

verb

Go
1
star
62

iris2

Community driven successor of the iris web framework
Go
1
star
63

yield

C++ web application server
C++
1
star
64

nats

friendly fork of nats-io/nats for use with glycerine/hnatsd
Go
1
star
65

lcon

local pipe that looks like net.Conn
Go
1
star
66

pelican-protocol

In ancient Egypt the pelican was believed to possess the ability to prophesy safe passage in the underworld. Pelicans are ferocious eaters of fish.
Go
1
star
67

mps-kit-1.108.0-x86_64-linux-port

See if the excellent Memory Pool System (MPS) can be ported to x86_64 on Linux
C
1
star
68

go-inthash

go version of basic hash map, open addressing with linear probing. uint64 keys. Inspired by http://preshing.com/20130107/this-hash-table-is-faster-than-a-judy-array/
Go
1
star
69

fastbit

fastbit-2.0.3 from https://sdm.lbl.gov/fastbit/, BSD licensed.
C++
1
star
70

geist

the geist script runner turns golang into a scripting language
Go
1
star
71

gopls-emacs-how-to

how to get gopls in emacs with golang go-mode working
1
star
72

double-messages-received-bug

Go
1
star
73

golang-repl

Go
1
star
74

python-extension-in-golang

code for https://hackernoon.com/extending-python-3-in-go-78f3a69552ac
Makefile
1
star
75

L3

L3: experiments in aggressive Garbage-collection and interpreter implementation based on the Pratt parser. Uses protocol buffers for serialization, and Judy arrays for fast, sparse arrays/hashtables. Linux and OSX.
1
star
76

SchemeTL

Scheme To Learn : Learning Scheme by implementing STL data structure equivalents
Common Lisp
1
star