type PurchaseType byte const ( Free PurchaseType = iota Rent Buy ) func (t *PurchaseType) UnmarshalJSON(data []byte) error { var s string if err := json.Unmarshal(data, &s); err != nil { return fmt.Errorf("purchase type should be a string, got %s", data) } have, ok := map[string]PurchaseType{"Free": Free, "Rent": Rent, "Buy": Buy}[s] if !ok { return fmt.Errorf("invalid purchase type %q", s) } *t = have return nil }
[{Inception 0.99 Rent} {Johnny Mnemonic 0 Free} {John Wick 17.99 Buy}]