summaryrefslogtreecommitdiff
path: root/populatePackagePB.go
blob: 78eb6a3aebbefda900d6b22fae01ab5fc48b21f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package debian

import (
	"google.golang.org/protobuf/proto"
	"google.golang.org/protobuf/reflect/protoreflect"

	"go.wit.com/lib/config"
)

func SetString(pb proto.Message, varname string, varvalue string) (bool, error) {
	msg := pb.ProtoReflect() // This is the entry point to the reflection API.

	descriptor := msg.Descriptor() // Get the message's descriptor, which contains metadata about its fields.

	fieldName := protoreflect.Name(varname)
	fieldDescriptor := descriptor.Fields().ByName(fieldName)

	if fieldDescriptor == nil {
		return false, config.ErrProtoNoVarName
	}

	if fieldDescriptor.Kind() != protoreflect.StringKind {
		// The field exists but is not a string, so we can't return it as one.
		return false, config.ErrProtoVarNotString
	}

	valueToSet := protoreflect.ValueOfString(varvalue)

	// 6. If the field exists and is a string, get its value.
	// The value is returned as a protoreflect.Value.
	msg.Set(fieldDescriptor, valueToSet)

	// 7. Convert the protoreflect.Value to a native Go string.
	return true, nil
}

/*
	msg := pb.ProtoReflect() // This is the entry point to the reflection API.

	descriptor := msg.Descriptor() // Get the message's descriptor, which contains metadata about its fields.

	debInfo := protoreflect.Name("debInfo")
	debInfoDescriptor := descriptor.ByName(debInfo)
	// debInfoMsgDescriptor := debInfoDescriptor.Descriptor() // Get the message's descriptor, which contains metadata about its fields.

	if debInfoDescriptor == nil {
		panic("DEB INFO COULD NOT BE FOUND")
		// return false, config.ErrProtoNoVarName
	}

	fieldName := protoreflect.Name(varname)
	fieldDescriptor := debInfoDescriptor.Fields().ByName(fieldName)

	if fieldDescriptor == nil {
		return false, config.ErrProtoNoVarName
	}

	if fieldDescriptor.Kind() != protoreflect.StringKind {
		// The field exists but is not a string, so we can't return it as one.
		return false, config.ErrProtoVarNotString
	}

	valueToSet := protoreflect.ValueOfString(varvalue)

	// 6. If the field exists and is a string, get its value.
	// The value is returned as a protoreflect.Value.
	msg.Set(fieldDescriptor, valueToSet)

	// 7. Convert the protoreflect.Value to a native Go string.
	return true, nil
}
*/