Commit 514fa9dd680613dec6bd8f07001f106c23c9a042

Authored by Marko Tikvić
1 parent 64041a2ea4
Exists in master and in 1 other branch v2

added formating utility

Showing 2 changed files with 59 additions and 2 deletions   Show diff stats
format_utility.go
File was created 1 package main
2
3 import (
4 "strings"
5 "time"
6 "strconv"
7 )
8
9 ////
10 //// TIME FORMAT UTILITY
11 ////
12
13 func unixToDate(input int64) time.Time {
14 return time.Unix(input, 0)
15 }
16
17 func dateToUnix(input interface{}) int64 {
18 if input != nil {
19 t := input.(time.Time)
20 return t.Unix()
21
22 }
23 return 0
24 }
25
26 func aersDate(unixString string) (string, error) {
27 unixTime, err := strconv.ParseInt(unixString, 10, 64)
28 if err != nil {
29 return "", err
30 }
31
32 date := unixToDate(unixTime).String()
33 tokens := strings.Split(date, "-")
34 dateString := tokens[0] + tokens[1] + strings.Split(tokens[2], " ")[0]
35
36 return dateString, nil
37 }
38
39 ////
40 //// STRING UTILITY
41 ////
42
43 // surrondWithSingleQuotes is used when url param is of type string
44 func putQuotes(input string) string {
45 if input != "" {
46 return " = '" + input + "'"
47 }
48 return ""
49 }
50
51 func putLikeQuotes(input string) string {
52 if input != "" {
53 return " LIKE UPPER('%" + input + "%')"
54 }
55 return ""
56 }
57
58
1 package restutility 1 package restutility
2 2
3 import ( 3 import (
4 "net/http" 4 "net/http"
5 "strings" 5 "strings"
6 ) 6 )
7 7
8 const APIVersion "/api/v1"
9
8 type LangMap map[string]map[string]string 10 type LangMap map[string]map[string]string
9 11
10 type Field struct { 12 type Field struct {
11 Parameter string `json:"param"` 13 Parameter string `json:"param"`
12 Type string `json:"type"` 14 Type string `json:"type"`
13 Visible bool `json:"visible"` 15 Visible bool `json:"visible"`
14 Editable bool `json:"editable"` 16 Editable bool `json:"editable"`
15 } 17 }
16 18
17 type JSONParams struct { 19 type JSONParams struct {
18 Lang LangMap 20 Lang LangMap
19 Fields []Field 21 Fields []Field
20 IdField string 22 IdField string
21 Correlations []CorrelationField `json:"correlation_fields"` 23 Correlations []CorrelationField `json:"correlation_fields"`
22 } 24 }
23 25
24 type JSONPayload struct { 26 type JSONPayload struct {
25 Method string `json:"method"` 27 Method string `json:"method"`
26 Params map[string]string `json:"params"` 28 Params map[string]string `json:"params"`
27 Lang LangMap `json:"lang"` 29 Lang LangMap `json:"lang"`
28 Fields []Field `json:"fields"` 30 Fields []Field `json:"fields"`
29 Correlations []CorrelationField `json:"correlation_fields"` 31 Correlations []CorrelationField `json:"correlation_fields"`
30 IdField string `json:"idField"` 32 IdField string `json:"idField"`
31 // Data can only hold slices of any type. It can't be used for itteration 33 // Data can only hold slices of any type. It can't be used for itteration
32 Data interface{} `json:"data"` 34 Data interface{} `json:"data"`
33 } 35 }
34 36
35 func NewJSONParams(lang LangMap, 37 func NewJSONParams(lang LangMap,
36 fields []Field, 38 fields []Field,
37 id string, 39 id string,
38 correlations []CorrelationField) JSONParams { 40 correlations []CorrelationField) JSONParams {
39 41
40 var jp JSONParams 42 var jp JSONParams
41 43
42 jp.Lang = lang 44 jp.Lang = lang
43 jp.Fields = fields 45 jp.Fields = fields
44 jp.IdField = id 46 jp.IdField = id
45 jp.Correlations = correlations 47 jp.Correlations = correlations
46 48
47 return jp 49 return jp
48 } 50 }
49 51
50 func NewJSONPayload(r *http.Request, params JSONParams) JSONPayload { 52 func NewJSONPayload(r *http.Request, params JSONParams) JSONPayload {
51 var obj JSONPayload 53 var obj JSONPayload
52 obj.Method = strings.ToLower(r.Method + " " + r.URL.Path) 54 obj.Method = strings.ToLower(r.Method + " " + r.URL.Path)
53 obj.Params = make(map[string]string, 0) 55 obj.Params = make(map[string]string, 0)
54 obj.Lang = make(map[string]map[string]string, 0) 56 obj.Lang = make(map[string]map[string]string, 0)
55 obj.Fields = make([]Field, 0) 57 obj.Fields = make([]Field, 0)
56 obj.IdField = params.IdField 58 obj.IdField = params.IdField
57 obj.Correlations = params.Correlations 59 obj.Correlations = params.Correlations
58 60
59 for k, m := range params.Lang { 61 for k, m := range params.Lang {
60 obj.Lang[k] = m 62 obj.Lang[k] = m
61 } 63 }
62 for _, f := range params.Fields { 64 for _, f := range params.Fields {
63 obj.Fields = append(obj.Fields, f) 65 obj.Fields = append(obj.Fields, f)
64 } 66 }
65 67
66 return obj 68 return obj
67 } 69 }
68 70
69 func decodeRequestBody(req *http.Request, model interface{}) {}