File size: 4,100 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
package graphql2
import (
"reflect"
"github.com/99designs/gqlgen/graphql"
"github.com/expr-lang/expr"
"github.com/expr-lang/expr/ast"
"github.com/expr-lang/expr/parser"
"github.com/target/goalert/validation"
)
// ExprIsID returns true if the node is a valid identifier.
func ExprIsID(n ast.Node) bool {
switch t := n.(type) {
case *ast.IdentifierNode:
return true
case *ast.MemberNode:
return !t.Method && ExprIsID(t.Node) && (ExprIsID(t.Property) || ExprIsLiteral(t.Property))
case *ast.ChainNode:
return ExprIsID(t.Node)
}
return false
}
// ExprIsLiteral returns true if the node is a literal value (scalar or array of scalar values).
func ExprIsLiteral(n ast.Node) bool {
switch t := n.(type) {
case *ast.StringNode:
case *ast.IntegerNode:
case *ast.BoolNode:
case *ast.FloatNode:
case *ast.UnaryNode:
if t.Operator != "-" {
return false
}
switch t.Node.(type) {
case *ast.IntegerNode:
return true
case *ast.FloatNode:
return true
}
return false
case *ast.ArrayNode:
for _, v := range t.Nodes {
if !ExprIsLiteral(v) {
return false
}
}
default:
return false
}
return true
}
func MarshalExprExpression(s string) graphql.Marshaler { return graphql.MarshalString(s) }
func MarshalExprBooleanExpression(s string) graphql.Marshaler { return graphql.MarshalString(s) }
func MarshalExprStringExpression(s string) graphql.Marshaler { return graphql.MarshalString(s) }
func exprExpressionWith(v interface{}, opts ...expr.Option) (string, error) {
str, err := graphql.UnmarshalString(v)
if err != nil {
return "", err
}
_, err = expr.Compile(str, opts...)
if err != nil {
return "", validation.WrapError(err)
}
return str, nil
}
func UnmarshalExprExpression(v interface{}) (string, error) { return exprExpressionWith(v) }
func UnmarshalExprBooleanExpression(v interface{}) (string, error) {
return exprExpressionWith(v, expr.AsBool())
}
func UnmarshalExprStringExpression(v interface{}) (string, error) {
return exprExpressionWith(v, expr.AsKind(reflect.String))
}
func MarshalExprValue(n ast.Node) graphql.Marshaler { return graphql.MarshalString(n.String()) }
func MarshalExprIdentifier(n ast.Node) graphql.Marshaler { return graphql.MarshalString(n.String()) }
func MarshalExprOperator(op string) graphql.Marshaler { return graphql.MarshalString(op) }
func exprVal(v interface{}) (ast.Node, error) {
str, err := graphql.UnmarshalString(v)
if err != nil {
return nil, err
}
t, err := parser.Parse(str)
if err != nil {
return nil, validation.WrapError(err)
}
return t.Node, nil
}
func UnmarshalExprValue(v interface{}) (ast.Node, error) {
n, err := exprVal(v)
if err != nil {
return nil, validation.WrapError(err)
}
if !ExprIsLiteral(n) {
return nil, validation.NewGenericError("must be a literal value")
}
return n, nil
}
func UnmarshalExprIdentifier(v interface{}) (ast.Node, error) {
n, err := exprVal(v)
if err != nil {
return nil, validation.WrapError(err)
}
if !ExprIsID(n) {
return nil, validation.NewGenericError("must be an identifier")
}
return n, nil
}
func UnmarshalExprOperator(v interface{}) (string, error) {
n, err := exprVal(v)
if err != nil {
return "", validation.WrapError(err)
}
bin, ok := n.(*ast.BinaryNode)
if !ok {
return "", validation.NewGenericError("invalid operator")
}
if _, ok := bin.Left.(*ast.IdentifierNode); !ok {
return "", validation.NewGenericError("invalid operator")
}
if _, ok := bin.Right.(*ast.IdentifierNode); !ok {
return "", validation.NewGenericError("invalid operator")
}
return bin.Operator, nil
}
func UnmarshalExprStringMap(v interface{}) (map[string]string, error) {
m, ok := v.(map[string]any)
if !ok {
return nil, validation.NewGenericError("must be a map")
}
res := make(map[string]string, len(m))
for k, v := range m {
str, err := UnmarshalExprStringExpression(v)
if err != nil {
return nil, MapValueError{Key: k, Err: err}
}
res[k] = str
}
return res, nil
}
func MarshalExprStringMap(v map[string]string) graphql.Marshaler {
return graphql.MarshalAny(v)
}
|