Skip to content

Commit

Permalink
chore: generate mappers for bigtable
Browse files Browse the repository at this point in the history
Some more work in the controllerbuilder for oneof and optional enums.
  • Loading branch information
justinsb committed Jul 25, 2024
1 parent 57832be commit 35cf679
Show file tree
Hide file tree
Showing 8 changed files with 1,648 additions and 16 deletions.
17 changes: 17 additions & 0 deletions dev/tools/controllerbuilder/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Examples:


```
go run . generate-types --proto-source-path ../proto-to-mapper/build/googleapis.pb \
--service google.bigtable.admin.v2 --version v1beta1 \
--output-api ~/kcc/k8s-config-connector
--kinds BigtableInstance
go run . generate-mapper --api-go-package-path github.com/GoogleCloudPlatform/k8s-config-connector/apis \
--output-dir ~/kcc/k8s-config-connector/pkg/controller/direct/ \
--proto-source-path ../proto-to-mapper/build/googleapis.pb \
--service google.bigtable.admin.v2 \
--api-dir ~/kcc/k8s-config-connector/apis/
```
42 changes: 40 additions & 2 deletions dev/tools/controllerbuilder/pkg/codegen/mappergenerator.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,12 @@ func (v *MapperGenerator) GenerateMappers() error {
pbPackage := pair.ProtoGoPackage
krmPackage := pair.KRMType.GoPackage

// TODO: Can we figure out a better way here?
switch pbPackage {
case "cloud.google.com/go/bigtable/admin/apiv2/adminpb":
pbPackage = "google.golang.org/genproto/googleapis/bigtable/admin/v2"
}

out.contents.WriteString(fmt.Sprintf("package %s\n\n", lastGoComponent(goPackage)))
out.contents.WriteString("import (\n")
out.contents.WriteString(fmt.Sprintf("\tpb %q\n", pbPackage))
Expand Down Expand Up @@ -314,10 +320,15 @@ func (v *MapperGenerator) writeMapFunctionsForPair(out io.Writer, pair *typePair
)
case protoreflect.EnumKind:
functionName := "direct.Enum_FromProto"
// Not needed if we use the accessor:
// protoTypeName := "pb." + protoNameForEnum(protoField.Enum())
// if protoIsPointerInGo(protoField) {
// functionName = "EnumPtr_FromProto[" + protoTypeName + "]"
// }
fmt.Fprintf(out, "\tout.%s = %s(mapCtx, in.%s)\n",
krmFieldName,
functionName,
krmFieldName,
protoAccessor,
)
case protoreflect.StringKind,
protoreflect.FloatKind,
Expand Down Expand Up @@ -457,7 +468,6 @@ func (v *MapperGenerator) writeMapFunctionsForPair(out io.Writer, pair *typePair
functionName := krmTypeName + "_ToProto"
switch krmTypeName {
case "string":
// functionName = "String_" + string(protoField.Message().Name()) + "_ToProto"
functionName = string(msg.Name()) + "_" + krmFieldName + "_ToProto"
}

Expand Down Expand Up @@ -487,6 +497,28 @@ func (v *MapperGenerator) writeMapFunctionsForPair(out io.Writer, pair *typePair
case protoreflect.EnumKind:
protoTypeName := "pb." + protoNameForEnum(protoField.Enum())
functionName := "direct.Enum_ToProto"
if protoIsPointerInGo(protoField) {
functionName = "EnumPtr_ToProto[" + protoTypeName + "]"
}

oneof := protoField.ContainingOneof()
if oneof != nil {
// These are very rare and irregular; just require a custom method
functionName := fmt.Sprintf("%s_%s_ToProto", goTypeName, protoFieldName)

fmt.Fprintf(out, "\tif oneof := %s(mapCtx, in.%s); oneof != nil {\n",
functionName,
krmFieldName,
)

oneofFieldName := ToGoFieldName(oneof.Name())

fmt.Fprintf(out, "\t\tout.%s = oneof\n",
oneofFieldName)
fmt.Fprintf(out, "\t}\n")
continue
}

fmt.Fprintf(out, "\tout.%s = %s[%s](mapCtx, in.%s)\n",
protoFieldName,
functionName,
Expand Down Expand Up @@ -584,6 +616,12 @@ func protoNameForOneOf(field protoreflect.FieldDescriptor) string {
name += "_"
}
}
if field.Enum() != nil {
elemTypeName := protoNameForEnum(field.Enum())
if name == elemTypeName {
name += "_"
}
}
return name
}

Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ require (
golang.org/x/sync v0.7.0
golang.org/x/time v0.5.0
google.golang.org/api v0.189.0
google.golang.org/genproto v0.0.0-20240722135656-d784300faade
google.golang.org/genproto/googleapis/api v0.0.0-20240701130421-f6361c86f094
google.golang.org/genproto/googleapis/rpc v0.0.0-20240722135656-d784300faade
google.golang.org/grpc v1.64.1
google.golang.org/protobuf v1.34.2
gopkg.in/dnaeon/go-vcr.v3 v3.2.0
Expand Down Expand Up @@ -213,8 +215,6 @@ require (
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d // indirect
gomodules.xyz/jsonpatch/v2 v2.3.0 // indirect
google.golang.org/appengine v1.6.8 // indirect
google.golang.org/genproto v0.0.0-20240722135656-d784300faade // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240722135656-d784300faade // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
Loading

0 comments on commit 35cf679

Please sign in to comment.