forked from PolarPanda611/trinity
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery_select.go
More file actions
43 lines (37 loc) · 934 Bytes
/
Copy pathquery_select.go
File metadata and controls
43 lines (37 loc) · 934 Bytes
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
package trinity
import (
"encoding/json"
"strings"
"github.com/gin-gonic/gin"
"github.com/jinzhu/gorm"
)
//QueryBySelect handling select
func QueryBySelect(c *gin.Context) func(db *gorm.DB) *gorm.DB {
return func(db *gorm.DB) *gorm.DB {
SelectByField := c.Query("SelectBy")
if SelectByField == "" {
return db
}
strings.Split(SelectByField, ",")
return db.Select(strings.Split(SelectByField, ","))
}
}
//HandlestructBySelect handling select
func HandlestructBySelect(c *gin.Context, resource interface{}) (int, interface{}, error) {
SelectByField := c.Query("SelectBy")
if SelectByField == "" {
return 200, resource, nil
}
m, _ := json.Marshal(&resource)
// decode it back to get a map
var a interface{}
json.Unmarshal(m, &a)
b := a.(map[string]interface{})
for k := range b {
if !stringInSlice(k, strings.Split(SelectByField, ",")) {
delete(b, k)
}
}
json.Marshal(b)
return 200, b, nil
}