-
Notifications
You must be signed in to change notification settings - Fork 26
fix(deps): bump cmf-sdk-go v0.0.5 to v0.0.7 and handle breaking changes #3333
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
7c6f756
0b22a62
571b26d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,14 @@ | ||
| package flink | ||
|
|
||
| import ( | ||
| "fmt" | ||
|
|
||
| "github.com/spf13/cobra" | ||
|
|
||
| cmfsdk "github.com/confluentinc/cmf-sdk-go/v1" | ||
|
|
||
| "github.com/confluentinc/cli/v4/pkg/config" | ||
| "github.com/confluentinc/cli/v4/pkg/flink" | ||
| ) | ||
|
|
||
| type computePoolOut struct { | ||
|
|
@@ -77,11 +80,16 @@ func convertSdkComputePoolToLocalComputePool(sdkComputePool cmfsdk.ComputePool) | |
| }, | ||
| } | ||
|
|
||
| if sdkComputePool.Status != nil { | ||
| if phase := extractComputePoolPhase(sdkComputePool); phase != "" { | ||
| localPool.Status = &LocalComputePoolStatus{ | ||
| Phase: sdkComputePool.Status.Phase, | ||
| Phase: phase, | ||
| } | ||
| } | ||
|
|
||
| return localPool | ||
| } | ||
|
|
||
| func extractComputePoolPhase(pool cmfsdk.ComputePool) string { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are we expecting similar We have an ongoing initiative to automate the CP CMF CLI source code generation, and your team doesn't need to spend time on the standard CRUDL feature development anymore, so we'd like to have the custom functions as few as possible. Can we explore ways to make this function more generic so that we can try to generate it whenever we have a need to extract certain key from a map?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed, it's realistically the only practical way to surface controller-driven values, and patterns like these will pop up from time to time. Generalized to |
||
| phase, _ := flink.GetMapField[string](pool.GetStatus(), "phase", fmt.Sprintf("compute pool %q", pool.GetMetadata().Name)) | ||
| return phase | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| package flink | ||
|
|
||
| import "github.com/confluentinc/cli/v4/pkg/log" | ||
|
|
||
| // GetMapField extracts a typed value of type T from an untyped map. | ||
| // cmf-sdk-go represents K8s/FKO-style fields (status, spec, metadata, defaults) | ||
| // as map[string]any since their schema is controller-driven and not statically | ||
| // known. This is the single safe entry point for reading those fields. | ||
| // | ||
| // Returns (zero, false) if the key is absent, nil, or not of type T. A type | ||
| // mismatch is logged at debug level — that's a server/schema contract | ||
| // violation, not a normal absence. | ||
| // | ||
| // For nested maps, call repeatedly: | ||
| // | ||
| // jobStatus, ok := GetMapField[map[string]any](status, "jobStatus", label) | ||
| // if ok { | ||
| // state, _ := GetMapField[string](jobStatus, "state", label) | ||
| // } | ||
| // | ||
| // contextLabel should identify the resource for actionable debug logs | ||
| // (e.g. `compute pool "foo"`). | ||
| func GetMapField[T any](m map[string]any, key, contextLabel string) (T, bool) { | ||
| var zero T | ||
| raw, ok := m[key] | ||
| if !ok || raw == nil { | ||
| return zero, false | ||
| } | ||
| v, ok := raw.(T) | ||
| if !ok { | ||
| log.CliLogger.Debugf("%s: %s has unexpected type %T, expected %T", contextLabel, key, raw, zero) | ||
| return zero, false | ||
| } | ||
| return v, true | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| package flink | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestGetMapField_String(t *testing.T) { | ||
|
Check warning on line 9 in pkg/flink/cmf_map_test.go
|
||
| tests := []struct { | ||
| name string | ||
| m map[string]any | ||
| want string | ||
| wantOk bool | ||
| }{ | ||
| {name: "nil map", m: nil, want: "", wantOk: false}, | ||
| {name: "empty map", m: map[string]any{}, want: "", wantOk: false}, | ||
| {name: "key missing", m: map[string]any{"other": "x"}, want: "", wantOk: false}, | ||
| {name: "value nil", m: map[string]any{"phase": nil}, want: "", wantOk: false}, | ||
| {name: "string value", m: map[string]any{"phase": "RUNNING"}, want: "RUNNING", wantOk: true}, | ||
| {name: "empty string", m: map[string]any{"phase": ""}, want: "", wantOk: true}, | ||
| {name: "wrong type int", m: map[string]any{"phase": 42}, want: "", wantOk: false}, | ||
| {name: "wrong type bool", m: map[string]any{"phase": true}, want: "", wantOk: false}, | ||
| } | ||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| got, ok := GetMapField[string](tt.m, "phase", "test") | ||
| require.Equal(t, tt.wantOk, ok) | ||
| require.Equal(t, tt.want, got) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestGetMapField_Int64(t *testing.T) { | ||
|
Check warning on line 34 in pkg/flink/cmf_map_test.go
|
||
| got, ok := GetMapField[int64](map[string]any{"observedGeneration": int64(7)}, "observedGeneration", "test") | ||
| require.True(t, ok) | ||
| require.Equal(t, int64(7), got) | ||
|
|
||
| // JSON-decoded numbers commonly arrive as float64 in untyped maps; document the | ||
| // (sharp-edged) implication: callers extracting numerics from JSON-sourced maps | ||
| // must request the type the decoder actually produced (float64), not int64. | ||
| _, ok = GetMapField[int64](map[string]any{"observedGeneration": float64(7)}, "observedGeneration", "test") | ||
| require.False(t, ok) | ||
| } | ||
|
|
||
| func TestGetMapField_NestedMap(t *testing.T) { | ||
|
Check warning on line 46 in pkg/flink/cmf_map_test.go
|
||
| nested := map[string]any{"jobName": "foo", "state": "RUNNING"} | ||
| root := map[string]any{"jobStatus": nested} | ||
|
|
||
| got, ok := GetMapField[map[string]any](root, "jobStatus", "test") | ||
| require.True(t, ok) | ||
| require.Equal(t, nested, got) | ||
|
|
||
| // Compose for nested traversal. | ||
| state, ok := GetMapField[string](got, "state", "test") | ||
| require.True(t, ok) | ||
| require.Equal(t, "RUNNING", state) | ||
| } | ||
|
|
||
| func TestGetMapField_Slice(t *testing.T) { | ||
|
Check warning on line 60 in pkg/flink/cmf_map_test.go
|
||
| conditions := []any{"Ready", "Healthy"} | ||
| got, ok := GetMapField[[]any](map[string]any{"conditions": conditions}, "conditions", "test") | ||
| require.True(t, ok) | ||
| require.Equal(t, conditions, got) | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.