示例#1
0
func (r *resource) Template(template string, vars map[string]interface{}) (*url.URL, error) {
	// Build the template object
	tmpl, err := uritemplates.Parse(template)
	if err != nil {
		return nil, err
	}

	// Encode all of the values if required
	for k, v := range vars {
		if s, isString := v.(string); isString {
			vars[k] = restdata.MaybeEncodeName(s)
		}
		if ss, isStringSlice := v.([]string); isStringSlice {
			tt := make([]string, len(ss))
			for i, s := range ss {
				tt[i] = restdata.MaybeEncodeName(s)
			}
			vars[k] = tt
		}
	}

	// Expand the template to produce a string
	expanded, err := tmpl.Expand(vars)
	if err != nil {
		return nil, err
	}

	// Return the parsed URL of the result, relative to ourselves
	return r.URL.Parse(expanded)
}
示例#2
0
func buildURLs(router *mux.Router, params ...string) *urlBuilder {
	// Encode all of the values in params
	for i, value := range params {
		if i%2 == 1 {
			params[i] = restdata.MaybeEncodeName(value)
		}
	}
	return &urlBuilder{Router: router, Params: params}
}