github.com/seeker-insurance/kit@v0.0.13/web/meta/meta.go (about) 1 package meta 2 3 import ( 4 "fmt" 5 6 "github.com/seeker-insurance/kit/web/pagination" 7 "github.com/spf13/viper" 8 ) 9 10 type ( 11 method string 12 inputType string 13 idType string 14 ActionHolder []*JsonAPIAction 15 16 Extendable interface { 17 Extend() error 18 } 19 20 JsonAPIAction struct { 21 Method string `json:"method"` 22 Name string `json:"name"` 23 URL string `json:"url"` 24 Multi bool `json:"multi"` 25 Relationship bool `json:"relationship"` 26 Fields []JsonAPIField `json:"fields"` 27 } 28 29 JsonAPIField struct { 30 Name string `json:"name"` 31 InputType string `json:"type"` 32 Value interface{} `json:"value,omitempty"` 33 Required bool `json:"required"` 34 Options []FieldOption `json:"options,omitempty"` 35 Data *pagination.Pagination `json:"data,omitempty"` 36 } 37 38 FieldOption struct { 39 Label string `json:"label,omitempty"` 40 Value interface{} `json:"value"` 41 Meta map[string]interface{} `json:"meta,omitempty"` 42 } 43 ) 44 45 const ( 46 DELETE method = "DELETE" 47 GET method = "GET" 48 POST method = "POST" 49 PATCH method = "PATCH" 50 PUT method = "PUT" 51 52 InputText inputType = "text" 53 InputPass inputType = "password" 54 InputBool inputType = "bool" 55 InputNumber inputType = "number" 56 InputSelect inputType = "select" 57 58 IdString idType = "string" 59 IdInt idType = "int" 60 ) 61 62 // AddAction ... 63 func (ah *ActionHolder) AddAction(m method, name, urlHelper string, params ...interface{}) *JsonAPIAction { 64 a := JsonAPIAction{ 65 Method: string(m), 66 Name: name, 67 URL: APIURL(fmt.Sprintf(urlHelper, params...)), 68 Fields: make([]JsonAPIField, 0), 69 } 70 *ah = append(*ah, &a) 71 return &a 72 } 73 74 // RelFields ... 75 func (a *JsonAPIAction) RelFields(typeName string) *JsonAPIAction { 76 fs := []JsonAPIField{ 77 { 78 Name: "type", 79 Value: typeName, 80 Required: true, 81 }, 82 { 83 Name: "id", 84 Required: true, 85 }, 86 } 87 a.Relationship = true 88 a.Fields = append(a.Fields, fs...) 89 return a 90 } 91 92 // Field ... 93 func (a *JsonAPIAction) Field(name string, inputType inputType, value interface{}, requred bool) *JsonAPIAction { 94 f := JsonAPIField{ 95 Name: name, 96 InputType: string(inputType), 97 Value: value, 98 Required: requred, 99 } 100 a.Fields = append(a.Fields, f) 101 return a 102 } 103 104 // FieldWithOpts add field with options to action 105 func (a *JsonAPIAction) FieldWithOpts(name string, inputType inputType, value interface{}, requred bool, options []FieldOption) *JsonAPIAction { 106 f := JsonAPIField{ 107 Name: name, 108 InputType: string(inputType), 109 Value: value, 110 Required: requred, 111 Options: options, 112 } 113 a.Fields = append(a.Fields, f) 114 return a 115 } 116 117 func FieldOptionsFromValues(values ...string) []FieldOption { 118 opts := make([]FieldOption, len(values)) 119 for i, v := range values { 120 opts[i] = FieldOption{Value: v} 121 } 122 return opts 123 } 124 125 // Pagination add pagination meta to action 126 func (a *JsonAPIAction) Pagination(data *pagination.Pagination) *JsonAPIAction { 127 f := JsonAPIField{ 128 Name: "page", 129 InputType: string(InputNumber), 130 Value: data.Page, 131 Required: false, 132 Data: data, 133 } 134 a.Fields = append(a.Fields, f) 135 return a 136 } 137 138 // APIURL full api url for the path 139 func APIURL(path string) string { 140 return fmt.Sprintf("%s%s", viper.GetString("root_url"), path) 141 }