|
| 1 | +package core |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "reflect" |
| 6 | + |
| 7 | + "github.com/docker/infrakit/pkg/types" |
| 8 | + "github.com/twmb/algoimpl/go/graph" |
| 9 | +) |
| 10 | + |
| 11 | +// converts a map to a Spec, nil if it cannot be done |
| 12 | +func mapToSpec(m map[string]interface{}) *types.Spec { |
| 13 | + // This is hacky -- generate a string representation |
| 14 | + // and try to parse it as struct |
| 15 | + buff, err := json.Marshal(m) |
| 16 | + if err != nil { |
| 17 | + return nil |
| 18 | + } |
| 19 | + s := types.Spec{} |
| 20 | + err = json.Unmarshal(buff, &s) |
| 21 | + if err != nil { |
| 22 | + return nil |
| 23 | + } |
| 24 | + if s.Validate() == nil { |
| 25 | + return &s |
| 26 | + } |
| 27 | + return nil |
| 28 | +} |
| 29 | + |
| 30 | +// findSpecs parses the bytes and returns a Spec, if the Spec can be parsed |
| 31 | +// from the buffer. Some fields are verified and must be present for the |
| 32 | +// buffer to be considered a representation of a Spec. |
| 33 | +func findSpecs(v interface{}) []*types.Spec { |
| 34 | + |
| 35 | + result := []*types.Spec{} |
| 36 | + |
| 37 | + switch v := v.(type) { |
| 38 | + |
| 39 | + case []*types.Spec: |
| 40 | + for _, x := range v { |
| 41 | + c := *x |
| 42 | + result = append(result, findSpecs(&c)...) |
| 43 | + } |
| 44 | + |
| 45 | + case []types.Spec: |
| 46 | + for _, x := range v { |
| 47 | + c := x |
| 48 | + result = append(result, findSpecs(&c)...) |
| 49 | + } |
| 50 | + |
| 51 | + case []interface{}: |
| 52 | + for _, x := range v { |
| 53 | + c := x |
| 54 | + result = append(result, findSpecs(c)...) |
| 55 | + } |
| 56 | + |
| 57 | + case map[string]interface{}: |
| 58 | + // convert to Spec? |
| 59 | + result = append(result, findSpecs(mapToSpec(v))...) |
| 60 | + |
| 61 | + case *types.Any: |
| 62 | + |
| 63 | + if v == nil { |
| 64 | + return result |
| 65 | + } |
| 66 | + |
| 67 | + spec := types.Spec{} |
| 68 | + if err := v.Decode(&spec); err == nil { |
| 69 | + |
| 70 | + if spec.Validate() == nil { |
| 71 | + result = append(result, findSpecs(&spec)...) |
| 72 | + return result |
| 73 | + } |
| 74 | + |
| 75 | + } |
| 76 | + |
| 77 | + // now decode as regular struct - map or []interface{} |
| 78 | + var vv interface{} |
| 79 | + if err := v.Decode(&vv); err != nil { |
| 80 | + return nil |
| 81 | + } |
| 82 | + |
| 83 | + switch vv := vv.(type) { |
| 84 | + case []interface{}: |
| 85 | + for _, x := range vv { |
| 86 | + result = append(result, findSpecs(x)...) |
| 87 | + } |
| 88 | + case map[string]interface{}: |
| 89 | + for _, x := range vv { |
| 90 | + result = append(result, findSpecs(x)...) |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + case types.Spec: |
| 95 | + c := v |
| 96 | + result = append(result, &c) |
| 97 | + result = append(result, findSpecs(c.Properties)...) |
| 98 | + |
| 99 | + case *types.Spec: |
| 100 | + |
| 101 | + if v == nil { |
| 102 | + return result |
| 103 | + } |
| 104 | + |
| 105 | + c := *v |
| 106 | + result = append(result, &c) |
| 107 | + result = append(result, findSpecs(c.Properties)...) |
| 108 | + |
| 109 | + default: |
| 110 | + value := reflect.Indirect(reflect.ValueOf(v)) |
| 111 | + if value.Type().Kind() == reflect.Struct { |
| 112 | + for i := 0; i < value.NumField(); i++ { |
| 113 | + fv := value.Field(i) |
| 114 | + if fv.IsValid() { |
| 115 | + result = append(result, findSpecs(fv.Interface())...) |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + return result |
| 121 | +} |
| 122 | + |
| 123 | +// nested recurses through the Properties of the spec and returns any nested specs. |
| 124 | +func nested(s *types.Spec) []*types.Spec { |
| 125 | + if s.Properties == nil { |
| 126 | + return nil |
| 127 | + } |
| 128 | + return findSpecs(s.Properties) |
| 129 | +} |
| 130 | + |
| 131 | +type key struct { |
| 132 | + class string |
| 133 | + name string |
| 134 | +} |
| 135 | + |
| 136 | +func indexSpecs(specs []*types.Spec, g *graph.Graph) map[key]*graph.Node { |
| 137 | + index := map[key]*graph.Node{} |
| 138 | + for _, spec := range specs { |
| 139 | + |
| 140 | + node := g.MakeNode() |
| 141 | + *(node.Value) = spec |
| 142 | + |
| 143 | + index[key{class: spec.Class, name: spec.Metadata.Name}] = &node |
| 144 | + } |
| 145 | + return index |
| 146 | +} |
| 147 | + |
| 148 | +func indexGet(index map[key]*graph.Node, class, name string) *graph.Node { |
| 149 | + if v, has := index[key{class: class, name: name}]; has { |
| 150 | + return v |
| 151 | + } |
| 152 | + return nil |
| 153 | +} |
| 154 | + |
| 155 | +// AllSpecs returns a list of all the specs given plus any nested specs |
| 156 | +func AllSpecs(specs []*types.Spec) []*types.Spec { |
| 157 | + all := []*types.Spec{} |
| 158 | + for _, s := range specs { |
| 159 | + all = append(all, s) |
| 160 | + all = append(all, nested(s)...) |
| 161 | + } |
| 162 | + return all |
| 163 | +} |
| 164 | + |
| 165 | +// OrderByDependency returns the given specs in dependency order. The input is assume to be exhaustive in that |
| 166 | +// all nested specs are part of the list. |
| 167 | +func OrderByDependency(specs []*types.Spec) ([]*types.Spec, error) { |
| 168 | + |
| 169 | + g := graph.New(graph.Directed) |
| 170 | + if g == nil { |
| 171 | + return nil, nil |
| 172 | + } |
| 173 | + |
| 174 | + index := indexSpecs(specs, g) |
| 175 | + |
| 176 | + for _, spec := range specs { |
| 177 | + |
| 178 | + from := indexGet(index, spec.Class, spec.Metadata.Name) |
| 179 | + if from == nil { |
| 180 | + return nil, errNotFound{class: spec.Class, name: spec.Metadata.Name} |
| 181 | + } |
| 182 | + |
| 183 | + for _, depend := range spec.Depends { |
| 184 | + |
| 185 | + to := indexGet(index, depend.Class, depend.Name) |
| 186 | + if to == nil { |
| 187 | + return nil, errBadDependency(depend) |
| 188 | + } |
| 189 | + |
| 190 | + if from == to { |
| 191 | + |
| 192 | + a := (*from.Value).(*types.Spec) |
| 193 | + b := (*to.Value).(*types.Spec) |
| 194 | + return nil, errCircularDependency([]*types.Spec{a, b}) |
| 195 | + } |
| 196 | + |
| 197 | + if err := g.MakeEdge(*to, *from); err != nil { |
| 198 | + return nil, err |
| 199 | + } |
| 200 | + } |
| 201 | + } |
| 202 | + |
| 203 | + // cycle detection |
| 204 | + for _, connected := range g.StronglyConnectedComponents() { |
| 205 | + if len(connected) > 1 { |
| 206 | + |
| 207 | + cycle := []*types.Spec{} |
| 208 | + for _, n := range connected { |
| 209 | + cycle = append(cycle, (*n.Value).(*types.Spec)) |
| 210 | + } |
| 211 | + return nil, errCircularDependency(cycle) |
| 212 | + } |
| 213 | + } |
| 214 | + |
| 215 | + ordered := []*types.Spec{} |
| 216 | + for _, n := range g.TopologicalSort() { |
| 217 | + ordered = append(ordered, (*n.Value).(*types.Spec)) |
| 218 | + } |
| 219 | + |
| 220 | + return ordered, nil |
| 221 | +} |
0 commit comments