File: main.go

package info (click to toggle)
golang-github-linkedin-goavro 2.10.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 944 kB
  • sloc: makefile: 8
file content (172 lines) | stat: -rw-r--r-- 3,623 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package main

import (
	"fmt"
	"io/ioutil"
	"os"
	"reflect"

	"github.com/linkedin/goavro/v2"
)

var (
	codec *goavro.Codec
)

func init() {

	schema, err := ioutil.ReadFile("schema.avsc")
	if err != nil {
		panic(err)
	}

	//Create Schema Once
	codec, err = goavro.NewCodec(string(schema))
	if err != nil {
		panic(err)
	}
}

func main() {

	//Sample Data
	user := &User{
		FirstName: "John",
		LastName:  "Snow",
		Address: &Address{
			Address1: "1106 Pennsylvania Avenue",
			City:     "Wilmington",
			State:    "DE",
			Zip:      19806,
		},
	}

	fmt.Printf("user in=%+v\n", user)

	///Convert Binary From Native
	binary, err := codec.BinaryFromNative(nil, user.ToStringMap())
	if err != nil {
		panic(err)
	}

	///Convert Native from Binary
	native, _, err := codec.NativeFromBinary(binary)
	if err != nil {
		panic(err)
	}

	//Convert it back tp Native
	userOut := StringMapToUser(native.(map[string]interface{}))
	fmt.Printf("user out=%+v\n", userOut)
	if ok := reflect.DeepEqual(user, userOut); !ok {
		fmt.Fprintf(os.Stderr, "struct Compare Failed ok=%t\n", ok)
		os.Exit(1)
	}
}

// User holds information about a user.
type User struct {
	FirstName string
	LastName  string
	Errors    []string
	Address   *Address
}

// Address holds information about an address.
type Address struct {
	Address1 string
	Address2 string
	City     string
	State    string
	Zip      int
}

// ToStringMap returns a map representation of the User.
func (u *User) ToStringMap() map[string]interface{} {
	datumIn := map[string]interface{}{
		"FirstName": string(u.FirstName),
		"LastName":  string(u.LastName),
	}

	if len(u.Errors) > 0 {
		datumIn["Errors"] = goavro.Union("array", u.Errors)
	} else {
		datumIn["Errors"] = goavro.Union("null", nil)
	}

	if u.Address != nil {
		addDatum := map[string]interface{}{
			"Address1": string(u.Address.Address1),
			"City":     string(u.Address.City),
			"State":    string(u.Address.State),
			"Zip":      int(u.Address.Zip),
		}
		if u.Address.Address2 != "" {
			addDatum["Address2"] = goavro.Union("string", u.Address.Address2)
		} else {
			addDatum["Address2"] = goavro.Union("null", nil)
		}

		//important need namespace and record name
		datumIn["Address"] = goavro.Union("my.namespace.com.address", addDatum)

	} else {
		datumIn["Address"] = goavro.Union("null", nil)
	}
	return datumIn
}

// StringMapToUser returns a User from a map representation of the User.
func StringMapToUser(data map[string]interface{}) *User {

	ind := &User{}
	for k, v := range data {
		switch k {
		case "FirstName":
			if value, ok := v.(string); ok {
				ind.FirstName = value
			}
		case "LastName":
			if value, ok := v.(string); ok {
				ind.LastName = value
			}
		case "Errors":
			if value, ok := v.(map[string]interface{}); ok {
				for _, item := range value["array"].([]interface{}) {
					ind.Errors = append(ind.Errors, item.(string))
				}
			}
		case "Address":
			if vmap, ok := v.(map[string]interface{}); ok {
				//important need namespace and record name
				if cookieSMap, ok := vmap["my.namespace.com.address"].(map[string]interface{}); ok {
					add := &Address{}
					for k, v := range cookieSMap {
						switch k {
						case "Address1":
							if value, ok := v.(string); ok {
								add.Address1 = value
							}
						case "Address2":
							if value, ok := v.(string); ok {
								add.Address2 = value
							}
						case "City":
							if value, ok := v.(string); ok {
								add.City = value
							}
						case "Zip":
							if value, ok := v.(int); ok {
								add.Zip = value
							}
						}
					}
					ind.Address = add
				}
			}
		}

	}
	return ind

}