colfer

package module
v1.6.1 Latest Latest
Warning

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

Go to latest
Published: Apr 6, 2017 License: CC0-1.0 Imports: 14 Imported by: 0

README

Colfer Build Status

Colfer is a binary serialization format optimized for speed and size.

The project's compiler colf(1) generates source code from schema definitions to marshal and unmarshall data structures.

This is free and unencumbered software released into the public domain. The format is inspired by Protocol Buffer.

Language Support
  • C, C99 compliant, C++11 compliant
  • Go, a.k.a. golang
  • Java, Android compatible
  • JavaScript, a.k.a. ECMAScript, NodeJS compatible
Features
  • Simple and straightforward in use
  • No dependencies other than the core library
  • Both faster and smaller than: Protocol Buffers, FlatBuffers and MessagePack
  • Robust including size protection
  • Maximum of 127 fields per data structure
  • No support for enumerations
  • Framed; suitable for concatenation/streaming
TODO's
  • Rust and Python
  • RMI (WIP)
  • List type support for integers and timestamps
  • Please share your experiences

Use

Download a prebuilt compiler or run go get -u github.com/pascaldekloe/colfer/cmd/colf to make one yourself. Without arguments the command prints its manual.

NAME
	colf — compile Colfer schemas

SYNOPSIS
	colf [ options ] language [ file ... ]

DESCRIPTION
	Generates source code for a language. The options are: C, Go,
	Java and JavaScript.
	The file operands specify the input. Directories are scanned for
	files with the colf extension. If file is absent, colf includes
	the working directory.
	A package can have multiple schema files.

OPTIONS
  -b directory
    	Use a specific destination base directory. (default ".")
  -f	Normalizes schemas on the fly.
  -l expression
    	Sets the default upper limit for the number of elements in a
    	list. The expression is applied to the target language under
    	the name ColferListMax. (default "64 * 1024")
  -p prefix
    	Adds a package prefix. Use slash as a separator when nesting.
  -s expression
    	Sets the default upper limit for serial byte sizes. The
    	expression is applied to the target language under the name
    	ColferSizeMax. (default "16 * 1024 * 1024")
  -v	Enables verbose reporting to the standard error.
  -x class
    	Makes all generated classes extend a super class. Use slash as
    	a package separator. Java only.

EXIT STATUS
	The command exits 0 on succes, 1 on compilation failure and 2
	when invoked without arguments.

