1 package json 2 3 import "github.com/gopherjs/gopherjs/js" 4 5 func getJSONTag(tag *js.Object) string { 6 strLen := tag.Length() 7 var val *js.Object 8 for i := 0; i < strLen; i++ { 9 start := i 10 for i < strLen && tag.Call("charCodeAt", i).Int() == ' ' { 11 i++ 12 } 13 var char int 14 for ; i < strLen; i++ { 15 char = tag.Call("charCodeAt", i).Int() 16 if char <= ' ' || char == ':' || char == '"' { 17 break 18 } 19 } 20 if i+1 >= strLen || char != ':' || tag.Call("charCodeAt", i+1).Int() != '"' { 21 break 22 } 23 name := tag.Call("substring", start, i) 24 i += 2 25 start = i 26 for ; i < strLen; i++ { 27 char = tag.Call("charCodeAt", i).Int() 28 if char == '\\' { 29 i++ 30 } else if char == '"' { 31 break 32 } 33 } 34 if name.String() == "json" { 35 val = tag.Call("substring", start, i) 36 break 37 } 38 } 39 if val == nil { 40 return "" 41 } 42 return val.String() 43 } 44 45 type tagOptions struct { 46 *js.Object 47 } 48 49 func parseTag(tag string) (string, tagOptions) { 50 split := js.Global.Get("String").Get("prototype").Get("indexOf").Call("call", tag, ",").Int() 51 if split < 0 { 52 return tag, tagOptions{nil} 53 } 54 return js.Global.Get("String").Get("prototype").Get("substr").Call("call", tag, 0, split).String(), tagOptions{js.Global.Get("String").Get("prototype").Get("substr").Call("call", tag, split+1)} 55 } 56 57 func (o tagOptions) Contains(option string) bool { 58 if o.Object == nil || o.Length() == 0 || o.Length() < len(option) { 59 return false 60 } 61 if o.Length() == len(option) { 62 return o.String() == option 63 } 64 return o.Call("substr", 0, len(option)+1).String() == option+"," || o.Call("substr", -1-len(option)).String() == ","+option || o.Call("includes", ","+option+",").Bool() 65 }