github.com/lmorg/murex@v0.0.0-20240217211045-e081c89cd4ef/lang/parameters/get.go (about) 1 package parameters 2 3 import ( 4 "errors" 5 "strconv" 6 "strings" 7 8 "github.com/lmorg/murex/lang/types" 9 ) 10 11 var errTooFew = errors.New("too few parameters") 12 13 // Byte gets a single parameter as a []byte slice 14 func (p *Parameters) Byte(pos int) ([]byte, error) { 15 p.mutex.RLock() 16 17 if p.Len() <= pos { 18 p.mutex.RUnlock() 19 return []byte{}, errTooFew 20 } 21 b := []byte(p.params[pos]) 22 p.mutex.RUnlock() 23 return b, nil 24 } 25 26 // ByteAll returns all parameters as one space-delimited []byte slice 27 func (p *Parameters) ByteAll() []byte { 28 p.mutex.RLock() 29 b := []byte(strings.Join(p.params, " ")) 30 p.mutex.RUnlock() 31 return b 32 } 33 34 // ByteAllRange returns all parameters within range as one space-delimited []byte slice 35 // `start` is first point in array. `end` is last. Set `end` to `-1` if you want `[n:]`. 36 func (p *Parameters) ByteAllRange(start, end int) []byte { 37 var b []byte 38 p.mutex.RLock() 39 40 /*if end == -1 { 41 b = []byte(strings.Join(p.params[start:], " ")) 42 }*/ 43 b = []byte(strings.Join(p.params[start:end], " ")) 44 45 p.mutex.RUnlock() 46 47 return b 48 } 49 50 // RuneArray gets all parameters as a [][]rune slice {parameter, characters} 51 func (p *Parameters) RuneArray() [][]rune { 52 p.mutex.RLock() 53 54 r := make([][]rune, len(p.params)) 55 for i := range p.params { 56 r[i] = []rune(p.params[i]) 57 } 58 p.mutex.RUnlock() 59 return r 60 } 61 62 // String gets a single parameter as string 63 func (p *Parameters) String(pos int) (string, error) { 64 p.mutex.RLock() 65 66 if p.Len() <= pos { 67 p.mutex.RUnlock() 68 return "", errTooFew 69 } 70 71 s := p.params[pos] 72 p.mutex.RUnlock() 73 return s, nil 74 } 75 76 // StringArray returns all parameters as a slice of strings 77 func (p *Parameters) StringArray() []string { 78 p.mutex.RLock() 79 80 params := make([]string, len(p.params)) 81 copy(params, p.params) 82 83 p.mutex.RUnlock() 84 85 return params 86 } 87 88 // StringAll returns all parameters as one space-delimited string 89 func (p *Parameters) StringAll() string { 90 p.mutex.RLock() 91 defer p.mutex.RUnlock() 92 93 return strings.Join(p.params, " ") 94 } 95 96 // StringAllRange returns all parameters within range as one space-delimited string. 97 // `start` is first point in array. `end` is last. Set `end` to `-1` if you want `[n:]`. 98 func (p *Parameters) StringAllRange(start, end int) string { 99 p.mutex.RLock() 100 101 var s string 102 103 switch { 104 case len(p.params) == 0: 105 s = "" 106 case end == -1: 107 s = strings.Join(p.params[start:], " ") 108 default: 109 s = strings.Join(p.params[start:end], " ") 110 } 111 112 p.mutex.RUnlock() 113 return s 114 } 115 116 // Int gets parameter as integer 117 func (p *Parameters) Int(pos int) (int, error) { 118 p.mutex.RLock() 119 defer p.mutex.RUnlock() 120 121 if p.Len() <= pos { 122 return 0, errTooFew 123 } 124 return strconv.Atoi(p.params[pos]) 125 } 126 127 // Uint32 gets parameter as Uint32 128 func (p *Parameters) Uint32(pos int) (uint32, error) { 129 p.mutex.RLock() 130 defer p.mutex.RUnlock() 131 132 if p.Len() <= pos { 133 return 0, errTooFew 134 } 135 i, err := strconv.ParseUint(p.params[pos], 10, 32) 136 return uint32(i), err 137 } 138 139 // Bool gets parameter as boolean 140 func (p *Parameters) Bool(pos int) (bool, error) { 141 p.mutex.RLock() 142 defer p.mutex.RUnlock() 143 144 if p.Len() <= pos { 145 return false, errTooFew 146 } 147 return types.IsTrue([]byte(p.params[pos]), 0), nil 148 } 149 150 // Block get parameter as a code block or JSON block 151 func (p *Parameters) Block(pos int) ([]rune, error) { 152 p.mutex.RLock() 153 defer p.mutex.RUnlock() 154 155 switch { 156 case p.Len() <= pos: 157 return []rune{}, errTooFew 158 159 case len(p.params[pos]) < 2: 160 return []rune{}, errors.New("not a valid code block. Too few characters. Code blocks should be surrounded by curly brace, eg `{ code }`") 161 162 case p.params[pos][0] != '{': 163 return []rune{}, errors.New("not a valid code block. Missing opening curly brace. Found `" + string([]byte{p.params[pos][0]}) + "` instead.") 164 165 case p.params[pos][len(p.params[pos])-1] != '}': 166 return []rune{}, errors.New("not a valid code block. Missing closing curly brace. Found `" + string([]byte{p.params[pos][len(p.params[pos])-1]}) + "` instead.") 167 168 } 169 170 return []rune(p.params[pos][1 : len(p.params[pos])-1]), nil 171 } 172 173 // Len returns the number of parameters 174 func (p *Parameters) Len() int { 175 p.mutex.RLock() 176 defer p.mutex.RUnlock() 177 178 return len(p.params) 179 }