EXAMPLES
	Compile ./io.colf with compact limits as C:

		colf -b src -s 2048 -l 96 C io.colf

	Compile ./api/*.colf in package com.example as Java:

		colf -p com/example -x com/example/Parent Java api

BUGS
	Report bugs at https://github.com/pascaldekloe/colfer/issues

SEE ALSO
	protoc(1)

It is recommended to commit the generated source code to the respective version control. Modern developers may disagree and use the Maven plugin.

<plugin>
	<groupId>net.quies.colfer</groupId>
	<artifactId>colfer-maven-plugin</artifactId>
	<version>1.9</version>
	<configuration>
		<packagePrefix>com/example</packagePrefix>
	</configuration>
</plugin>

Schema

Data structures are defined in .colf files. The format is quite conventional.

// Package demo offers a demonstration.
// These comment lines will end up in the generated code.
package demo

// Course is the grounds where the game of golf is played.
type course struct {
	ID    uint64
	name  text
	holes []hole
	image binary
	tags  []text
}

type hole struct {
	// Lat is the latitude of the cup.
	lat float64
	// Lon is the longitude of the cup.
	lon float64
	// Par is the difficulty index.
	par uint8
	// Water marks the presence of water.
	water bool
	// Sand marks the presence of sand.
	sand bool
}

See what the generated code looks like in C, Go, Java or JavaScript.

The following table shows how Colfer data types are applied per language.

Colfer C Go Java JavaScript
bool char bool boolean Boolean
uint8 uint8_t uint8 byte † Number
uint16 uint16_t uint16 short † Number
uint32 uint32_t uint32 int † Number
uint64 uint64_t uint64 long † Number ‡
int32 int32_t int32 int Number
int64 int64_t int64 long Number ‡
float32 float float32 float Number
float64 double float64 double Number
timestamp 2 × int_fast64_t Time †† Instant Date + Number
text const char*, size_t string String †‡ String †‡
binary uint8_t*, size_t []byte byte[] Uint8Array
list struct*, size_t slice array Array
  • † signed representation of unsigned data, i.e. may overflow to negative.
  • ‡ range limited to (1 - 2⁵³, 2⁵³ - 1)
  • †† timezone not preserved
  • †‡ characters limited by UTF-16 (U+0000, U+10FFFF)

Lists may contain floating points, text, binaries or data structures.

Compatibility

Name changes do not affect the serialization format. Deprecated fields should be renamed to clearly discourage their use. For backwards compatibility new fields must be added to the end of colfer structs. Thus the number of fields can be seen as the schema version.

Performance

Colfer aims to be the fastest and the smallest format while still resilient to malicious input. See the benchmark wiki for a comparison. Suboptimal performance is treated like a bug.

Documentation

Overview

Package colfer provides schema definitions.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Format

func Format(file string) (changed bool, err error)

Format normalizes the file's content. The content of file is expected to be syntactically correct.

func GenerateC

func GenerateC(basedir string, packages []*Package) error

GenerateC writes the code into file "Colfer.h" and "Colfer.c".

func GenerateECMA

func GenerateECMA(basedir string, packages []*Package) error

GenerateECMA writes the code into file "Colfer.js".

func GenerateGo

func GenerateGo(basedir string, packages []*Package) error

GenerateGo writes the code into file "Colfer.go".

func GenerateJava

func GenerateJava(basedir string, packages []*Package) error

GenerateJava writes the code into the respective ".java" files.

func IsCKeyword

func IsCKeyword(s string) bool

IsCKeyword returs whether s is a reserved word in C code.

func IsECMAKeyword

func IsECMAKeyword(s string) bool

IsECMAKeyword returs whether s is a reserved word in ECMAScript code.

func IsJavaKeyword

func IsJavaKeyword(s string) bool

IsJavaKeyword returs whether s is a reserved word in Java code.

Types

type Field

type Field struct {
	// Struct is the parent.
	Struct *Struct
	// Index is the Struct.Fields position.
	Index int
	// Name is the identification token.
	Name string
	// NameNative is the language specific Name.
	NameNative string
	// Docs are the documentation texts.
	Docs []string
	// Type is the datatype.
	Type string
	// TypeNative is the language specific Type.
	TypeNative string
	// TypeRef is the Colfer data structure reference.
	TypeRef *Struct
	// TypeList flags whether the datatype is a list.
	TypeList bool
}

Field is a Struct member definition.

func (*Field) DocText

func (f *Field) DocText(indent string) string

DocText returns the documentation lines prefixed with ident.

func (*Field) NameTitle

func (f *Field) NameTitle() string

NameTitle returns the identification token in title case.

func (*Field) String

func (f *Field) String() string

String returns the qualified name.

type Package

type Package struct {
	// Name is the identification token.
	Name string
	// NameNative is the language specific Name.
	NameNative string
	// Docs are the documentation texts.
	Docs []string
	// Structs are the type definitions.
	Structs []*Struct
	// SchemaFiles are the source filenames.
	SchemaFiles []string
	// SizeMax is the uper limit expression.
	SizeMax string
	// ListMax is the uper limit expression.
	ListMax string
	// SuperClass is the fully qualified path.
	SuperClass string
	// SuperClassNative is the language specific SuperClass.
	SuperClassNative string
}

Package is a named definition bundle.

func ParseFiles

func ParseFiles(files []string) ([]*Package, error)

ParseFiles returns the schema definitions.

func (*Package) DocText

func (p *Package) DocText(ident string) string

DocText returns the documentation lines prefixed with ident.

func (*Package) HasFloat

func (p *Package) HasFloat() bool

HasFloat returns whether p has one or more floating point fields.

func (*Package) HasList

func (p *Package) HasList() bool

HasList returns whether p has one or more list fields.

func (*Package) HasTimestamp

func (p *Package) HasTimestamp() bool

HasTimestamp returns whether p has one or more timestamp fields.

func (*Package) Refs

func (p *Package) Refs() []*Package

Refs returns all direct references sorted by name.

func (*Package) SchemaFileList

func (p *Package) SchemaFileList() string

SchemaFileList returns a listing text.

type Struct

type Struct struct {
	Pkg *Package
	// Name is the identification token.
	Name string
	// NameNative is the language specific Name.
	NameNative string
	// Docs are the documentation texts.
	Docs []string
	// Fields are the elements in order of appearance.
	Fields []*Field
	// SchemaFile is the source filename.
	SchemaFile string
}

Struct is a data structure definition.

func (*Struct) DocText

func (s *Struct) DocText(indent string) string

DocText returns the documentation lines prefixed with ident.

func (*Struct) HasBinary

func (s *Struct) HasBinary() bool

HasBinary returns whether s has one or more binary fields.

func (*Struct) HasBinaryList

func (s *Struct) HasBinaryList() bool

HasBinaryList returns whether s has one or more binary list fields.

func (*Struct) HasFloat

func (s *Struct) HasFloat() bool

HasFloat returns whether s has one or more floating point fields.

func (*Struct) HasList

func (s *Struct) HasList() bool

HasList returns whether s has one or more list fields.

func (*Struct) HasText

func (s *Struct) HasText() bool

HasText returns whether s has one or more text fields.

func (*Struct) HasTimestamp

func (s *Struct) HasTimestamp() bool

HasTimestamp returns whether s has one or more timestamp fields.

func (*Struct) NameTitle

func (s *Struct) NameTitle() string

NameTitle returns the identification token in title case.

func (*Struct) String

func (s *Struct) String() string

String returns the qualified name.

Directories

Path Synopsis
cmd
go
gen
Package gen tests all field mapping options.
Package gen tests all field mapping options.
rpc
Package rpc implements the net/rpc codecs.
Package rpc implements the net/rpc codecs.

Jump to

Keyboard shortcuts

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