github.com/GuanceCloud/cliutils@v1.1.21/pipeline/ptinput/funcs/utils.go (about) 1 // Unless explicitly stated otherwise all files in this repository are licensed 2 // under the MIT License. 3 // This product includes software developed at Guance Cloud (https://www.guance.com/). 4 // Copyright 2021-present Guance, Inc. 5 6 package funcs 7 8 import ( 9 "fmt" 10 "math" 11 "strconv" 12 "strings" 13 "time" 14 15 "github.com/GuanceCloud/cliutils/pipeline/ptinput" 16 "github.com/GuanceCloud/platypus/pkg/ast" 17 conv "github.com/spf13/cast" 18 ) 19 20 func getKeyName(node *ast.Node) (string, error) { 21 var key string 22 23 switch node.NodeType { //nolint:exhaustive 24 case ast.TypeIdentifier: 25 key = node.Identifier().Name 26 case ast.TypeAttrExpr: 27 key = node.AttrExpr().String() 28 case ast.TypeStringLiteral: 29 key = node.StringLiteral().Val 30 default: 31 return "", fmt.Errorf("expect StringLiteral or Identifier or AttrExpr, got %s", 32 node.NodeType) 33 } 34 return key, nil 35 } 36 37 func isPlVarbOrFunc(node *ast.Node) bool { 38 if node == nil { 39 return false 40 } 41 switch node.NodeType { //nolint:exhaustive 42 case ast.TypeIdentifier, ast.TypeAttrExpr, ast.TypeCallExpr: 43 return true 44 default: 45 return false 46 } 47 } 48 49 const ( 50 InvalidInt = math.MinInt32 51 DefaultInt = int64(0xdeadbeef) 52 DefaultStr = "" 53 InvalidStr = "deadbeaf" 54 InvalidFloat = math.SmallestNonzeroFloat64 55 DefaultFloat = float64(0.0) 56 ) 57 58 func fixYear(now time.Time, x int64) (int, error) { 59 if x == DefaultInt { 60 return now.Year(), nil 61 } 62 63 if x < 0 { 64 return -1, fmt.Errorf("year should larger than 0") 65 } 66 67 // year like 02 -> 2002, 21 -> 2021 68 if x < int64(100) { //nolint:gomnd 69 x += int64(now.Year() / 100 * 100) //nolint:gomnd 70 return int(x), nil 71 } 72 73 return int(x), nil 74 } 75 76 func fixMonth(now time.Time, x string) (time.Month, error) { 77 if x == DefaultStr { 78 return now.Month(), nil 79 } else { 80 if v, err := strconv.ParseInt(x, 10, 64); err == nil { // digital month: i.e., 01, 11 81 if v < 1 || v > 12 { 82 return time.Month(-1), fmt.Errorf("month should between [1,12], got %x,", x) 83 } 84 return time.Month(v), nil 85 } else { // month like aug/august, january/jan 86 v, ok := monthMaps[strings.ToLower(x)] 87 if !ok { 88 return InvalidInt, fmt.Errorf("unknown month %s", x) 89 } 90 return v, nil 91 } 92 } 93 } 94 95 func fixDay(now time.Time, x int64) (int, error) { 96 if x == DefaultInt { 97 return now.Day(), nil 98 } 99 100 if x < 1 || x > 31 { 101 return -1, fmt.Errorf("month day should between [1,31], got %d", x) 102 } 103 104 return int(x), nil 105 } 106 107 func fixHour(now time.Time, x int64) (int, error) { 108 if x == DefaultInt { 109 return now.Hour(), nil 110 } 111 112 if x < 0 || x > 23 { 113 return -1, fmt.Errorf("hour should between [0,24], got %d", x) 114 } 115 116 return int(x), nil 117 } 118 119 func fixMinute(now time.Time, x int64) (int, error) { 120 if x == DefaultInt { 121 return now.Minute(), nil 122 } 123 124 if x < 0 || x > 59 { 125 return -1, fmt.Errorf("minute should between [0,59], got %d", x) 126 } 127 128 return int(x), nil 129 } 130 131 func fixSecond(x int64) (int, error) { 132 if x == DefaultInt { 133 return 0, nil 134 } 135 136 if x < 0 || x > 59 { 137 return -1, fmt.Errorf("second should between [0,59], got %d", x) 138 } 139 140 return int(x), nil 141 } 142 143 func tz(s string) (z *time.Location, err error) { 144 z, err = time.LoadLocation(s) 145 if err != nil { 146 if _, ok := timezoneList[s]; !ok { 147 return nil, fmt.Errorf("unknown timezone %s", s) 148 } 149 150 z, err = time.LoadLocation(timezoneList[s]) 151 if err != nil { 152 return nil, err 153 } 154 } 155 156 return z, nil 157 } 158 159 func doCast(result interface{}, tInfo string) (interface{}, ast.DType) { 160 switch strings.ToLower(tInfo) { 161 case "bool": 162 return conv.ToBool(result), ast.Bool 163 164 case "int": 165 return conv.ToInt64(conv.ToFloat64(result)), ast.Int 166 167 case "float": 168 return conv.ToFloat64(result), ast.Float 169 170 case "str": 171 return conv.ToString(result), ast.String 172 } 173 174 return nil, ast.Nil 175 } 176 177 func getPoint(in any) (ptinput.PlInputPt, error) { 178 if in == nil { 179 return nil, fmt.Errorf("nil ptr: input") 180 } 181 182 pt, ok := in.(ptinput.PlInputPt) 183 184 if !ok { 185 return nil, fmt.Errorf("typeof input is not Point") 186 } 187 188 if pt == nil { 189 return nil, fmt.Errorf("nil ptr: input") 190 } 191 return pt, nil 192 } 193 194 func getPtKey(in any, key string) (any, ast.DType, error) { 195 pt, err := getPoint(in) 196 if err != nil { 197 return nil, ast.Invalid, err 198 } 199 200 if key == "_" { 201 key = ptinput.Originkey 202 } 203 return pt.Get(key) 204 } 205 206 func deletePtKey(in any, key string) { 207 pt, err := getPoint(in) 208 if err != nil { 209 return 210 } 211 212 if key == "_" { 213 key = ptinput.Originkey 214 } 215 216 pt.Delete(key) 217 } 218 219 func pointTime(in any) int64 { 220 pt, ok := in.(ptinput.PlInputPt) 221 if !ok { 222 return time.Now().UnixNano() 223 } 224 t := pt.PtTime() 225 if t.IsZero() { 226 return time.Now().UnixNano() 227 } else { 228 return t.UnixNano() 229 } 230 } 231 232 func addKey2PtWithVal(in any, key string, value any, dtype ast.DType, kind ptinput.KeyKind) bool { 233 pt, err := getPoint(in) 234 if err != nil { 235 return false 236 } 237 238 if key == "_" { 239 key = ptinput.Originkey 240 } 241 242 switch kind { //nolint:exhaustive 243 case ptinput.KindPtTag: 244 return pt.SetTag(key, value, dtype) 245 default: 246 return pt.Set(key, value, dtype) 247 } 248 } 249 250 func renamePtKey(in any, to, from string) error { 251 if to == "_" { 252 to = ptinput.Originkey 253 } 254 255 if from == "_" { 256 from = ptinput.Originkey 257 } 258 259 if to == from { 260 return nil 261 } 262 263 if in == nil { 264 return fmt.Errorf("nil ptr: input") 265 } 266 267 pt, err := getPoint(in) 268 if err != nil { 269 return err 270 } 271 272 return pt.RenameKey(from, to) 273 } 274 275 func setPtName(in any, val string) error { 276 pt, err := getPoint(in) 277 if err != nil { 278 return err 279 } 280 pt.SetPtName(val) 281 return nil 282 } 283 284 func getPtName(in any) string { 285 pt, err := getPoint(in) 286 if err != nil { 287 return "" 288 } 289 return pt.GetPtName() 290 } 291 292 func markPtDrop(in any) error { 293 pt, err := getPoint(in) 294 if err != nil { 295 return err 296 } 297 298 pt.MarkDrop(true) 299 300 return nil 301 }