-
Notifications
You must be signed in to change notification settings - Fork 1
/
xp_string.go
45 lines (36 loc) · 1006 Bytes
/
xp_string.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
package xparam
import (
"fmt"
"strings"
)
//------------------------------------------------------------
// Xparam access for string
//------------------------------------------------------------
// Gets parameter as string (trimmed).
func (xp XP) As_String(key string) (str string) {
if val, ok := xp[key]; ok && val != nil {
str = strings.TrimSpace(fmt.Sprint(val))
}
return
}
// Gets parameter as string pointer allowing for the nil option.
func (xp XP) As_StringNil(key string) (str *string) {
if val, ok := xp[key]; ok && val != nil {
s := strings.TrimSpace(fmt.Sprint(val))
str = &s
}
return
}
// Gets parameter as email address string (trimmed & lower cased).
func (xp XP) As_StringEmail(key string) string {
return strings.ToLower(xp.As_String(key))
}
// Sets parameter as string.
func (xp XP) To_String(to *map[string]string, key string) {
if val, ok := xp[key]; ok && val != nil {
str := strings.TrimSpace(fmt.Sprint(val))
if str != "" {
(*to)[key] = str
}
}
}