~sircmpwn/go-bare

88b7cfa8e3c5de704b5791950869ddf77fa6e758 — Drew DeVault a month ago 3115a7e
Implement void types
M cmd/gen/gen.go => cmd/gen/gen.go +2 -0
@@ 210,6 210,8 @@ func primitiveType(kind schema.TypeKind) string {
		return "bool"
	case schema.String:
		return "string"
	case schema.Void:
		return "struct{}"
	}
	panic(fmt.Errorf("Invalid primitive type %d", kind))
}

M example/main.go => example/main.go +2 -0
@@ 65,5 65,7 @@ Hire date: %s
		person.Address.City, person.Address.State,
		person.Address.Country, person.Department.String(),
		time.Time(person.HireDate).Format(time.RFC3339))
	case *TerminatedEmployee:
		log.Println("Terminated employee (no data)")
	}
}

M example/schema.bare => example/schema.bare +3 -1
@@ 32,7 32,9 @@ type Employee {
	metadata: map[string]data
}

type Person (Customer | Employee)
type TerminatedEmployee void

type Person (Customer | Employee | TerminatedEmployee)

type Address {
	address: [4]string

M example/schema.go => example/schema.go +14 -1
@@ 80,6 80,16 @@ func (t *Employee) Encode() ([]byte, error) {
	return bare.Marshal(t)
}

type TerminatedEmployee struct{}

func (t *TerminatedEmployee) Decode(data []byte) error {
	return bare.Unmarshal(data, t)
}

func (t *TerminatedEmployee) Encode() ([]byte, error) {
	return bare.Marshal(t)
}

type Person interface {
	bare.Union
}


@@ 88,6 98,8 @@ func (_ Customer) IsUnion() { }

func (_ Employee) IsUnion() { }

func (_ TerminatedEmployee) IsUnion() { }

type Address struct {
	Address [4]string `bare:"address"`
	City    string `bare:"city"`


@@ 106,5 118,6 @@ func (t *Address) Encode() ([]byte, error) {
func init() {
	bare.RegisterUnion((*Person)(nil)).
		Member(*new(Customer), 0).
		Member(*new(Employee), 1)
		Member(*new(Employee), 1).
		Member(*new(TerminatedEmployee), 2)
}

A example/terminated.bin => example/terminated.bin +1 -0
@@ 0,0 1,1 @@


M schema/ast.go => schema/ast.go +5 -4
@@ 67,14 67,15 @@ const (
	F64
	Bool
	String
	// [len]type
	Array
	// []type
	Slice
	Void
	// data
	Data
	// data<length>
	DataFixed
	// [len]type
	Array
	// []type
	Slice
	// optional<type>
	Optional
	// data<len>

M schema/lex.go => schema/lex.go +5 -0
@@ 161,6 161,8 @@ func (sc *Scanner) scanWord() (Token, error) {
		return Token{TSTRING, ""}, nil
	case "data":
		return Token{TDATA, ""}, nil
	case "void":
		return Token{TVOID, ""}, nil
	case "optional":
		return Token{TOPTIONAL, ""}, nil
	case "map":


@@ 225,6 227,7 @@ const (
	TBOOL
	TSTRING
	TDATA
	TVOID
	TMAP
	TOPTIONAL



@@ 292,6 295,8 @@ func (t Token) String() string {
		return "string"
	case TDATA:
		return "data"
	case TVOID:
		return "void"
	case TMAP:
		return "map"
	case TOPTIONAL:

M schema/lex_test.go => schema/lex_test.go +1 -0
@@ 25,6 25,7 @@ func TestScanWords(t *testing.T) {
		"bool": TBOOL,
		"string": TSTRING,
		"data": TDATA,
		"void": TVOID,
		"map": TMAP,
		"optional": TOPTIONAL,
	}

M schema/parser.go => schema/parser.go +2 -0
@@ 233,6 233,8 @@ func parseType(scanner *Scanner) (Type, error) {
		return &PrimitiveType{Bool}, nil
	case TSTRING:
		return &PrimitiveType{String}, nil
	case TVOID:
		return &PrimitiveType{Void}, nil
	case TOPTIONAL:
		scanner.PushBack(tok)
		return parseOptionalType(scanner)

M schema/parser_test.go => schema/parser_test.go +2 -0
@@ 28,6 28,7 @@ func TestParsePrimitives(t *testing.T) {
		{ "MyF64", F64 },
		{ "MyBool", Bool },
		{ "MyString", String },
		{ "MyVoid", Void },
	}

	types, err := Parse(strings.NewReader(`


@@ 45,6 46,7 @@ func TestParsePrimitives(t *testing.T) {
		type MyF64 f64
		type MyBool bool
		type MyString string
		type MyVoid void
	`))
	assert.NoError(t, err)
	assert.Len(t, types, len(reference))