2 Copyright 2015 The Kubernetes Authors.
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
8 http://www.apache.org/licenses/LICENSE-2.0
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
19 // FlattenMembers recursively takes any embedded members and puts them in the
20 // top level, correctly hiding them if the top level hides them. There must not
21 // be a cycle-- that implies infinite members.
23 // This is useful for e.g. computing all the valid keys in a json struct,
24 // properly considering any configuration of embedded structs.
25 func FlattenMembers(m []Member) []Member {
26 embedded := []Member{}
28 type nameInfo struct {
32 names := map[string]nameInfo{}
34 if m[i].Embedded && m[i].Type.Kind == Struct {
35 embedded = append(embedded, m[i])
37 normal = append(normal, m[i])
38 names[m[i].Name] = nameInfo{true, len(normal) - 1}
41 for i := range embedded {
42 for _, e := range FlattenMembers(embedded[i].Type.Members) {
43 if info, found := names[e.Name]; found {
47 if n := normal[info.i]; n.Name == e.Name && n.Type == e.Type {
50 panic("conflicting members")
52 normal = append(normal, e)
53 names[e.Name] = nameInfo{false, len(normal) - 1}