super-graph/serv/vars.go

53 lines
1.1 KiB
Go
Raw Normal View History

2019-04-19 07:55:03 +02:00
package serv
import (
"io"
"strconv"
"github.com/valyala/fasttemplate"
)
2019-05-13 01:27:26 +02:00
func varMap(ctx *coreContext) variables {
2019-04-19 07:55:03 +02:00
userIDFn := func(w io.Writer, _ string) (int, error) {
if v := ctx.Value(userIDKey); v != nil {
return w.Write([]byte(v.(string)))
}
return 0, errNoUserID
}
userIDProviderFn := func(w io.Writer, _ string) (int, error) {
if v := ctx.Value(userIDProviderKey); v != nil {
return w.Write([]byte(v.(string)))
}
return 0, errNoUserID
}
userIDTag := fasttemplate.TagFunc(userIDFn)
userIDProviderTag := fasttemplate.TagFunc(userIDProviderFn)
vm := variables{
"user_id": userIDTag,
"user_id_provider": userIDProviderTag,
"USER_ID": userIDTag,
"USER_ID_PROVIDER": userIDProviderTag,
}
2019-05-13 01:27:26 +02:00
for k, v := range ctx.req.Vars {
var buf []byte
2019-04-19 07:55:03 +02:00
if _, ok := vm[k]; ok {
continue
}
switch val := v.(type) {
case string:
vm[k] = val
case int:
2019-05-13 01:27:26 +02:00
vm[k] = strconv.AppendInt(buf, int64(val), 10)
2019-04-19 07:55:03 +02:00
case int64:
2019-05-13 01:27:26 +02:00
vm[k] = strconv.AppendInt(buf, val, 10)
2019-04-19 07:55:03 +02:00
case float64:
2019-05-13 01:27:26 +02:00
vm[k] = strconv.AppendFloat(buf, val, 'f', -1, 64)
2019-04-19 07:55:03 +02:00
}
}
return vm
}