Skip to content

Commit

Permalink
Merge pull request GoogleCloudPlatform#2285 from gemmahou/gen-mapper
Browse files Browse the repository at this point in the history
chore: Handle list of enum during mapper generation
  • Loading branch information
google-oss-prow[bot] authored Jul 15, 2024
2 parents 9de4abf + 79b1c88 commit e8c8b0d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
1 change: 0 additions & 1 deletion dev/tools/proto-to-mapper/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ generate-pb: install-protoc-linux
./third_party/googleapis/google/monitoring/v3/*.proto \
./third_party/googleapis/google/monitoring/dashboard/v1/*.proto \
./third_party/googleapis/google/devtools/cloudbuild/*/*.proto \
./third_party/googleapis/google/cloud/dataform/*/*.proto \
-o build/googleapis.pb

install-protoc-linux:
Expand Down
35 changes: 29 additions & 6 deletions dev/tools/proto-to-mapper/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ func (v *visitor) writeMapFunctionsForPair(out io.Writer, pair *typePair) {
}

if protoField.Cardinality() == protoreflect.Repeated {
useSliceFromProto := ""
useSliceFromProtoFunction := ""
useCustomMethod := false

switch protoField.Kind() {
Expand All @@ -390,12 +390,22 @@ func (v *visitor) writeMapFunctionsForPair(out io.Writer, pair *typePair) {
krmElemTypeName = strings.TrimPrefix(krmElemTypeName, "[]")

functionName := krmElemTypeName + "_FromProto"
useSliceFromProto = functionName
useSliceFromProtoFunction = functionName
case protoreflect.StringKind:
if krmField.Type != "[]string" {
useCustomMethod = true
// useSliceFromProto = fmt.Sprintf("%s_%s_FromProto", goTypeName, protoFieldName)
// useSliceFromProtoFunction = fmt.Sprintf("%s_%s_FromProto", goTypeName, protoFieldName)
}
case protoreflect.EnumKind:
krmElemTypeName := krmField.Type
krmElemTypeName = strings.TrimPrefix(krmElemTypeName, "*")
krmElemTypeName = strings.TrimPrefix(krmElemTypeName, "[]")

functionName := "Enum_FromProto"
useSliceFromProtoFunction = fmt.Sprintf("%s(mapCtx, in.%s)",
functionName,
krmFieldName,
)

case
protoreflect.FloatKind,
Expand All @@ -406,16 +416,16 @@ func (v *visitor) writeMapFunctionsForPair(out io.Writer, pair *typePair) {
protoreflect.Uint32Kind,
protoreflect.Uint64Kind,
protoreflect.BytesKind:
useSliceFromProto = ""
useSliceFromProtoFunction = ""
default:
klog.Fatalf("unhandled kind %q for repeated field %v", protoField.Kind(), protoField)
}

if useSliceFromProto != "" {
if useSliceFromProtoFunction != "" {
fmt.Fprintf(out, "\tout.%s = Slice_FromProto(mapCtx, in.%s, %s)\n",
krmFieldName,
krmFieldName,
useSliceFromProto,
useSliceFromProtoFunction,
)
} else if useCustomMethod {
methodName := fmt.Sprintf("%s_%s_FromProto", goTypeName, protoFieldName)
Expand Down Expand Up @@ -514,6 +524,19 @@ func (v *visitor) writeMapFunctionsForPair(out io.Writer, pair *typePair) {
//useSliceToProtoFunction = fmt.Sprintf("%s_%s_ToProto", goTypeName, protoFieldName)
}

case protoreflect.EnumKind:
krmElemTypeName := krmField.Type
krmElemTypeName = strings.TrimPrefix(krmElemTypeName, "*")
krmElemTypeName = strings.TrimPrefix(krmElemTypeName, "[]")

protoTypeName := "pb." + protoNameForEnum(protoField.Enum())
functionName := "Enum_ToProto"
useSliceToProtoFunction = fmt.Sprintf("%s[%s](mapCtx, in.%s)",
functionName,
protoTypeName,
krmFieldName,
)

case protoreflect.FloatKind,
protoreflect.DoubleKind,
protoreflect.BoolKind,
Expand Down

0 comments on commit e8c8b0d

Please sign in to comment.