git.frostfs.info/TrueCloudLab/frostfs-sdk-go@v0.0.0-20241022124111-5361f0ecebd3/netmap/yml_unmarshal.go (about) 1 package netmap 2 3 import ( 4 "strings" 5 6 "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/netmap" 7 ) 8 9 type tempPlacementPolicy struct { 10 BackupFactor uint32 `yaml:"containerBackupFactor"` 11 Filters []tempFilter `yaml:"filters"` 12 Selectors []tempSelector `yaml:"selectors"` 13 Replicas []tempReplica `yaml:"replicas"` 14 Unique bool `yaml:"unique"` 15 } 16 17 type tempFilter struct { 18 Name string `yaml:"name"` 19 Key string `yaml:"key"` 20 Op string `yaml:"op"` 21 Value string `yaml:"value"` 22 Filters []tempFilter `yaml:"filters"` 23 } 24 25 type tempSelector struct { 26 Name string `yaml:"name"` 27 Count uint32 `yaml:"count"` 28 Clause string `yaml:"clause"` 29 Attribute string `yaml:"attribute"` 30 Filter string `yaml:"filter"` 31 } 32 33 type tempReplica struct { 34 Count uint32 `yaml:"count"` 35 Selector string `yaml:"selector"` 36 } 37 38 func convertNFilters(temp []tempFilter) []netmap.Filter { 39 var filters []netmap.Filter 40 for _, tf := range temp { 41 filters = append(filters, convertNFilter(tf)) 42 } 43 return filters 44 } 45 46 var stringToOperationMap = map[string]netmap.Operation{ 47 "EQ": netmap.EQ, 48 "NE": netmap.NE, 49 "GT": netmap.GT, 50 "GE": netmap.GE, 51 "LT": netmap.LT, 52 "LE": netmap.LE, 53 "OR": netmap.OR, 54 "AND": netmap.AND, 55 "NOT": netmap.NOT, 56 "LIKE": netmap.LIKE, 57 } 58 59 func convertStringToOperation(opStr string) netmap.Operation { 60 opStr = strings.ToUpper(opStr) 61 if op, exists := stringToOperationMap[opStr]; exists { 62 return op 63 } 64 return netmap.UnspecifiedOperation 65 } 66 67 func convertStringToClause(clauseStr string) netmap.Clause { 68 switch strings.ToUpper(clauseStr) { 69 case "DISTINCT": 70 return netmap.Distinct 71 default: 72 return netmap.Same 73 } 74 } 75 76 func convertNFilter(temp tempFilter) netmap.Filter { 77 filter := netmap.Filter{} 78 filter.SetKey(temp.Key) 79 filter.SetName(temp.Name) 80 filter.SetValue(temp.Value) 81 filter.SetOp(convertStringToOperation(temp.Op)) 82 83 if temp.Filters != nil { 84 filter.SetFilters(convertNFilters(temp.Filters)) 85 } 86 return filter 87 } 88 89 func (p *PlacementPolicy) UnmarshalYAML(unmarshal func(interface{}) error) error { 90 var temp tempPlacementPolicy 91 if err := unmarshal(&temp); err != nil { 92 return err 93 } 94 95 for _, ts := range temp.Filters { 96 netmapFilters := convertNFilter(ts) 97 p.AddFilters(Filter{m: netmapFilters}) 98 } 99 100 for _, ts := range temp.Selectors { 101 selector := Selector{} 102 selector.SetName(ts.Name) 103 selector.SetNumberOfNodes(ts.Count) 104 selector.SetClause(convertStringToClause(ts.Clause)) 105 selector.SelectByBucketAttribute(ts.Attribute) 106 selector.SetFilterName(ts.Filter) 107 p.AddSelectors(selector) 108 } 109 110 for _, tr := range temp.Replicas { 111 replica := ReplicaDescriptor{} 112 replica.SetSelectorName(tr.Selector) 113 replica.m.SetCount(tr.Count) 114 p.AddReplicas(replica) 115 } 116 117 p.SetContainerBackupFactor(temp.BackupFactor) 118 p.SetUnique(temp.Unique) 119 120 return nil 121 } 122 123 type Attribute struct { 124 Key string `yaml:"key"` 125 Value string `yaml:"value"` 126 } 127 128 type tempNode struct { 129 Attributes []Attribute `yaml:"attributes"` 130 } 131 132 func (x *NodeInfo) UnmarshalYAML(unmarshal func(interface{}) error) error { 133 var temp tempNode 134 if err := unmarshal(&temp); err != nil { 135 return err 136 } 137 138 for _, atr := range temp.Attributes { 139 x.SetAttribute(atr.Key, atr.Value) 140 } 141 142 return nil 143 }