46 lines
1.2 KiB
Go
Raw Permalink Normal View History

2022-03-08 22:09:04 +08:00
package main
import (
"sync"
"time"
)
// Fruit represents a sweet, fresh fruit.
type Fruit struct {
Name string `json:"name" validate:"required" example:"banana"`
Origin string `json:"origin" validate:"required" description:"Country of origin of the fruit" enum:"ecuador,france,senegal,china,spain"`
Price float64 `json:"price" validate:"required" description:"Price in euros" example:"5.13"`
AddedAt time.Time `json:"-" binding:"-" description:"Date of addition of the fruit to the market"`
}
// TypeName implements openapi.Typer interface for Fruit.
func (f *Fruit) TypeName() string { return "RottenFruit" }
// Market is a fruit market.
type Market struct {
fruits map[string]*Fruit
sync.RWMutex
}
var market *Market
func init() {
market = &Market{
fruits: make(map[string]*Fruit),
RWMutex: sync.RWMutex{},
}
for _, f := range fruits {
market.fruits[f.Name] = f
}
}
var fruits = []*Fruit{
{"banana", "ecuador", 2.99, time.Now()},
{"apricot", "france", 4.50, time.Now()},
{"mango", "senegal", 6.99, time.Now()},
{"litchi", "china", 5.65, time.Now()},
{"apple", "france", 2.49, time.Now()},
{"peach", "spain", 3.20, time.Now()},
{"peach", "spain", 3.20, time.Now()},
}