|
| 1 | +package nodeconfigutils |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + "google.golang.org/protobuf/proto" |
| 7 | + "google.golang.org/protobuf/reflect/protoreflect" |
| 8 | +) |
| 9 | + |
| 10 | +// PopulateAllFields recursively populates all fields in a protobuf message with non-zero test values. |
| 11 | +// This is useful for testing to ensure all fields can be marshaled/unmarshaled correctly. |
| 12 | +func PopulateAllFields(msg proto.Message) { |
| 13 | + populateMessage(msg.ProtoReflect(), 0) |
| 14 | +} |
| 15 | + |
| 16 | +func populateMessage(msg protoreflect.Message, depth int) { |
| 17 | + // Prevent infinite recursion for deeply nested structures |
| 18 | + if depth > 10 { |
| 19 | + return |
| 20 | + } |
| 21 | + |
| 22 | + // Iterate over all field descriptors (including unset ones) |
| 23 | + fields := msg.Descriptor().Fields() |
| 24 | + for i := 0; i < fields.Len(); i++ { |
| 25 | + fd := fields.Get(i) |
| 26 | + setFieldValue(msg, fd, depth) |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +func setFieldValue(msg protoreflect.Message, fd protoreflect.FieldDescriptor, depth int) { |
| 31 | + if fd.IsList() { |
| 32 | + // Handle repeated fields - add 2 elements |
| 33 | + list := msg.Mutable(fd).List() |
| 34 | + for j := 0; j < 2; j++ { |
| 35 | + if fd.Kind() == protoreflect.MessageKind { |
| 36 | + // For repeated message fields, create new message and populate it |
| 37 | + elem := list.NewElement() |
| 38 | + populateMessage(elem.Message(), depth+1) |
| 39 | + list.Append(elem) |
| 40 | + } else { |
| 41 | + val := getDefaultValueForField(fd, fmt.Sprintf("item%d", j), depth) |
| 42 | + list.Append(val) |
| 43 | + } |
| 44 | + } |
| 45 | + } else if fd.IsMap() { |
| 46 | + // Handle map fields - add 2 entries |
| 47 | + mapVal := msg.Mutable(fd).Map() |
| 48 | + for j := 0; j < 2; j++ { |
| 49 | + key := getDefaultKeyForKind(fd.MapKey().Kind(), j) |
| 50 | + if fd.MapValue().Kind() == protoreflect.MessageKind { |
| 51 | + // For map values that are messages, create and populate |
| 52 | + val := mapVal.NewValue() |
| 53 | + populateMessage(val.Message(), depth+1) |
| 54 | + mapVal.Set(key, val) |
| 55 | + } else { |
| 56 | + val := getDefaultValueForMapValue(fd.MapValue(), j, depth) |
| 57 | + mapVal.Set(key, val) |
| 58 | + } |
| 59 | + } |
| 60 | + } else if fd.Kind() == protoreflect.MessageKind { |
| 61 | + // Handle singular message fields - use Mutable to get/create the message |
| 62 | + nestedMsg := msg.Mutable(fd).Message() |
| 63 | + populateMessage(nestedMsg, depth+1) |
| 64 | + } else { |
| 65 | + // Handle singular primitive fields |
| 66 | + val := getDefaultValueForField(fd, "", depth) |
| 67 | + msg.Set(fd, val) |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +func getDefaultValueForField(fd protoreflect.FieldDescriptor, suffix string, depth int) protoreflect.Value { |
| 72 | + switch fd.Kind() { |
| 73 | + case protoreflect.BoolKind: |
| 74 | + return protoreflect.ValueOfBool(true) |
| 75 | + case protoreflect.Int32Kind, protoreflect.Sint32Kind, protoreflect.Sfixed32Kind: |
| 76 | + return protoreflect.ValueOfInt32(42) |
| 77 | + case protoreflect.Int64Kind, protoreflect.Sint64Kind, protoreflect.Sfixed64Kind: |
| 78 | + return protoreflect.ValueOfInt64(42) |
| 79 | + case protoreflect.Uint32Kind, protoreflect.Fixed32Kind: |
| 80 | + return protoreflect.ValueOfUint32(42) |
| 81 | + case protoreflect.Uint64Kind, protoreflect.Fixed64Kind: |
| 82 | + return protoreflect.ValueOfUint64(42) |
| 83 | + case protoreflect.FloatKind: |
| 84 | + return protoreflect.ValueOfFloat32(42.0) |
| 85 | + case protoreflect.DoubleKind: |
| 86 | + return protoreflect.ValueOfFloat64(42.0) |
| 87 | + case protoreflect.StringKind: |
| 88 | + fieldName := string(fd.Name()) |
| 89 | + if suffix != "" { |
| 90 | + return protoreflect.ValueOfString(fmt.Sprintf("test-%s-%s", fieldName, suffix)) |
| 91 | + } |
| 92 | + return protoreflect.ValueOfString(fmt.Sprintf("test-%s-value", fieldName)) |
| 93 | + case protoreflect.BytesKind: |
| 94 | + return protoreflect.ValueOfBytes([]byte(fmt.Sprintf("test-bytes-%s", fd.Name()))) |
| 95 | + case protoreflect.EnumKind: |
| 96 | + // Use the last enum value (latest/most recent) |
| 97 | + enumDesc := fd.Enum() |
| 98 | + lastIndex := enumDesc.Values().Len() - 1 |
| 99 | + if lastIndex >= 0 { |
| 100 | + return protoreflect.ValueOfEnum(enumDesc.Values().Get(lastIndex).Number()) |
| 101 | + } |
| 102 | + return protoreflect.ValueOfEnum(0) |
| 103 | + case protoreflect.MessageKind: |
| 104 | + // Message fields should be handled in setFieldValue using Mutable |
| 105 | + // This shouldn't be called for message fields |
| 106 | + return protoreflect.Value{} |
| 107 | + default: |
| 108 | + return protoreflect.Value{} |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +func getDefaultKeyForKind(kind protoreflect.Kind, index int) protoreflect.MapKey { |
| 113 | + switch kind { |
| 114 | + case protoreflect.StringKind: |
| 115 | + return protoreflect.ValueOfString(fmt.Sprintf("test-key-%d", index)).MapKey() |
| 116 | + case protoreflect.Int32Kind: |
| 117 | + return protoreflect.ValueOfInt32(int32(index + 1)).MapKey() |
| 118 | + case protoreflect.Int64Kind: |
| 119 | + return protoreflect.ValueOfInt64(int64(index + 1)).MapKey() |
| 120 | + case protoreflect.Uint32Kind: |
| 121 | + return protoreflect.ValueOfUint32(uint32(index + 1)).MapKey() |
| 122 | + case protoreflect.Uint64Kind: |
| 123 | + return protoreflect.ValueOfUint64(uint64(index + 1)).MapKey() |
| 124 | + case protoreflect.BoolKind: |
| 125 | + return protoreflect.ValueOfBool(index == 0).MapKey() |
| 126 | + default: |
| 127 | + return protoreflect.ValueOfString(fmt.Sprintf("key-%d", index)).MapKey() |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +func getDefaultValueForMapValue(fd protoreflect.FieldDescriptor, index int, depth int) protoreflect.Value { |
| 132 | + suffix := fmt.Sprintf("map%d", index) |
| 133 | + return getDefaultValueForField(fd, suffix, depth) |
| 134 | +} |
0 commit comments