package srarchive import ( "encoding/json" "errors" "fmt" "strconv" ) type SourceSetDescriptorType rune const ( PixelDensity SourceSetDescriptorType = 'x' Width SourceSetDescriptorType = 'w' ) type SourceSetSpecifier struct { Value int DescriptorType SourceSetDescriptorType } func (s *SourceSetSpecifier) String() string { return fmt.Sprintf("%d%c", s.Value, s.DescriptorType) } func (s *SourceSetSpecifier) parse(value string) error { if len(value) < 2 { return errors.New("too short string") } s.DescriptorType = SourceSetDescriptorType(value[len(value)-1]) switch s.DescriptorType { case PixelDensity: case Width: default: return errors.New("invalid source-set-value descriptor") } var err error s.Value, err = strconv.Atoi(value[0 : len(value)-1]) if err != nil { return err } return nil } func (s *SourceSetSpecifier) UnmarshalJSON(b []byte) error { var str string err := json.Unmarshal(b, &str) if err != nil { return err } return s.parse(str) } func (s *SourceSetSpecifier) MarshalJSON() ([]byte, error) { return json.Marshal(s.String()) }