forked from GoogleCloudPlatform/k8s-config-connector
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request GoogleCloudPlatform#2146 from yuwenma/cbwp-unified…
…-mapper-2 chore: use auto-generated mapper in cbwp
- Loading branch information
Showing
4 changed files
with
228 additions
and
215 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
// Copyright 2024 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package cloudbuild | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"strings" | ||
"time" | ||
|
||
"google.golang.org/protobuf/reflect/protoreflect" | ||
"google.golang.org/protobuf/types/known/timestamppb" | ||
) | ||
|
||
type MapContext struct { | ||
errs []error | ||
} | ||
|
||
func (c *MapContext) Errorf(msg string, args ...interface{}) { | ||
c.errs = append(c.errs, fmt.Errorf(msg, args...)) | ||
} | ||
|
||
func (c *MapContext) Err() error { | ||
return errors.Join(c.errs...) | ||
} | ||
|
||
type ProtoEnum interface { | ||
~int32 | ||
Descriptor() protoreflect.EnumDescriptor | ||
} | ||
|
||
func Enum_ToProto[U ProtoEnum](mapCtx *MapContext, in *string) U { | ||
var defaultU U | ||
descriptor := defaultU.Descriptor() | ||
|
||
inValue := ValueOf(in) | ||
if inValue == "" { | ||
unspecifiedValue := U(0) | ||
return unspecifiedValue | ||
} | ||
|
||
n := descriptor.Values().Len() | ||
for i := 0; i < n; i++ { | ||
value := descriptor.Values().Get(i) | ||
if string(value.Name()) == inValue { | ||
v := U(value.Number()) | ||
return v | ||
} | ||
} | ||
|
||
var validValues []string | ||
for i := 0; i < n; i++ { | ||
value := descriptor.Values().Get(i) | ||
validValues = append(validValues, string(value.Name())) | ||
} | ||
|
||
mapCtx.Errorf("unknown enum value %q for %v (valid values are %v)", inValue, descriptor.FullName(), strings.Join(validValues, ", ")) | ||
return 0 | ||
} | ||
|
||
func Enum_FromProto[U ProtoEnum](mapCtx *MapContext, v U) *string { | ||
descriptor := v.Descriptor() | ||
|
||
if v == 0 { | ||
return nil | ||
} | ||
|
||
val := descriptor.Values().ByNumber(protoreflect.EnumNumber(v)) | ||
if val == nil { | ||
mapCtx.Errorf("unknown enum value %d", v) | ||
return nil | ||
} | ||
s := string(val.Name()) | ||
return &s | ||
} | ||
|
||
func LazyPtr[V comparable](v V) *V { | ||
var defaultV V | ||
if v == defaultV { | ||
return nil | ||
} | ||
return &v | ||
} | ||
|
||
func ToOpenAPIDateTime(ts *timestamppb.Timestamp) *string { | ||
formatted := ts.AsTime().Format(time.RFC3339) | ||
return &formatted | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.