func (n *Node) SetAttr(name string, value interface{}) *Node { if n.Attrs == nil { n.Attrs = Attrs{} } n.Attrs[name] = types.ToString(value) return n }
func (n *Node) AddAttr(name string, value interface{}) *Node { if n.Attrs == nil { n.Attrs = Attrs{} } n.Attrs.Add(name, types.ToString(value)) return n }
// AddHeader is a conveniency function which adds a new HTTP header // and returns the same *Request, to allow chaining. The value is // converted to an string using types.ToString. func (r *Request) AddHeader(key string, value interface{}) *Request { if r.resp != nil { panic("can't add header after sending request") } r.Header.Add(key, types.ToString(value)) return r }
func encode(params map[string]interface{}) string { values := make(url.Values) for k, v := range params { values.Add(k, types.ToString(v)) } return values.Encode() }
func concat(args ...interface{}) string { s := make([]string, len(args)) for ii, v := range args { s[ii] = types.ToString(v) } return strings.Join(s, "") }
func quote(v reflect.Value) string { s := types.ToString(v.Interface()) if v.Kind() == reflect.String { return fmt.Sprintf("'%s'", s) } return s }
func setupFlags(fields fieldMap) (varMap, error) { m := make(varMap) for k, v := range fields { name := parameterName(k) help := v.Tag.Get("help") var p interface{} val := v.Value switch val.Type().Kind() { case reflect.Bool: p = flag.Bool(name, val.Bool(), help) case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32: p = flag.Int(name, int(val.Int()), help) case reflect.Int64: p = flag.Int64(name, val.Int(), help) case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32: p = flag.Uint(name, uint(val.Uint()), help) case reflect.Uint64: p = flag.Uint64(name, val.Uint(), help) case reflect.Float32, reflect.Float64: p = flag.Float64(name, val.Float(), help) case reflect.String: p = flag.String(name, val.String(), help) case reflect.Slice, reflect.Array: if !canParse(val.Type().Elem()) { return nil, fmt.Errorf("config field %q has unsupported type %s", k, val.Type()) } p = flag.String(name, sliceToString(val), help) case reflect.Map: if !canParse(val.Type().Elem()) || !canParse(val.Type().Key()) { return nil, fmt.Errorf("config field %q has unsupported type %s", k, val.Type()) } p = flag.String(name, mapToString(val), help) default: if _, ok := val.Interface().(input.Parser); !ok { return nil, fmt.Errorf("config field %q has unsupported type %s", k, val.Type()) } // Type implements input.Parser, define it as a string p = flag.String(name, types.ToString(val.Interface()), help) } m[name] = p } return m, nil }
func eval(obj interface{}, varname string) (string, error) { k := varname v := reflect.ValueOf(obj) dot := strings.IndexByte(k, '.') if dot >= 0 { k = k[:dot] } if k == "Vars" { return eval(obj, varname[dot+1:]) } res, err := lookup(v, k) if err != nil { return "", err } if dot >= 0 { return eval(res.Interface(), varname[dot+1:]) } return types.ToString(res.Interface()), nil }
func toHTMLValue(val interface{}) string { v := reflect.ValueOf(val) if v.IsValid() { for v.Kind() == reflect.Ptr { v = v.Elem() } if v.IsValid() { // Avoid enum types with a String() method to be represented // as a string. Use their numeric representation. k := types.Kind(v.Kind()) if k == types.Int { return strconv.FormatInt(v.Int(), 10) } if k == types.Uint { return strconv.FormatUint(v.Uint(), 10) } } } return html.Escape(types.ToString(val)) }
func (f *Form) writeInput(buf *bytes.Buffer, itype string, field *Field) error { if err := f.beginInput(buf, field, -1); err != nil { return err } attrs := html.Attrs{ "id": field.Id(), "type": itype, "name": field.HTMLName, } if err := f.prepareFieldAttributes(field, attrs, -1); err != nil { return err } switch field.Type { case CHECKBOX: if t, ok := types.IsTrue(field.value.Interface()); t && ok { attrs["checked"] = "checked" } case TEXT, PASSWORD, EMAIL, HIDDEN: attrs["value"] = html.Escape(types.ToString(field.Value())) if field.Placeholder != "" { attrs["placeholder"] = html.Escape(field.Placeholder.TranslatedString(f.ctx)) } if ml, ok := field.Tag().MaxLength(); ok { attrs["maxlength"] = strconv.Itoa(ml) } case FILE: default: panic("unreachable") } f.openTag(buf, "input", attrs) if field.Type == CHECKBOX { // Close the label before calling EndInput if err := f.endLabel(buf, field, field.Label.TranslatedString(f.ctx), -1); err != nil { return err } } if err := f.endInput(buf, field, -1); err != nil { return err } return nil }
func (n *Node) RemoveAttr(name string, value interface{}) *Node { if n.Attrs != nil { n.Attrs.Remove(name, types.ToString(value)) } return n }
func (r *Request) expect(what interface{}, name string, value interface{}) *Request { if r.do() { var s string switch v := value.(type) { case string: s = v case []byte: s = string(v) default: s = types.ToString(value) } switch x := what.(type) { case int: val, err := strconv.ParseInt(s, 0, 64) if err != nil { r.errorf("expecting %s = %d, got non numeric value %q instead", name, x, s) } else if val != int64(x) { r.errorf("expecting %s = %d, got %d instead", name, x, val) } case []byte: if val, ok := value.([]byte); ok { if !bytes.Equal(val, x) { r.errorf("expecting %s = %v, got %v instead", name, x, val) } } else { if s != string(x) { r.errorf("expecting %s = %q, got %q instead", name, string(x), s) } } case string: if s != x { r.errorf("expecting %s = %q, got %q instead", name, x, s) } case Contains: if !strings.Contains(s, string(x)) { r.errorf("expecting %s containing %q, got %q instead", name, x, s) } case Match: re, err := regexp.Compile(string(x)) if err != nil { r.err = fmt.Errorf("error compiling regular expression %q: %s", x, err) r.Reporter.Fatal(r.err) break } if !re.MatchString(s) { r.errorf("expecting %s matching %q, got %q instead", name, x, s) } case io.Reader: data, err := ioutil.ReadAll(x) if err != nil { r.errorf("error reading expected body: %s", err) break } return r.expect(data, name, value) case nil: if val, ok := value.([]byte); ok { if len(val) > 0 { r.errorf("expecting empty %s, got %v instead", name, val) } } else if len(s) > 0 { r.errorf("expecting empty %s, got %q instead", name, s) } default: r.Reporter.Fatal(fmt.Errorf("don't know what to expect from %T", what)) } } return r }