2019-04-19 07:55:03 +02:00
|
|
|
package serv
|
|
|
|
|
|
|
|
import (
|
2019-09-05 06:09:56 +02:00
|
|
|
"bytes"
|
|
|
|
"fmt"
|
2019-04-19 07:55:03 +02:00
|
|
|
"io"
|
|
|
|
|
2019-09-05 06:09:56 +02:00
|
|
|
"github.com/dosco/super-graph/jsn"
|
2019-04-19 07:55:03 +02:00
|
|
|
)
|
|
|
|
|
2019-09-05 06:09:56 +02:00
|
|
|
func varMap(ctx *coreContext) func(w io.Writer, tag string) (int, error) {
|
|
|
|
return func(w io.Writer, tag string) (int, error) {
|
|
|
|
switch tag {
|
2019-10-24 08:07:42 +02:00
|
|
|
case "user_id_provider":
|
|
|
|
if v := ctx.Value(userIDProviderKey); v != nil {
|
|
|
|
return stringVar(w, v.(string))
|
|
|
|
}
|
|
|
|
io.WriteString(w, "null")
|
|
|
|
return 0, nil
|
|
|
|
|
2019-09-05 06:09:56 +02:00
|
|
|
case "user_id":
|
|
|
|
if v := ctx.Value(userIDKey); v != nil {
|
|
|
|
return stringVar(w, v.(string))
|
|
|
|
}
|
2019-04-19 07:55:03 +02:00
|
|
|
|
2019-10-24 08:07:42 +02:00
|
|
|
io.WriteString(w, "null")
|
|
|
|
return 0, nil
|
|
|
|
|
|
|
|
case "user_role":
|
|
|
|
if v := ctx.Value(userRoleKey); v != nil {
|
2019-09-05 06:09:56 +02:00
|
|
|
return stringVar(w, v.(string))
|
|
|
|
}
|
2019-10-24 08:07:42 +02:00
|
|
|
io.WriteString(w, "null")
|
|
|
|
return 0, nil
|
2019-04-19 07:55:03 +02:00
|
|
|
}
|
|
|
|
|
2019-09-05 06:09:56 +02:00
|
|
|
fields := jsn.Get(ctx.req.Vars, [][]byte{[]byte(tag)})
|
|
|
|
if len(fields) == 0 {
|
|
|
|
return 0, fmt.Errorf("variable '%s' not found", tag)
|
|
|
|
}
|
2019-04-19 07:55:03 +02:00
|
|
|
|
2019-09-05 06:09:56 +02:00
|
|
|
is := false
|
2019-07-29 07:13:33 +02:00
|
|
|
|
2019-09-05 06:09:56 +02:00
|
|
|
for i := range fields[0].Value {
|
|
|
|
c := fields[0].Value[i]
|
|
|
|
if c != ' ' {
|
|
|
|
is = (c == '"') || (c == '{') || (c == '[')
|
|
|
|
break
|
|
|
|
}
|
2019-04-19 07:55:03 +02:00
|
|
|
}
|
2019-07-29 07:13:33 +02:00
|
|
|
|
2019-09-05 06:09:56 +02:00
|
|
|
if is {
|
|
|
|
return stringVarB(w, fields[0].Value)
|
2019-04-19 07:55:03 +02:00
|
|
|
}
|
2019-09-05 06:09:56 +02:00
|
|
|
|
|
|
|
w.Write(fields[0].Value)
|
|
|
|
return 0, nil
|
2019-04-19 07:55:03 +02:00
|
|
|
}
|
|
|
|
}
|
2019-07-29 07:13:33 +02:00
|
|
|
|
2019-09-05 06:09:56 +02:00
|
|
|
func varList(ctx *coreContext, args [][]byte) []interface{} {
|
|
|
|
vars := make([]interface{}, len(args))
|
|
|
|
|
|
|
|
var fields map[string]interface{}
|
|
|
|
var err error
|
2019-07-29 07:13:33 +02:00
|
|
|
|
2019-09-05 06:09:56 +02:00
|
|
|
if len(ctx.req.Vars) != 0 {
|
|
|
|
fields, _, err = jsn.Tree(ctx.req.Vars)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
logger.Warn().Err(err).Msg("Failed to parse variables")
|
|
|
|
}
|
2019-07-29 07:13:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for i := range args {
|
2019-09-05 06:09:56 +02:00
|
|
|
av := args[i]
|
2019-07-29 07:13:33 +02:00
|
|
|
|
2019-09-05 06:09:56 +02:00
|
|
|
switch {
|
|
|
|
case bytes.Equal(av, []byte("user_id")):
|
2019-07-29 07:13:33 +02:00
|
|
|
if v := ctx.Value(userIDKey); v != nil {
|
2019-09-05 06:09:56 +02:00
|
|
|
vars[i] = v.(string)
|
2019-07-29 07:13:33 +02:00
|
|
|
}
|
|
|
|
|
2019-09-05 06:09:56 +02:00
|
|
|
case bytes.Equal(av, []byte("user_id_provider")):
|
2019-07-29 07:13:33 +02:00
|
|
|
if v := ctx.Value(userIDProviderKey); v != nil {
|
2019-09-05 06:09:56 +02:00
|
|
|
vars[i] = v.(string)
|
2019-07-29 07:13:33 +02:00
|
|
|
}
|
|
|
|
|
2019-09-05 06:09:56 +02:00
|
|
|
default:
|
|
|
|
if v, ok := fields[string(av)]; ok {
|
|
|
|
vars[i] = v
|
2019-07-29 07:13:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return vars
|
|
|
|
}
|
2019-09-05 06:09:56 +02:00
|
|
|
|
|
|
|
func stringVar(w io.Writer, v string) (int, error) {
|
|
|
|
if n, err := w.Write([]byte(`'`)); err != nil {
|
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
if n, err := w.Write([]byte(v)); err != nil {
|
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
return w.Write([]byte(`'`))
|
|
|
|
}
|
|
|
|
|
|
|
|
func stringVarB(w io.Writer, v []byte) (int, error) {
|
|
|
|
if n, err := w.Write([]byte(`'`)); err != nil {
|
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
if n, err := w.Write(v); err != nil {
|
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
return w.Write([]byte(`'`))
|
|
|
|
}
|