~sircmpwn/go-bare

ebcf80d60cab49ca0d293f3b46d56d21092163bd — Drew DeVault a month ago 2f4a595
Add examples
6 files changed, 70 insertions(+), 2 deletions(-)

M cmd/gen/gen.go
A examples/.gitignore
A examples/README.md
A examples/main.go
A examples/schema.bare
A examples/time.go
M cmd/gen/gen.go => cmd/gen/gen.go +2 -2
@@ 32,8 32,8 @@ func genUserType(w io.Writer, udt *schema.UserDefinedType) {
	genType(w, udt.Type(), 0)
	fmt.Fprintf(w, "\n")

	fmt.Fprintf(w, "\nfunc (t *%s) Decode(data []byte) {", udt.Name())
	fmt.Fprintf(w, "\n\tbare.Unmarshal(data, t)")
	fmt.Fprintf(w, "\nfunc (t *%s) Decode(data []byte) error {", udt.Name())
	fmt.Fprintf(w, "\n\treturn bare.Unmarshal(data, t)")
	fmt.Fprintf(w, "\n}\n")
}


A examples/.gitignore => examples/.gitignore +1 -0
@@ 0,0 1,1 @@
schema.go

A examples/README.md => examples/README.md +2 -0
@@ 0,0 1,2 @@
    go generate ./...
    go run .

A examples/main.go => examples/main.go +11 -0
@@ 0,0 1,11 @@
package main
//go:generate go run git.sr.ht/~sircmpwn/go-bare/cmd/gen -p main -s Time schema.bare schema.go

import (
	"log"
)

func main() {
	var dept = ACCOUNTING
	log.Printf("%s: %d", dept.String(), uint8(dept))
}

A examples/schema.bare => examples/schema.bare +40 -0
@@ 0,0 1,40 @@
type PublicKey data<128>
type Time string # ISO 8601

enum Department e8 {
	ACCOUNTING
	ADMINISTRATION
	CUSTOMER_SERVICE
	DEVELOPMENT

	# Reserved for the CEO
	JSMITH = 99
}

type Customer {
	name: string
	email: string
	address: Address
	orders: []{
		orderId: i64
		quantity: i32
	}
	metadata: map[string]data
}

type Employee {
	name: string
	email: string
	address: Address
	department: Department
	hireDate: Time
	publicKey: optional<PublicKey>
	metadata: map[string]data
}

type Address {
	address: [4]string
	city: string
	state: string
	country: string
}

A examples/time.go => examples/time.go +14 -0
@@ 0,0 1,14 @@
package main

import "time"

type Time time.Time

func (t *Time) Decode(data []byte) error {
	tm, err := time.Parse("2006-01-02T15:04:05Z0700", string(data))
	if err != nil {
		return err
	}
	*t = Time(tm)
	return nil
}