2019-04-19 07:55:03 +02:00
|
|
|
package serv
|
|
|
|
|
|
|
|
import (
|
2019-09-05 06:09:56 +02:00
|
|
|
"bytes"
|
2019-11-25 08:22:33 +01:00
|
|
|
"context"
|
|
|
|
"errors"
|
2019-09-05 06:09:56 +02:00
|
|
|
"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-11-25 08:22:33 +01:00
|
|
|
func argMap(ctx context.Context, vars []byte) func(w io.Writer, tag string) (int, error) {
|
2019-09-05 06:09:56 +02:00
|
|
|
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 {
|
2019-11-21 08:14:12 +01:00
|
|
|
return io.WriteString(w, v.(string))
|
2019-10-24 08:07:42 +02:00
|
|
|
}
|
2019-11-25 08:22:33 +01:00
|
|
|
return 0, errors.New("query requires variable $user_id_provider")
|
2019-10-24 08:07:42 +02:00
|
|
|
|
2019-09-05 06:09:56 +02:00
|
|
|
case "user_id":
|
|
|
|
if v := ctx.Value(userIDKey); v != nil {
|
2019-11-21 08:14:12 +01:00
|
|
|
return io.WriteString(w, v.(string))
|
2019-09-05 06:09:56 +02:00
|
|
|
}
|
2019-11-25 08:22:33 +01:00
|
|
|
return 0, errors.New("query requires variable $user_id")
|
2019-10-24 08:07:42 +02:00
|
|
|
|
|
|
|
case "user_role":
|
|
|
|
if v := ctx.Value(userRoleKey); v != nil {
|
2019-11-21 08:14:12 +01:00
|
|
|
return io.WriteString(w, v.(string))
|
2019-09-05 06:09:56 +02:00
|
|
|
}
|
2019-11-25 08:22:33 +01:00
|
|
|
return 0, errors.New("query requires variable $user_role")
|
2019-04-19 07:55:03 +02:00
|
|
|
}
|
|
|
|
|
2019-11-25 08:22:33 +01:00
|
|
|
fields := jsn.Get(vars, [][]byte{[]byte(tag)})
|
2019-09-05 06:09:56 +02:00
|
|
|
if len(fields) == 0 {
|
2019-11-25 08:22:33 +01:00
|
|
|
return 0, nil
|
2019-09-05 06:09:56 +02:00
|
|
|
}
|
2019-12-09 07:48:18 +01:00
|
|
|
v := fields[0].Value
|
|
|
|
if len(v) >= 2 && v[0] == '"' && v[len(v)-1] == '"' {
|
|
|
|
fields[0].Value = v[1 : len(v)-1]
|
|
|
|
}
|
2019-04-19 07:55:03 +02:00
|
|
|
|
2019-11-21 08:14:12 +01:00
|
|
|
return w.Write(fields[0].Value)
|
2019-04-19 07:55:03 +02:00
|
|
|
}
|
|
|
|
}
|
2019-07-29 07:13:33 +02:00
|
|
|
|
2019-11-25 08:22:33 +01:00
|
|
|
func argList(ctx *coreContext, args [][]byte) ([]interface{}, error) {
|
2019-09-05 06:09:56 +02:00
|
|
|
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 {
|
2019-11-25 08:22:33 +01:00
|
|
|
return nil, err
|
2019-09-05 06:09:56 +02:00
|
|
|
}
|
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-11-25 08:22:33 +01:00
|
|
|
} else {
|
|
|
|
return nil, errors.New("query requires variable $user_id")
|
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-11-25 08:22:33 +01:00
|
|
|
} else {
|
|
|
|
return nil, errors.New("query requires variable $user_id_provider")
|
2019-07-29 07:13:33 +02:00
|
|
|
}
|
|
|
|
|
2019-11-05 05:44:42 +01:00
|
|
|
case bytes.Equal(av, []byte("user_role")):
|
|
|
|
if v := ctx.Value(userRoleKey); v != nil {
|
|
|
|
vars[i] = v.(string)
|
2019-11-25 08:22:33 +01:00
|
|
|
} else {
|
|
|
|
return nil, errors.New("query requires variable $user_role")
|
2019-11-05 05:44:42 +01:00
|
|
|
}
|
|
|
|
|
2019-09-05 06:09:56 +02:00
|
|
|
default:
|
|
|
|
if v, ok := fields[string(av)]; ok {
|
|
|
|
vars[i] = v
|
2019-11-25 08:22:33 +01:00
|
|
|
} else {
|
|
|
|
return nil, fmt.Errorf("query requires variable $%s", string(av))
|
|
|
|
|
2019-07-29 07:13:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-25 08:22:33 +01:00
|
|
|
return vars, nil
|
2019-07-29 07:13:33 +02:00
|
|
|
}
|