1
0
Fork 0
jf2/type.go

63 lines
1.7 KiB
Go

package jf2
import (
"fmt"
"strconv"
"strings"
)
// Type defines the object classification.
//
// In microformats, this is presumed to be an h-* class from the microformats2
// vocabulary.
type Type struct{ value string }
var (
TypeUnd Type = Type{} // "und"
TypeAdr Type = Type{"adr"} // "adr"
TypeCard Type = Type{"card"} // "card"
TypeEntry Type = Type{"entry"} // "entry"
TypeEvent Type = Type{"event"} // "event"
TypeFeed Type = Type{"feed"} // "feed"
TypeGeo Type = Type{"geo"} // "geo"
TypeItem Type = Type{"item"} // "item"
TypeListing Type = Type{"listing"} // "listing"
TypeProduct Type = Type{"product"} // "product"
TypeRecipe Type = Type{"recipe"} // "recipe"
TypeResume Type = Type{"resume"} // "resume"
TypeReview Type = Type{"review"} // "review"
TypeReviewAggregate Type = Type{"review-aggregate"} // "review-aggregate"
)
func (t *Type) UnmarshalJSON(v []byte) error {
unquoted, err := strconv.Unquote(string(v))
if err != nil {
return fmt.Errorf("JF2: Type: cannot unquote value: %w", err)
}
t.value = strings.ToLower(strings.TrimSpace(unquoted))
return nil
}
func (t Type) MarshalJSON() ([]byte, error) {
if t.value != "" {
return []byte(strconv.Quote(t.value)), nil
}
return nil, nil
}
func (t Type) String() string {
if t.value != "" {
return t.value
}
return "und"
}
func (t Type) GoString() string {
return "jf2.Type(" + t.String() + ")"
}