reader.go 2.1 KB
Newer Older
Y
Your Name 已提交
1 2 3 4 5 6
package interpreter

import (
	"fmt"
	"net/url"
	"reflect"
L
Liujian 已提交
7
	"strconv"
Y
Your Name 已提交
8 9
)

Y
Your Name 已提交
10
//Reader reder
Y
Your Name 已提交
11
type Reader interface {
Y
Your Name 已提交
12
	Read(variables *Variables) string
Y
Your Name 已提交
13 14 15 16 17 18 19 20 21 22 23 24
}

type _NotReader string

func (r _NotReader) Read(variables *Variables) string {
	return string(r)
}

type _OrgReader struct {
}

func (r *_OrgReader) Read(variables *Variables) string {
Y
Your Name 已提交
25
	return string(variables.Org)
Y
Your Name 已提交
26 27 28 29
}

type _BodyReader struct {
	Index int
Y
Your Name 已提交
30 31
	Path  []string
	Name  string
Y
Your Name 已提交
32 33
}

Y
Your Name 已提交
34
func (r *_BodyReader) Read(variables *Variables) string {
Y
Your Name 已提交
35

Y
Your Name 已提交
36
	if len(variables.Bodes) <= r.Index {
Y
Your Name 已提交
37 38
		return ""
	}
Y
Your Name 已提交
39 40 41 42
	if r.Index == 0 {
		body := variables.Bodes[r.Index]
		form, ok := body.(url.Values)
		if ok {
Y
Your Name 已提交
43 44 45 46
			return form.Get(r.Name)
		}
	}

Y
Your Name 已提交
47 48
	root := reflect.ValueOf(variables.Bodes[r.Index])
	return find(&root, r.Path)
Y
Your Name 已提交
49
}
Y
Your Name 已提交
50
func find(node *reflect.Value, path []string) string {
Y
Your Name 已提交
51

Y
Your Name 已提交
52
	if len(path) == 0 {
L
Liujian 已提交
53 54
		if !node.IsValid(){
			return ""
L
Liujian 已提交
55
		}
L
Liujian 已提交
56 57 58 59
		if node.IsNil(){
			return ""
		}
		return fmt.Sprint(node.Interface())
Y
Your Name 已提交
60 61
	}

Y
Your Name 已提交
62
	k := node.Kind()
Y
Your Name 已提交
63 64 65 66 67

	switch k {

	case reflect.Interface:

Y
Your Name 已提交
68 69
		next := node.Elem()
		return find(&next, path)
Y
Your Name 已提交
70 71
	case reflect.Map:
		{
Y
Your Name 已提交
72 73 74
			key := reflect.ValueOf(path[0])
			next := node.MapIndex(key)
			return find(&next, path[1:])
Y
Your Name 已提交
75
		}
L
Liujian 已提交
76 77 78 79 80 81 82 83 84
	case reflect.Slice:
		{
			index,err:= strconv.Atoi(path[0])
			if err!= nil{
				return ""
			}
			next :=node.Index(index)
			return find(&next,path[1:])
		}
Y
Your Name 已提交
85 86 87 88 89 90 91 92 93
	default:
		return ""
	}

	return ""
}

type _HeaderReader struct {
	Index int
Y
Your Name 已提交
94
	Key   string
Y
Your Name 已提交
95 96 97
}

func (r *_HeaderReader) Read(variables *Variables) string {
Y
Your Name 已提交
98
	if len(variables.Headers) <= r.Index {
Y
Your Name 已提交
99 100 101 102 103 104 105 106 107 108 109 110 111
		return ""
	}

	return variables.Headers[r.Index].Get(r.Key)
}

type _RestFulReader struct {
	Key string
}

func (r *_RestFulReader) Read(variables *Variables) string {
	return variables.Restful[r.Key]
}
Y
Your Name 已提交
112

Y
Your Name 已提交
113 114 115 116 117 118 119 120 121 122
type _QueryReader struct {
	Key string
}

func (r *_QueryReader) Read(variables *Variables) string {
	return variables.Query.Get(r.Key)
}

type _CookieReader struct {
	Index int
Y
Your Name 已提交
123
	Name  string
Y
Your Name 已提交
124 125 126
}

func (r *_CookieReader) Read(variables *Variables) string {
Y
Your Name 已提交
127
	if len(variables.Cookies) <= r.Index {
Y
Your Name 已提交
128 129
		return ""
	}
Y
Your Name 已提交
130 131
	for _, c := range variables.Cookies[r.Index] {
		if c.Name == r.Name {
Y
Your Name 已提交
132 133
			return c.Value
		}
Y
Your Name 已提交
134 135
	}
	return ""
Y
Your Name 已提交
136
}