|
| 1 | +/* |
| 2 | +Copyright 2025 The Kubernetes Authors. |
| 3 | +
|
| 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 |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 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. |
| 15 | +*/ |
| 16 | + |
| 17 | +package rules |
| 18 | + |
| 19 | +import ( |
| 20 | + "reflect" |
| 21 | + |
| 22 | + "k8s.io/gengo/v2/types" |
| 23 | +) |
| 24 | + |
| 25 | +// StreamingListTypeFieldOrder implements APIRule interface. |
| 26 | +// Fields must be ordered TypeMeta, ListMeta, Items |
| 27 | +type StreamingListTypeFieldOrder struct{} |
| 28 | + |
| 29 | +func (l *StreamingListTypeFieldOrder) Name() string { |
| 30 | + return "streaming_list_type_field_order" |
| 31 | +} |
| 32 | +func (l *StreamingListTypeFieldOrder) Validate(t *types.Type) ([]string, error) { |
| 33 | + if !isListType(t) { |
| 34 | + return nil, nil |
| 35 | + } |
| 36 | + var fields []string |
| 37 | + if t.Members[0].Name != "TypeMeta" { |
| 38 | + fields = append(fields, "TypeMeta") |
| 39 | + } |
| 40 | + if t.Members[1].Name != "ListMeta" { |
| 41 | + fields = append(fields, "ListMeta") |
| 42 | + } |
| 43 | + if t.Members[2].Name != "Items" { |
| 44 | + fields = append(fields, "Items") |
| 45 | + } |
| 46 | + return fields, nil |
| 47 | +} |
| 48 | + |
| 49 | +// StreamingListTypeJSONTags implements APIRule interface. |
| 50 | +// Fields must be JSON-tagged |
| 51 | +type StreamingListTypeJSONTags struct{} |
| 52 | + |
| 53 | +func (l *StreamingListTypeJSONTags) Name() string { |
| 54 | + return "streaming_list_type_json_tags" |
| 55 | +} |
| 56 | + |
| 57 | +func (l *StreamingListTypeJSONTags) Validate(t *types.Type) ([]string, error) { |
| 58 | + if !isListType(t) { |
| 59 | + return nil, nil |
| 60 | + } |
| 61 | + var fields []string |
| 62 | + for _, m := range t.Members { |
| 63 | + switch m.Name { |
| 64 | + case "TypeMeta": |
| 65 | + if reflect.StructTag(m.Tags).Get("json") != ",inline" { |
| 66 | + fields = append(fields, "TypeMeta") |
| 67 | + } |
| 68 | + case "ListMeta": |
| 69 | + if reflect.StructTag(m.Tags).Get("json") != "metadata,omitempty" { |
| 70 | + fields = append(fields, "ListMeta") |
| 71 | + } |
| 72 | + case "Items": |
| 73 | + if reflect.StructTag(m.Tags).Get("json") != "items" { |
| 74 | + fields = append(fields, "Items") |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + return fields, nil |
| 79 | +} |
| 80 | + |
| 81 | +// StreamingListTypeProtoTags implements APIRule interface. |
| 82 | +// Fields must be Proto-tagged with specific tags for streaming to work. |
| 83 | +type StreamingListTypeProtoTags struct{} |
| 84 | + |
| 85 | +func (l *StreamingListTypeProtoTags) Name() string { |
| 86 | + return "streaming_list_type_proto_tags" |
| 87 | +} |
| 88 | +func (l *StreamingListTypeProtoTags) Validate(t *types.Type) ([]string, error) { |
| 89 | + if !isListType(t) { |
| 90 | + return nil, nil |
| 91 | + } |
| 92 | + var fields []string |
| 93 | + for _, m := range t.Members { |
| 94 | + switch m.Name { |
| 95 | + case "TypeMeta": |
| 96 | + if v := reflect.StructTag(m.Tags).Get("protobuf"); v != "" { |
| 97 | + fields = append(fields, "TypeMeta") |
| 98 | + } |
| 99 | + case "ListMeta": |
| 100 | + if v := reflect.StructTag(m.Tags).Get("protobuf"); v != "" && v != "bytes,1,opt,name=metadata" { |
| 101 | + fields = append(fields, "ListMeta") |
| 102 | + } |
| 103 | + case "Items": |
| 104 | + if v := reflect.StructTag(m.Tags).Get("protobuf"); v != "" && v != "bytes,2,rep,name=items" { |
| 105 | + fields = append(fields, "Items") |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + return fields, nil |
| 110 | +} |
| 111 | + |
| 112 | +func isListType(t *types.Type) bool { |
| 113 | + return len(t.Members) == 3 && |
| 114 | + hasNamedMember(t, "TypeMeta") && |
| 115 | + hasNamedMember(t, "ListMeta") && |
| 116 | + hasNamedMember(t, "Items") |
| 117 | +} |
0 commit comments