Skip to content

Instantly share code, notes, and snippets.

@pascaldekloe
Created April 2, 2017 22:20
Show Gist options
  • Save pascaldekloe/786fd46e6e4710c14fee7da1f480c2d4 to your computer and use it in GitHub Desktop.
Save pascaldekloe/786fd46e6e4710c14fee7da1f480c2d4 to your computer and use it in GitHub Desktop.
Demo Go Compilation
// Package demo offers a demonstration.
// These comment lines will end up in the generated code.
package demo
// Code generated by colf(1); DO NOT EDIT.
// The compiler used schema file demo.colf.
import (
"encoding/binary"
"fmt"
"io"
"math"
)
var intconv = binary.BigEndian
// Colfer configuration attributes
var (
// ColferSizeMax is the upper limit for serial byte sizes.
ColferSizeMax = 16 * 1024 * 1024
// ColferListMax is the upper limit for the number of elements in a list.
ColferListMax = 64 * 1024
)
// ColferMax signals an upper limit breach.
type ColferMax string
// Error honors the error interface.
func (m ColferMax) Error() string { return string(m) }
// ColferError signals a data mismatch as as a byte index.
type ColferError int
// Error honors the error interface.
func (i ColferError) Error() string {
return fmt.Sprintf("colfer: unknown header at byte %d", i)
}
// ColferTail signals data continuation as a byte index.
type ColferTail int
// Error honors the error interface.
func (i ColferTail) Error() string {
return fmt.Sprintf("colfer: data continuation at byte %d", i)
}
// Course is the grounds where the game of golf is played.
type Course struct {
ID uint64
Name string
Holes []*Hole
Image []byte
Tags []string
}
// MarshalTo encodes o as Colfer into buf and returns the number of bytes written.
// If the buffer is too small, MarshalTo will panic.
// All nil entries in o.Holes will be replaced with a new value.
func (o *Course) MarshalTo(buf []byte) int {
var i int
if x := o.ID; x >= 1<<49 {
buf[i] = 0 | 0x80
intconv.PutUint64(buf[i+1:], x)
i += 9
} else if x != 0 {
buf[i] = 0
i++
for x >= 0x80 {
buf[i] = byte(x | 0x80)
x >>= 7
i++
}
buf[i] = byte(x)
i++
}
if l := len(o.Name); l != 0 {
buf[i] = 1
i++
x := uint(l)
for x >= 0x80 {
buf[i] = byte(x | 0x80)
x >>= 7
i++
}
buf[i] = byte(x)
i++
i += copy(buf[i:], o.Name)
}
if l := len(o.Holes); l != 0 {
buf[i] = 2
i++
x := uint(l)
for x >= 0x80 {
buf[i] = byte(x | 0x80)
x >>= 7
i++
}
buf[i] = byte(x)
i++
for vi, v := range o.Holes {
if v == nil {
v = new(Hole)
o.Holes[vi] = v
}
i += v.MarshalTo(buf[i:])
}
}
if l := len(o.Image); l != 0 {
buf[i] = 3
i++
x := uint(l)
for x >= 0x80 {
buf[i] = byte(x | 0x80)
x >>= 7
i++
}
buf[i] = byte(x)
i++
i += copy(buf[i:], o.Image)
}
if l := len(o.Tags); l != 0 {
buf[i] = 4
i++
x := uint(l)
for x >= 0x80 {
buf[i] = byte(x | 0x80)
x >>= 7
i++
}
buf[i] = byte(x)
i++
for _, a := range o.Tags {
x = uint(len(a))
for x >= 0x80 {
buf[i] = byte(x | 0x80)
x >>= 7
i++
}
buf[i] = byte(x)
i++
i += copy(buf[i:], a)
}
}
buf[i] = 0x7f
i++
return i
}
// MarshalLen returns the Colfer serial byte size.
// The error return option is demo.ColferMax.
func (o *Course) MarshalLen() (int, error) {
l := 1
if x := o.ID; x >= 1<<49 {
l += 9
} else if x != 0 {
for l += 2; x >= 0x80; l++ {
x >>= 7
}
}
if x := len(o.Name); x != 0 {
if x > ColferSizeMax {
return 0, ColferMax(fmt.Sprintf("colfer: field demo.course.name exceeds %d bytes", ColferSizeMax))
}
for l += x + 2; x >= 0x80; l++ {
x >>= 7
}
}
if x := len(o.Holes); x != 0 {
if x > ColferListMax {
return 0, ColferMax(fmt.Sprintf("colfer: field demo.course.holes exceeds %d elements", ColferListMax))
}
for l += 2; x >= 0x80; l++ {
x >>= 7
}
for _, v := range o.Holes {
if v == nil {
l++
continue
}
vl, err := v.MarshalLen()
if err != nil {
return 0, err
}
l += vl
}
if x > ColferSizeMax {
return 0, ColferMax(fmt.Sprintf("colfer: struct demo.course size exceeds %d bytes", ColferSizeMax))
}
}
if x := len(o.Image); x != 0 {
if x > ColferSizeMax {
return 0, ColferMax(fmt.Sprintf("colfer: field demo.course.image exceeds %d bytes", ColferSizeMax))
}
for l += x + 2; x >= 0x80; l++ {
x >>= 7
}
}
if x := len(o.Tags); x != 0 {
if x > ColferListMax {
return 0, ColferMax(fmt.Sprintf("colfer: field demo.course.tags exceeds %d elements", ColferListMax))
}
for l += 2; x >= 0x80; l++ {
x >>= 7
}
for _, a := range o.Tags {
x = len(a)
if x > ColferSizeMax {
return 0, ColferMax(fmt.Sprintf("colfer: field demo.course.tags exceeds %d bytes", ColferSizeMax))
}
for l += x + 1; x >= 0x80; l++ {
x >>= 7
}
}
if l >= ColferSizeMax {
return 0, ColferMax(fmt.Sprintf("colfer: struct demo.course size exceeds %d bytes", ColferSizeMax))
}
}
if l > ColferSizeMax {
return l, ColferMax(fmt.Sprintf("colfer: struct demo.course exceeds %d bytes", ColferSizeMax))
}
return l, nil
}
// MarshalBinary encodes o as Colfer conform encoding.BinaryMarshaler.
// All nil entries in o.Holes will be replaced with a new value.
// The error return option is demo.ColferMax.
func (o *Course) MarshalBinary() (data []byte, err error) {
l, err := o.MarshalLen()
if err != nil {
return nil, err
}
data = make([]byte, l)
o.MarshalTo(data)
return data, nil
}
// Unmarshal decodes data as Colfer and returns the number of bytes read.
// The error return options are io.EOF, demo.ColferError and demo.ColferMax.
func (o *Course) Unmarshal(data []byte) (int, error) {
if len(data) == 0 {
return 0, io.EOF
}
header := data[0]
i := 1
if header == 0 {
start := i
i++
if i >= len(data) {
goto eof
}
x := uint64(data[start])
if x >= 0x80 {
x &= 0x7f
for shift := uint(7); ; shift += 7 {
b := uint64(data[i])
i++
if i >= len(data) {
goto eof
}
if b < 0x80 || shift == 56 {
x |= b << shift
break
}
x |= (b & 0x7f) << shift
}
}
o.ID = x
header = data[i]
i++
} else if header == 0|0x80 {
start := i
i += 8
if i >= len(data) {
goto eof
}
o.ID = intconv.Uint64(data[start:])
header = data[i]
i++
}
if header == 1 {
if i >= len(data) {
goto eof
}
x := uint(data[i])
i++
if x >= 0x80 {
x &= 0x7f
for shift := uint(7); ; shift += 7 {
if i >= len(data) {
goto eof
}
b := uint(data[i])
i++
if b < 0x80 {
x |= b << shift
break
}
x |= (b & 0x7f) << shift
}
}
if x > uint(ColferSizeMax) {
return 0, ColferMax(fmt.Sprintf("colfer: demo.course.name size %d exceeds %d bytes", x, ColferSizeMax))
}
start := i
i += int(x)
if i >= len(data) {
goto eof
}
o.Name = string(data[start:i])
header = data[i]
i++
}
if header == 2 {
if i >= len(data) {
goto eof
}
x := uint(data[i])
i++
if x >= 0x80 {
x &= 0x7f
for shift := uint(7); ; shift += 7 {
if i >= len(data) {
goto eof
}
b := uint(data[i])
i++
if b < 0x80 {
x |= b << shift
break
}
x |= (b & 0x7f) << shift
}
}
if x > uint(ColferListMax) {
return 0, ColferMax(fmt.Sprintf("colfer: demo.course.holes length %d exceeds %d elements", x, ColferListMax))
}
l := int(x)
a := make([]*Hole, l)
malloc := make([]Hole, l)
for ai := range a {
v := &malloc[ai]
a[ai] = v
n, err := v.Unmarshal(data[i:])
if err != nil {
if err == io.EOF && len(data) >= ColferSizeMax {
return 0, ColferMax(fmt.Sprintf("colfer: demo.course size exceeds %d bytes", ColferSizeMax))
}
return 0, err
}
i += n
}
o.Holes = a
if i >= len(data) {
goto eof
}
header = data[i]
i++
}
if header == 3 {
if i >= len(data) {
goto eof
}
x := uint(data[i])
i++
if x >= 0x80 {
x &= 0x7f
for shift := uint(7); ; shift += 7 {
if i >= len(data) {
goto eof
}
b := uint(data[i])
i++
if b < 0x80 {
x |= b << shift
break
}
x |= (b & 0x7f) << shift
}
}
if x > uint(ColferSizeMax) {
return 0, ColferMax(fmt.Sprintf("colfer: demo.course.image size %d exceeds %d bytes", x, ColferSizeMax))
}
v := make([]byte, int(x))
start := i
i += len(v)
if i >= len(data) {
goto eof
}
copy(v, data[start:i])
o.Image = v
header = data[i]
i++
}
if header == 4 {
if i >= len(data) {
goto eof
}
x := uint(data[i])
i++
if x >= 0x80 {
x &= 0x7f
for shift := uint(7); ; shift += 7 {
if i >= len(data) {
goto eof
}
b := uint(data[i])
i++
if b < 0x80 {
x |= b << shift
break
}
x |= (b & 0x7f) << shift
}
}
if x > uint(ColferListMax) {
return 0, ColferMax(fmt.Sprintf("colfer: demo.course.tags length %d exceeds %d elements", x, ColferListMax))
}
a := make([]string, int(x))
o.Tags = a
for ai := range a {
if i >= len(data) {
goto eof
}
x := uint(data[i])
i++
if x >= 0x80 {
x &= 0x7f
for shift := uint(7); ; shift += 7 {
if i >= len(data) {
goto eof
}
b := uint(data[i])
i++
if b < 0x80 {
x |= b << shift
break
}
x |= (b & 0x7f) << shift
}
}
if x > uint(ColferSizeMax) {
return 0, ColferMax(fmt.Sprintf("colfer: demo.course.tags element %d size %d exceeds %d bytes", ai, x, ColferSizeMax))
}
start := i
i += int(x)
if i >= len(data) {
goto eof
}
a[ai] = string(data[start:i])
}
if i >= len(data) {
goto eof
}
header = data[i]
i++
}
if header != 0x7f {
return 0, ColferError(i - 1)
}
if i < ColferSizeMax {
return i, nil
}
eof:
if i >= ColferSizeMax {
return 0, ColferMax(fmt.Sprintf("colfer: struct demo.course size exceeds %d bytes", ColferSizeMax))
}
return 0, io.EOF
}
// UnmarshalBinary decodes data as Colfer conform encoding.BinaryUnmarshaler.
// The error return options are io.EOF, demo.ColferError, demo.ColferTail and demo.ColferMax.
func (o *Course) UnmarshalBinary(data []byte) error {
i, err := o.Unmarshal(data)
if i < len(data) && err == nil {
return ColferTail(i)
}
return err
}
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
}
// MarshalTo encodes o as Colfer into buf and returns the number of bytes written.
// If the buffer is too small, MarshalTo will panic.
func (o *Hole) MarshalTo(buf []byte) int {
var i int
if v := o.Lat; v != 0 {
buf[i] = 0
intconv.PutUint64(buf[i+1:], math.Float64bits(v))
i += 9
}
if v := o.Lon; v != 0 {
buf[i] = 1
intconv.PutUint64(buf[i+1:], math.Float64bits(v))
i += 9
}
if x := o.Par; x != 0 {
buf[i] = 2
i++
buf[i] = x
i++
}
if o.Water {
buf[i] = 3
i++
}
if o.Sand {
buf[i] = 4
i++
}
buf[i] = 0x7f
i++
return i
}
// MarshalLen returns the Colfer serial byte size.
// The error return option is demo.ColferMax.
func (o *Hole) MarshalLen() (int, error) {
l := 1
if o.Lat != 0 {
l += 9
}
if o.Lon != 0 {
l += 9
}
if x := o.Par; x != 0 {
l += 2
}
if o.Water {
l++
}
if o.Sand {
l++
}
if l > ColferSizeMax {
return l, ColferMax(fmt.Sprintf("colfer: struct demo.hole exceeds %d bytes", ColferSizeMax))
}
return l, nil
}
// MarshalBinary encodes o as Colfer conform encoding.BinaryMarshaler.
// The error return option is demo.ColferMax.
func (o *Hole) MarshalBinary() (data []byte, err error) {
l, err := o.MarshalLen()
if err != nil {
return nil, err
}
data = make([]byte, l)
o.MarshalTo(data)
return data, nil
}
// Unmarshal decodes data as Colfer and returns the number of bytes read.
// The error return options are io.EOF, demo.ColferError and demo.ColferMax.
func (o *Hole) Unmarshal(data []byte) (int, error) {
if len(data) == 0 {
return 0, io.EOF
}
header := data[0]
i := 1
if header == 0 {
start := i
i += 8
if i >= len(data) {
goto eof
}
o.Lat = math.Float64frombits(intconv.Uint64(data[start:]))
header = data[i]
i++
}
if header == 1 {
start := i
i += 8
if i >= len(data) {
goto eof
}
o.Lon = math.Float64frombits(intconv.Uint64(data[start:]))
header = data[i]
i++
}
if header == 2 {
start := i
i++
if i >= len(data) {
goto eof
}
o.Par = data[start]
header = data[i]
i++
}
if header == 3 {
if i >= len(data) {
goto eof
}
o.Water = true
header = data[i]
i++
}
if header == 4 {
if i >= len(data) {
goto eof
}
o.Sand = true
header = data[i]
i++
}
if header != 0x7f {
return 0, ColferError(i - 1)
}
if i < ColferSizeMax {
return i, nil
}
eof:
if i >= ColferSizeMax {
return 0, ColferMax(fmt.Sprintf("colfer: struct demo.hole size exceeds %d bytes", ColferSizeMax))
}
return 0, io.EOF
}
// UnmarshalBinary decodes data as Colfer conform encoding.BinaryUnmarshaler.
// The error return options are io.EOF, demo.ColferError, demo.ColferTail and demo.ColferMax.
func (o *Hole) UnmarshalBinary(data []byte) error {
i, err := o.Unmarshal(data)
if i < len(data) && err == nil {
return ColferTail(i)
}
return err
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment