github.com/kotovmak/go-admin@v1.1.1/plugins/admin/modules/form/form.go (about) 1 package form 2 3 import ( 4 "errors" 5 ) 6 7 const ( 8 PostTypeKey = "__go_admin_post_type" 9 PostResultKey = "__go_admin_post_result" 10 PostIsSingleUpdateKey = "__go_admin_is_single_update" 11 12 PreviousKey = "__go_admin_previous_" 13 TokenKey = "__go_admin_t_" 14 MethodKey = "__go_admin_method_" 15 16 NoAnimationKey = "__go_admin_no_animation_" 17 ) 18 19 // Values maps a string key to a list of values. 20 // It is typically used for query parameters and form values. 21 // Unlike in the http.Header map, the keys in a Values map 22 // are case-sensitive. 23 type Values map[string][]string 24 25 // Get gets the first value associated with the given key. 26 // If there are no values associated with the key, Get returns 27 // the empty string. To access multiple values, use the map 28 // directly. 29 func (f Values) Get(key string) string { 30 if len(f[key]) > 0 { 31 return f[key][0] 32 } 33 return "" 34 } 35 36 // Add adds the value to key. It appends to any existing 37 // values associated with key. 38 func (f Values) Add(key string, value string) { 39 f[key] = []string{value} 40 } 41 42 // IsEmpty check the key is empty or not. 43 func (f Values) IsEmpty(key ...string) bool { 44 for _, k := range key { 45 if f.Get(k) == "" { 46 return true 47 } 48 } 49 return false 50 } 51 52 // Has check the key exists or not. 53 func (f Values) Has(key ...string) bool { 54 for _, k := range key { 55 if f.Get(k) != "" { 56 return true 57 } 58 } 59 return false 60 } 61 62 // Delete deletes the values associated with key. 63 func (f Values) Delete(key string) { 64 delete(f, key) 65 } 66 67 // ToMap turn the values to a map[string]string type. 68 func (f Values) ToMap() map[string]string { 69 var m = make(map[string]string) 70 for key, v := range f { 71 if len(v) > 0 { 72 m[key] = v[0] 73 } 74 } 75 return m 76 } 77 78 // IsUpdatePost check the param if is from an update post request type or not. 79 func (f Values) IsUpdatePost() bool { 80 return f.Get(PostTypeKey) == "0" 81 } 82 83 // IsInsertPost check the param if is from an insert post request type or not. 84 func (f Values) IsInsertPost() bool { 85 return f.Get(PostTypeKey) == "1" 86 } 87 88 // PostError get the post result. 89 func (f Values) PostError() error { 90 msg := f.Get(PostResultKey) 91 if msg == "" { 92 return nil 93 } 94 return errors.New(msg) 95 } 96 97 // IsSingleUpdatePost check the param if from an single update post request type or not. 98 func (f Values) IsSingleUpdatePost() bool { 99 return f.Get(PostIsSingleUpdateKey) == "1" 100 } 101 102 // RemoveRemark removes the PostType and IsSingleUpdate flag parameters. 103 func (f Values) RemoveRemark() Values { 104 f.Delete(PostTypeKey) 105 f.Delete(PostIsSingleUpdateKey) 106 return f 107 } 108 109 // RemoveSysRemark removes all framework post flag parameters. 110 func (f Values) RemoveSysRemark() Values { 111 f.Delete(PostTypeKey) 112 f.Delete(PostIsSingleUpdateKey) 113 f.Delete(PostResultKey) 114 f.Delete(PreviousKey) 115 f.Delete(TokenKey) 116 f.Delete(MethodKey) 117 f.Delete(NoAnimationKey) 118 return f 119 }