Compare commits

...

4 Commits

Author SHA1 Message Date
75cba09617 Make hat size random 2021-07-31 13:34:43 -05:00
e2cf17ff80 Rename model conversion functions 2021-07-27 18:00:35 -05:00
a35901b976 Make hat selection a variable 2021-07-27 00:34:53 -05:00
df49dcb9ea Fix typo 2021-07-26 20:39:39 -05:00
2 changed files with 18 additions and 12 deletions

View File

@ -3,12 +3,15 @@ package main
import ( import (
"context" "context"
"fmt" "fmt"
"math/rand"
"net/http" "net/http"
"os" "os"
"haberdasher-twirp/haberdasher" "haberdasher-twirp/haberdasher"
) )
const MaxSize = 12
func main() { func main() {
var host string = "http://localhost:8080" var host string = "http://localhost:8080"
if len(os.Args) > 1 { if len(os.Args) > 1 {
@ -17,7 +20,7 @@ func main() {
client := haberdasher.NewHaberdasherProtobufClient(host, &http.Client{}) client := haberdasher.NewHaberdasherProtobufClient(host, &http.Client{})
hat, err := client.MakeHat(context.Background(), &haberdasher.Size{Inches: 12}) hat, err := client.MakeHat(context.Background(), &haberdasher.Size{Inches: int32(rand.Intn(MaxSize))})
if err != nil { if err != nil {
fmt.Printf("oh no: %v\n", err) fmt.Printf("oh no: %v\n", err)
os.Exit(1) os.Exit(1)

View File

@ -21,7 +21,7 @@ type Server struct {
} }
type Hat struct { type Hat struct {
Inches int32 `json:"inchues"` Inches int32 `json:"inches"`
Color string `json:"color"` Color string `json:"color"`
Name string `json:"name"` Name string `json:"name"`
} }
@ -34,7 +34,7 @@ type Store struct {
db *bolt.DB db *bolt.DB
} }
func HatToHatModel(h *pb.Hat) Hat { func PbToHatModel(h *pb.Hat) Hat {
return Hat{ return Hat{
Inches: h.Inches, Inches: h.Inches,
Color: h.Color, Color: h.Color,
@ -42,7 +42,7 @@ func HatToHatModel(h *pb.Hat) Hat {
} }
} }
func HatModelToHat(h Hat) *pb.Hat { func HatModelToPb(h Hat) *pb.Hat {
return &pb.Hat{ return &pb.Hat{
Inches: h.Inches, Inches: h.Inches,
Color: h.Color, Color: h.Color,
@ -50,22 +50,25 @@ func HatModelToHat(h Hat) *pb.Hat {
} }
} }
func HatsModelToHats(hs []Hat) (hats *pb.Hats) { func HatsModelToPb(hs []Hat) (hats *pb.Hats) {
hats = &pb.Hats{} hats = &pb.Hats{}
for _, h := range hs { for _, h := range hs {
hat := HatModelToHat(h) hat := HatModelToPb(h)
hats.Hats = append(hats.Hats, hat) hats.Hats = append(hats.Hats, hat)
} }
return hats return hats
} }
func HatQueryToHatQueryModel(q *pb.HatQuery) HatQuery { func PbToHatQueryModel(q *pb.HatQuery) HatQuery {
return HatQuery{ return HatQuery{
Limit: q.Limit, Limit: q.Limit,
} }
} }
func (s *Server) MakeHat(ctx context.Context, size *pb.Size) (hat *pb.Hat, err error) { func (s *Server) MakeHat(ctx context.Context, size *pb.Size) (hat *pb.Hat, err error) {
var colors []string = []string{"white", "black", "red", "blue", "white"}
var names []string = []string{"bowler", "baseball cap", "top hat", "derby", "tricorne"}
st, _ := NewStore(s.DBPath, 0600, nil) st, _ := NewStore(s.DBPath, 0600, nil)
defer st.Close() defer st.Close()
@ -75,24 +78,24 @@ func (s *Server) MakeHat(ctx context.Context, size *pb.Size) (hat *pb.Hat, err e
h := Hat{ h := Hat{
Inches: size.Inches, Inches: size.Inches,
Color: []string{"white", "black", "red", "blue"}[rand.Intn(4)], Color: colors[rand.Intn(len(colors))],
Name: []string{"bowler", "baseball cap", "top hat", "derby"}[rand.Intn(3)], Name: names[rand.Intn(len(names))],
} }
fmt.Printf("made hat: %+v\n", h) fmt.Printf("made hat: %+v\n", h)
st.SaveHat(h) st.SaveHat(h)
return HatModelToHat(h), nil return HatModelToPb(h), nil
} }
func (s *Server) ListHats(ctx context.Context, q *pb.HatQuery) (hats *pb.Hats, err error) { func (s *Server) ListHats(ctx context.Context, q *pb.HatQuery) (hats *pb.Hats, err error) {
st, _ := NewStore(s.DBPath, 0600, nil) st, _ := NewStore(s.DBPath, 0600, nil)
defer st.Close() defer st.Close()
hs, err := st.ListHats(HatQueryToHatQueryModel(q)) hs, err := st.ListHats(PbToHatQueryModel(q))
return HatsModelToHats(hs), nil return HatsModelToPb(hs), nil
} }
func itob(v uint64) []byte { func itob(v uint64) []byte {