123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- package xvdoc
- import (
- "bytes"
- "encoding/json"
- "fmt"
- )
- type dropDown struct {
- values []string
- x int
- y int
- dx int
- dy int
- }
- func (s *dropDown) UnmarshalJSON(src []byte) (err error) {
- s.y, s.x, s.dy, s.dx = -1, -1, -1, -1
- dec := json.NewDecoder(bytes.NewReader(src))
- var empty interface{}
- var t json.Token
- for dec.More() && err == nil {
- if t, err = dec.Token(); err == nil {
- if _, check := t.(json.Delim); !check {
- switch t.(string) {
- case "dx":
- err = dec.Decode(&s.dx)
- case "dy":
- err = dec.Decode(&s.dy)
- case "x":
- err = dec.Decode(&s.x)
- case "y":
- err = dec.Decode(&s.y)
- case "values":
- err = dec.Decode(&s.values)
- default:
- dec.Decode(&empty)
- }
- }
- }
- }
- if s.x == -1 {
- err = fmt.Errorf("xvdoc dropdown list parse error :: expected x field")
- return
- }
- if s.y == -1 {
- err = fmt.Errorf("xvdoc dropdown list parse error :: expected y field")
- return
- }
- if s.dx == -1 {
- s.dx = s.x
- }
- if s.dy == -1 {
- s.dy = -1
- }
- return
- }
|