github.com/GuanceCloud/cliutils@v1.1.21/pipeline/plpt/plpt.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 plpt implement pipeline point. 7 package plpt 8 9 import ( 10 "github.com/GuanceCloud/cliutils/point" 11 "github.com/GuanceCloud/platypus/pkg/ast" 12 ) 13 14 type PlPt struct { 15 kvs []*point.Field 16 kvsDelPp []int 17 originPp *point.Point 18 pooled bool 19 } 20 21 func PtWrap(pp *point.Point) *PlPt { 22 return &PlPt{ 23 originPp: pp, 24 pooled: pp.HasFlag(point.Ppooled), 25 } 26 } 27 28 func (pt *PlPt) Get(k string) (any, ast.DType) { 29 oKvs := pt.originPp.KVs() 30 for i := range pt.kvsDelPp { 31 if oKvs[i].Key == k { 32 return nil, ast.Nil 33 } 34 } 35 36 for _, kv := range pt.kvs { 37 if kv.Key != k { 38 continue 39 } 40 return getVal(kv) 41 } 42 43 for _, kv := range oKvs { 44 if kv.Key != k { 45 continue 46 } 47 return getVal(kv) 48 } 49 return nil, ast.Nil 50 } 51 52 func (pt *PlPt) Set(k string, v any, dtype ast.DType, asTag bool) { 53 oKvs := pt.originPp.KVs() 54 for i := range pt.kvsDelPp { 55 if oKvs[i].Key == k { 56 pt.kvsDelPp = append(pt.kvsDelPp[:i], pt.kvsDelPp[i+1:]...) 57 pt.kvs = append(pt.kvs, point.NewKV(k, v, point.WithKVTagSet(asTag))) 58 return 59 } 60 } 61 62 // replace 63 for i, kv := range pt.kvs { 64 if kv.Key != k { 65 continue 66 } 67 if pt.pooled { 68 if pool := point.GetPointPool(); pool != nil { 69 pool.PutKV(kv) 70 pt.kvs[i] = point.NewKV(k, v, point.WithKVTagSet(asTag)) 71 } 72 return 73 } else { 74 pt.kvs[i] = point.NewKV(k, v, point.WithKVTagSet(asTag)) 75 return 76 } 77 } 78 79 // append 80 pt.kvs = append(pt.kvs, point.NewKV(k, v, point.WithKVTagSet(asTag))) 81 } 82 83 func (pt *PlPt) Delete(k string) { 84 oKvs := pt.originPp.KVs() 85 for i := range pt.kvsDelPp { 86 if oKvs[i].Key == k { 87 return 88 } 89 } 90 91 for i, kv := range pt.kvs { 92 if kv.Key == k { 93 if pt.pooled { 94 if pool := point.GetPointPool(); pool != nil { 95 pool.PutKV(kv) 96 } 97 } 98 pt.kvs = append(pt.kvs[:i], pt.kvs[i+1:]...) 99 break 100 } 101 } 102 103 // append to kvsDel, if k in origin pt 104 for i, kv := range oKvs { 105 if kv.Key == k { 106 pt.kvsDelPp = append(pt.kvsDelPp, i) 107 break 108 } 109 } 110 } 111 112 func getVal(kv *point.Field) (any, ast.DType) { 113 switch kv.Val.(type) { 114 case *point.Field_I: 115 return kv.GetI(), ast.Int 116 case *point.Field_U: 117 return int64(kv.GetU()), ast.Int 118 case *point.Field_F: 119 return kv.GetF(), ast.Float 120 case *point.Field_B: 121 return kv.GetB(), ast.Bool 122 case *point.Field_D: 123 raw := kv.GetD() 124 r := make([]any, 0, len(raw)) 125 for _, v := range raw { 126 r = append(r, v) 127 } 128 return r, ast.List 129 case *point.Field_S: 130 return kv.GetS(), ast.String 131 132 case *point.Field_A: 133 v, err := point.AnyRaw(kv.GetA()) 134 if err != nil { 135 return nil, ast.Nil 136 } 137 switch v.(type) { 138 case []any: 139 return v, ast.List 140 case map[string]any: 141 return v, ast.Map 142 default: 143 return nil, ast.Nil 144 } 145 default: 146 return nil, ast.Nil 147 } 148 }