-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutility.go
More file actions
131 lines (106 loc) · 2.84 KB
/
utility.go
File metadata and controls
131 lines (106 loc) · 2.84 KB
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
package gql
import (
"fmt"
"log"
"reflect"
"regexp"
"strings"
)
func checkRelationsMap(m *Model) {
if m.Relations == nil{
relations:=make(map[string]Relation)
m.Relations = relations
}
}
func inStringArray(selected []string,col string) bool {
for i:=0;i<len(selected);i++ {
if selected[i] == col{
return true
}
}
return false
}
func getStrutFields(u interface{},columns []string,fields *map[int]int) []interface{}{
val := reflect.ValueOf(u).Elem()
fieldsMap:=*fields
v := make([]interface{}, len(fieldsMap))
for i := 0; i < len(columns); i++ {
valueField := val.Field(fieldsMap[i])
v[i] = valueField.Addr().Interface()
}
return v
}
func mapColumnsFields(u interface{},columns []string) *map[int]int{
val := reflect.ValueOf(u).Elem()
matched := 0
fields:=make(map[int]int)
if val.NumField() < len(columns){
log.Fatal("the scanner not has all columns you selected it , your scanner must be has fields for this columns:",columns)
}
for i := 0; i < val.NumField(); i++ {
if InColumns(val.Type().Field(i).Name,val.Type().Field(i).Tag.Get("db"),columns){
fields[matched] = i
matched++
}
}
if len(columns) > matched{
log.Fatal("the scanner not has all columns you selected it , your scanner must be has fields for this columns:",columns)
}
return &fields
}
func InColumns(field string,tag string,columns []string) bool{
for i := 0; i < len(columns); i++ {
if tag == columns[i] || strings.ToLower(field) == columns[i]{
return true
}
}
return false
}
func structFieldsValues(insertObject interface{}) (*[]string,*[]interface{}){
val := reflect.ValueOf(insertObject).Elem()
var columns []string
var values []interface{}
for i := 0; i < val.NumField(); i++ {
var column string
if val.Type().Field(i).Tag.Get("db") != ""{
column = val.Type().Field(i).Tag.Get("db")
}else{
column = strings.ToLower(val.Type().Field(i).Name)
}
field := val.Field(i).Interface()
fieldVal := reflect.ValueOf(field)
columns=append(columns,column)
values = append(values, fmt.Sprintf("%v",fieldVal))
}
return &columns,&values
}
func checkWhereCombinationMap(m *Model) {
if m.query.combinationWhere == nil{
combinationWhere:=make(map[int]int)
m.query.combinationWhere = combinationWhere
}
}
func stringHasDot(text string) bool {
result, _ := regexp.MatchString("\\.", text)
return result
}
func removeFromStrings(s *[]string, r *string) *[]string {
slice:=*s
for i, v := range slice {
if v == *r {
slice = append(slice[:i], slice[i+1:]...)
return &slice
}
}
return &slice
}
func removeUnFillable(m *Model, columns *[]string,values *[]interface{}) {
if len(m.Fillable) > 0{
for i:=0;i<len(*columns);i++ {
if inStringArray(m.Fillable,(*columns)[i]) == false{
*columns = append((*columns)[:i], (*columns)[i+1:]...)
*values = append((*values)[:i], (*values)[i+1:]...)
}
}
}
}