Blame view

json_utility.go 1.59 KB
64041a2ea   Marko Tikvić   first commit
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
  package restutility
  
  import (
  	"net/http"
  	"strings"
  )
  
  type LangMap map[string]map[string]string
  
  type Field struct {
  	Parameter  string `json:"param"`
  	Type       string `json:"type"`
  	Visible    bool   `json:"visible"`
  	Editable   bool   `json:"editable"`
  }
  
  type JSONParams struct {
  	Lang    LangMap
  	Fields  []Field
  	IdField string
  	Correlations []CorrelationField `json:"correlation_fields"`
  }
  
  type JSONPayload struct {
  	Method	     string             `json:"method"`
  	Params	     map[string]string  `json:"params"`
  	Lang	     LangMap            `json:"lang"`
  	Fields	     []Field            `json:"fields"`
  	Correlations []CorrelationField `json:"correlation_fields"`
  	IdField      string             `json:"idField"`
  	// Data can only hold slices of any type. It can't be used for itteration
  	Data	     interface{}        `json:"data"`
  }
  
  func NewJSONParams(lang LangMap,
  	fields []Field,
  	id string,
  	correlations []CorrelationField) JSONParams {
  
  	var jp JSONParams
  
  	jp.Lang = lang
  	jp.Fields = fields
  	jp.IdField = id
  	jp.Correlations = correlations
  
  	return jp
  }
  
  func NewJSONPayload(r *http.Request, params JSONParams) JSONPayload {
  	var obj JSONPayload
  	obj.Method = strings.ToLower(r.Method + " " + r.URL.Path)
  	obj.Params = make(map[string]string, 0)
  	obj.Lang = make(map[string]map[string]string, 0)
  	obj.Fields = make([]Field, 0)
  	obj.IdField = params.IdField
  	obj.Correlations = params.Correlations
  
  	for k, m := range params.Lang {
  		obj.Lang[k] = m
  	}
  	for _, f := range params.Fields {
  		obj.Fields = append(obj.Fields, f)
  	}
  
  	return obj
  }
  
  func decodeRequestBody(req *http.Request, model interface{}) {}