| 1 | package main |
| 2 | |
| 3 | //go:generate go run git.sr.ht/~sircmpwn/go-bare/cmd/gen -p example -s Time ../schema.bare ../schema.go |
| 4 | |
| 5 | import ( |
| 6 | "fmt" |
| 7 | "log" |
| 8 | "os" |
| 9 | "strings" |
| 10 | "time" |
| 11 | |
| 12 | "git.sr.ht/~sircmpwn/go-bare" |
| 13 | "git.sr.ht/~sircmpwn/go-bare/example" |
| 14 | ) |
| 15 | |
| 16 | func main() { |
| 17 | var person example.Person |
| 18 | err := bare.UnmarshalReader(os.Stdin, &person) |
| 19 | if err != nil { |
| 20 | log.Fatalf("decode: %e", err) |
| 21 | } |
| 22 | switch person := person.(type) { |
| 23 | case *example.Customer: |
| 24 | var addrs []string |
| 25 | for _, addr := range person.Address.Address { |
| 26 | if addr != "" { |
| 27 | addrs = append(addrs, addr) |
| 28 | } |
| 29 | } |
| 30 | fmt.Printf(`Customer details: |
| 31 | Name: %s |
| 32 | Email: %s |
| 33 | Address: |
| 34 | %s |
| 35 | %s, %s |
| 36 | %s |
| 37 | Orders: |
| 38 | `, person.Name, person.Email, strings.Join(addrs, "\n"), |
| 39 | person.Address.City, person.Address.State, |
| 40 | person.Address.Country) |
| 41 | for _, order := range person.Orders { |
| 42 | fmt.Printf("- Order ID: %d\n Quantity: %d\n", |
| 43 | order.OrderId, order.Quantity) |
| 44 | } |
| 45 | case *example.Employee: |
| 46 | var addrs []string |
| 47 | for _, addr := range person.Address.Address { |
| 48 | if addr != "" { |
| 49 | addrs = append(addrs, addr) |
| 50 | } |
| 51 | } |
| 52 | fmt.Printf(`Employee details: |
| 53 | Name: %s |
| 54 | Email: %s |
| 55 | Address: |
| 56 | %s |
| 57 | %s, %s |
| 58 | %s |
| 59 | Department: %s |
| 60 | Hire date: %s |
| 61 | `, person.Name, person.Email, strings.Join(addrs, "\n"), |
| 62 | person.Address.City, person.Address.State, |
| 63 | person.Address.Country, person.Department.String(), |
| 64 | time.Time(person.HireDate).Format(time.RFC3339)) |
| 65 | case *example.TerminatedEmployee: |
| 66 | log.Println("Terminated employee (no data)") |
| 67 | } |
| 68 | } |