| 1 | package example |
| 2 | |
| 3 | // Code generated by go-bare/cmd/gen, DO NOT EDIT. |
| 4 | |
| 5 | import ( |
| 6 | "errors" |
| 7 | "git.sr.ht/~sircmpwn/go-bare" |
| 8 | ) |
| 9 | |
| 10 | type PublicKey [128]byte |
| 11 | |
| 12 | func (t *PublicKey) Decode(data []byte) error { |
| 13 | return bare.Unmarshal(data, t) |
| 14 | } |
| 15 | |
| 16 | func (t *PublicKey) Encode() ([]byte, error) { |
| 17 | return bare.Marshal(t) |
| 18 | } |
| 19 | |
| 20 | type Customer struct { |
| 21 | Name string `bare:"name"` |
| 22 | Email string `bare:"email"` |
| 23 | Address Address `bare:"address"` |
| 24 | Orders []struct { |
| 25 | OrderId int64 `bare:"orderId"` |
| 26 | Quantity int32 `bare:"quantity"` |
| 27 | } `bare:"orders"` |
| 28 | Metadata map[string][]byte `bare:"metadata"` |
| 29 | } |
| 30 | |
| 31 | func (t *Customer) Decode(data []byte) error { |
| 32 | return bare.Unmarshal(data, t) |
| 33 | } |
| 34 | |
| 35 | func (t *Customer) Encode() ([]byte, error) { |
| 36 | return bare.Marshal(t) |
| 37 | } |
| 38 | |
| 39 | type Employee struct { |
| 40 | Name string `bare:"name"` |
| 41 | Email string `bare:"email"` |
| 42 | Address Address `bare:"address"` |
| 43 | Department Department `bare:"department"` |
| 44 | HireDate Time `bare:"hireDate"` |
| 45 | PublicKey *PublicKey `bare:"publicKey"` |
| 46 | Metadata map[string][]byte `bare:"metadata"` |
| 47 | } |
| 48 | |
| 49 | func (t *Employee) Decode(data []byte) error { |
| 50 | return bare.Unmarshal(data, t) |
| 51 | } |
| 52 | |
| 53 | func (t *Employee) Encode() ([]byte, error) { |
| 54 | return bare.Marshal(t) |
| 55 | } |
| 56 | |
| 57 | type TerminatedEmployee struct{} |
| 58 | |
| 59 | func (t *TerminatedEmployee) Decode(data []byte) error { |
| 60 | return bare.Unmarshal(data, t) |
| 61 | } |
| 62 | |
| 63 | func (t *TerminatedEmployee) Encode() ([]byte, error) { |
| 64 | return bare.Marshal(t) |
| 65 | } |
| 66 | |
| 67 | type Address struct { |
| 68 | Address [4]string `bare:"address"` |
| 69 | City string `bare:"city"` |
| 70 | State string `bare:"state"` |
| 71 | Country string `bare:"country"` |
| 72 | } |
| 73 | |
| 74 | func (t *Address) Decode(data []byte) error { |
| 75 | return bare.Unmarshal(data, t) |
| 76 | } |
| 77 | |
| 78 | func (t *Address) Encode() ([]byte, error) { |
| 79 | return bare.Marshal(t) |
| 80 | } |
| 81 | |
| 82 | type Department uint |
| 83 | |
| 84 | const ( |
| 85 | ACCOUNTING Department = 0 |
| 86 | ADMINISTRATION Department = 1 |
| 87 | CUSTOMER_SERVICE Department = 2 |
| 88 | DEVELOPMENT Department = 3 |
| 89 | JSMITH Department = 99 |
| 90 | ) |
| 91 | |
| 92 | func (t Department) String() string { |
| 93 | switch t { |
| 94 | case ACCOUNTING: |
| 95 | return "ACCOUNTING" |
| 96 | case ADMINISTRATION: |
| 97 | return "ADMINISTRATION" |
| 98 | case CUSTOMER_SERVICE: |
| 99 | return "CUSTOMER_SERVICE" |
| 100 | case DEVELOPMENT: |
| 101 | return "DEVELOPMENT" |
| 102 | case JSMITH: |
| 103 | return "JSMITH" |
| 104 | } |
| 105 | panic(errors.New("Invalid Department value")) |
| 106 | } |
| 107 | |
| 108 | type Person interface { |
| 109 | bare.Union |
| 110 | } |
| 111 | |
| 112 | func (_ Customer) IsUnion() {} |
| 113 | |
| 114 | func (_ Employee) IsUnion() {} |
| 115 | |
| 116 | func (_ TerminatedEmployee) IsUnion() {} |
| 117 | |
| 118 | func init() { |
| 119 | bare.RegisterUnion((*Person)(nil)). |
| 120 | Member(*new(Customer), 0). |
| 121 | Member(*new(Employee), 1). |
| 122 | Member(*new(TerminatedEmployee), 2) |
| 123 | |
| 124 | } |