-
-
Notifications
You must be signed in to change notification settings - Fork 120
/
main.go
105 lines (88 loc) · 2.46 KB
/
main.go
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
// Copyright 2020 Saferwall. All rights reserved.
// Use of this source code is governed by Apache v2 license
// license that can be found in the LICENSE file.
package utils
import (
"fmt"
"reflect"
"strings"
"time"
"github.com/labstack/echo/v4"
)
// IsStringInSlice check if a string exist in a list of strings
func IsStringInSlice(a string, list []string) bool {
for _, b := range list {
if b == a {
return true
}
}
return false
}
// RemoveStringFromSlice removes a string item from a list of strings.
func RemoveStringFromSlice(s []string, r string) []string {
for i, v := range s {
if v == r {
return append(s[:i], s[i+1:]...)
}
}
return s
}
// GetQueryParamsFields retrieve `fields`` so we can filter them in GET/
func GetQueryParamsFields(c echo.Context) []string {
var filters []string
fields := c.QueryParam("fields")
if fields != "" {
filters = strings.Split(fields, ",")
}
return filters
}
// IsFilterAllowed check if we are allowed to filter GET with fields
func IsFilterAllowed(allowed []string, filters []string) bool {
for _, filter := range filters {
if !IsStringInSlice(filter, allowed) {
return false
}
}
return true
}
// GetStructFields retrieve json struct fields names
func GetStructFields(i interface{}) []string {
val := reflect.ValueOf(i)
var temp string
var listFields []string
for i := 0; i < val.Type().NumField(); i++ {
temp = val.Type().Field(i).Tag.Get("json")
temp = strings.Replace(temp, ",omitempty", "", -1)
listFields = append(listFields, temp)
}
return listFields
}
func fieldSet(fields ...string) map[string]bool {
set := make(map[string]bool, len(fields))
for _, s := range fields {
set[s] = true
}
return set
}
// SelectFields execlude sensitive fields
func SelectFields(i interface{}, fields ...string) map[string]interface{} {
fs := fieldSet(fields...)
rt, rv := reflect.TypeOf(i), reflect.ValueOf(i)
out := make(map[string]interface{}, rt.NumField())
for i := 0; i < rt.NumField(); i++ {
field := rt.Field(i)
jsonKey := field.Tag.Get("json")
if fs[jsonKey] {
out[jsonKey] = rv.Field(i).Interface()
}
}
return out
}
// JSONTime alias for timne.Time
type JSONTime time.Time
// MarshalJSON will marshall date
func (t JSONTime) MarshalJSON() ([]byte, error) {
//do your serializing here
stamp := fmt.Sprintf("\"%s\"", time.Time(t).Format("Mon Jan _2"))
return []byte(stamp), nil
}