Skip to content

Commit

Permalink
Merge pull request #2354 from acpana/acpana/tmplmaputils
Browse files Browse the repository at this point in the history
enh: use maputils
  • Loading branch information
google-oss-prow[bot] authored Jul 24, 2024
2 parents 157a736 + 2453de2 commit a2600a5
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 44 deletions.
45 changes: 3 additions & 42 deletions dev/tools/controllerbuilder/template/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
krm "github.com/GoogleCloudPlatform/k8s-config-connector/apis/{{.Service}}/{{.Version}}"
refs "github.com/GoogleCloudPlatform/k8s-config-connector/apis/refs/v1beta1"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/config"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/direct"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/direct/directbase"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/direct/registry"
Expand Down Expand Up @@ -93,7 +94,7 @@ func (m *model) AdapterForObject(ctx context.Context, reader client.Reader, u *u
}
// Get ResourceID
resourceID := ValueOf(obj.Spec.ResourceID)
resourceID := direct.ValueOf(obj.Spec.ResourceID)
if resourceID == "" {
resourceID = obj.GetName()
}
Expand Down Expand Up @@ -151,7 +152,7 @@ func (a *Adapter) Find(ctx context.Context) (bool, error) {
req := &{{.Service}}pb.Get{{.Kind}}Request{Name: a.fullyQualifiedName()}
{{.KindToLower}}pb, err := a.gcpClient.Get{{.Kind}}(ctx, req)
if err != nil {
if IsNotFound(err) {
if direct.IsNotFound(err) {
return false, nil
}
return false, fmt.Errorf("getting {{.Kind}} %q: %w", a.fullyQualifiedName(), err)
Expand Down Expand Up @@ -297,46 +298,6 @@ func setStatus(u *unstructured.Unstructured, typedStatus any) error {
return nil
}
func ValueOf[T any](p *T) T {
var v T
if p != nil {
v = *p
}
return v
}
// IsNotFound returns true if the given error is an HTTP 404.
func IsNotFound(err error) bool {
return HasHTTPCode(err, 404)
}
// HasHTTPCode returns true if the given error is an HTTP response with the given code.
func HasHTTPCode(err error, code int) bool {
if err == nil {
return false
}
apiError := &apierror.APIError{}
if errors.As(err, &apiError) {
if apiError.HTTPCode() == code {
return true
}
} else {
klog.Warningf("unexpected error type %T", err)
}
return false
}
// LazyPtr returns a pointer to v, unless it is the empty value, in which case it returns nil.
// It is essentially the inverse of ValueOf, though it is lossy
// because we can't tell nil and empty apart without a pointer.
func LazyPtr[T comparable](v T) *T {
var defaultValue T
if v == defaultValue {
return nil
}
return &v
}
func ToOpenAPIDateTime(ts *timestamppb.Timestamp) *string {
formatted := ts.AsTime().Format(time.RFC3339)
return &formatted
Expand Down
4 changes: 2 additions & 2 deletions dev/tools/proto-to-mapper/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ func (v *visitor) writeMapFunctionsForPair(out io.Writer, pair *typePair) {
}

{
fmt.Fprintf(out, "func %s_FromProto(mapCtx *MapContext, in *pb.%s) *krm.%s {\n", goTypeName, pbTypeName, goTypeName)
fmt.Fprintf(out, "func %s_FromProto(mapCtx *direct.MapContext, in *pb.%s) *krm.%s {\n", goTypeName, pbTypeName, goTypeName)
fmt.Fprintf(out, "\tif in == nil {\n")
fmt.Fprintf(out, "\t\treturn nil\n")
fmt.Fprintf(out, "\t}\n")
Expand Down Expand Up @@ -658,7 +658,7 @@ func (v *visitor) writeMapFunctionsForPair(out io.Writer, pair *typePair) {
krmFieldName,
)
} else {
fmt.Fprintf(out, "\tout.%s = ValueOf(in.%s)\n",
fmt.Fprintf(out, "\tout.%s = direct.ValueOf(in.%s)\n",
protoFieldName,
krmFieldName,
)
Expand Down

0 comments on commit a2600a5

Please sign in to comment.