summaryrefslogtreecommitdiff
path: root/example/main.go
blob: 078dbce1f938508cc7edc12b5d822ac22a682623 (plain)
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
//go:build go1.20
// +build go1.20

package main

import (
	"fmt"
	"math/rand"
	"os"

	"go.wit.com/log"
)

// sent via -ldflags
var VERSION string
var BUILDTIME string

var sortmap map[string]string
var marshalKeys []string
var uniqueKeys []string

var counter int = rand.Intn(100)

func main() {
	var pb *Fruits
	// pb = NewFruits()
	pb = new(Fruits)
	// attempt to load basket.pb
	if err := pb.loadBasket(); err != nil {
		log.Info("load basket failed. running this for the first time?")
		a := pb.getNewFruit()
		pb.Fruits = append(pb.Fruits, a)
		pb.printTable()
	}
	if err := pb.saveBasket(); err != nil {
		badExit(err)
	}
	if pb == nil {
		badExit(fmt.Errorf("pb == nil This should not have happened"))
	}
	pb.addThings()
	pb.printTable()
	pb.saveBasket()
	pb.sortTable(100)
}

func badExit(err error) {
	log.Info("autogenpb error:", err)
	os.Exit(-1)
}

func (pb *Fruits) getNewFruit() *Fruit {
	counter += rand.Intn(100)
	upc := fmt.Sprintf("%d", counter)
	fut := &Fruit{
		Brand: "mom",
		City:  "New NewYork",
		UPC:   upc,
	}
	return fut
}

/*
	x := new(Fruit)
	x = &Fruit{
		Brand: "fry",
		City:  "Germany",
	}
	pb.printTable()

	testAppend(x)
*/

func (pb *Fruits) printTable() {
	var count int
	all := pb.All()
	for all.Scan() {
		tmp := all.Next()
		count += 1
		log.Printf("found %d %s %s\n", count, tmp.Brand, tmp.City)
		if count > 5 {
			break
		}
	}
	log.Printf("Total entries: %d\n", pb.Len())
}

func (pb *Fruits) sortTable(i int) {
	var count int
	all := pb.SortByBrand()
	for all.Scan() {
		tmp := all.Next()
		count += 1
		log.Printf("found %d %s %s\n", count, tmp.Brand, tmp.City)
		if count > i {
			break
		}
	}
	log.Printf("Total entries: %d\n", pb.Len())
}

// adds 10 new entries
func (pb *Fruits) addThings() {
	var added int
	var count int = rand.Intn(10000)
	// var base string = "bender"
	for {
		count += rand.Intn(1000)
		var found bool
		name := fmt.Sprintf("bender%d", count)
		all := pb.All()
		for all.Scan() {
			tmp := all.Next()
			if tmp.Brand == name {
				// log.Printf("tmp name EQUAL %d %s vs %s city = %s len=%d\n", count, tmp.Brand, name, tmp.City, pb.Len())
				found = true
				break
			}
			// log.Printf("DID NOT EXIST %d %s vs %s city = %s len=%d\n", count, tmp.Brand, name, tmp.City, pb.Len())
		}
		if found {
			continue
		}
		// log.Printf("DID NOT EXIST %d %s len=%d\n", count, name, pb.Len())
		tmp := new(Fruit)
		tmp.Brand = name
		tmp.City = "paris"
		if pb.AppendUniqueByBrand(tmp) {
			// log.Printf("AppendUniqueBrand() ok   len=%s %s %d\n", tmp.Brand, tmp.City, pb.Len())
		} else {
			badExit(fmt.Errorf("AppendUniqueByBrand() ERROR len=%s %s %d\n", tmp.Brand, tmp.City, pb.Len()))
		}
		pb.Append(tmp)
		added += 1
		if added > 10 {
			return
		}
	}
}

func (pb *Fruits) saveBasket() error {
	data, err := pb.Marshal()
	if err != nil {
		return err
	}

	w, err := os.OpenFile("basket.pb", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
	if err != nil {
		return err
	}
	w.Write(data)
	w.Close()
	log.Info("saved basket.pb ok")
	return nil
}

func (pb *Fruits) loadBasket() error {
	if pb == nil {
		pb = new(Fruits)
	}
	data, err := os.ReadFile("basket.pb")
	if err != nil {
		return err
	}

	if err := pb.Unmarshal(data); err != nil {
		return err
	}
	log.Info("loaded basket.pb ok")
	return nil
}