-
Notifications
You must be signed in to change notification settings - Fork 18
Fixes unmarshalling of YAML #3116
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?
Conversation
🎉 All Contributor License Agreements have been signed. Ready to merge. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR improves YAML unmarshalling by first parsing into a generic map to preserve key casing, then converting to JSON to leverage struct JSON tags, and adds a fallback for apiVersion
if it wasn’t populated during the JSON unmarshal.
- Introduce a two-step YAML→map→JSON→struct unmarshalling flow
- Add fallback assignment for
application.ApiVersion
from raw map - (Unintended) add a debug print of the
application
struct
Comments suppressed due to low confidence (1)
internal/flink/command_application_create.go:62
- Add unit tests for the new YAML unmarshalling path, including cases where
apiVersion
is populated via the raw map fallback.
var raw map[string]interface{}
default: | ||
return errors.NewErrorWithSuggestions(fmt.Sprintf("unsupported file format: %s", ext), "Supported file formats are .json, .yaml, and .yml.") | ||
} | ||
if err != nil { | ||
return err | ||
} | ||
|
||
fmt.Print("application", application) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove this debug print or replace it with a proper logger call at the appropriate log level to avoid dumping raw structs to stdout in production.
fmt.Print("application", application) | |
log.Printf("Debug: application struct: %+v", application) |
Copilot uses AI. Check for mistakes.
// First unmarshal into a generic map to preserve the case | ||
var raw map[string]interface{} | ||
if err = yaml.Unmarshal(data, &raw); err != nil { | ||
return fmt.Errorf("failed to unmarshal YAML: %v", err) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use fmt.Errorf("failed to unmarshal YAML: %w", err)
to wrap the original error and preserve unwrapping support.
return fmt.Errorf("failed to unmarshal YAML: %v", err) | |
return fmt.Errorf("failed to unmarshal YAML: %w", err) |
Copilot uses AI. Check for mistakes.
// First unmarshal into a generic map to preserve the case | ||
var raw map[string]interface{} | ||
if err = yaml.Unmarshal(data, &raw); err != nil { | ||
return fmt.Errorf("failed to unmarshal YAML: %v", err) | ||
} | ||
|
||
// Convert to JSON bytes to use the struct's JSON tags | ||
jsonBytes, err := json.Marshal(raw) | ||
if err != nil { | ||
return fmt.Errorf("failed to convert to JSON: %v", err) | ||
} | ||
|
||
// Now unmarshal into the struct using JSON unmarshaler | ||
// This will respect the json tags in the struct | ||
if err = json.Unmarshal(jsonBytes, &application); err != nil { | ||
return fmt.Errorf("failed to unmarshal JSON: %v", err) | ||
} | ||
|
||
// Verify the apiVersion was set correctly | ||
if application.ApiVersion == "" { | ||
// If still empty, try to get it directly from the raw map | ||
if apiVer, ok := raw["apiVersion"].(string); ok { | ||
application.ApiVersion = apiVer | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] Converting YAML to JSON through an intermediate map adds overhead; consider using a YAML library or decoder that respects JSON struct tags directly to avoid double marshalling.
// First unmarshal into a generic map to preserve the case | |
var raw map[string]interface{} | |
if err = yaml.Unmarshal(data, &raw); err != nil { | |
return fmt.Errorf("failed to unmarshal YAML: %v", err) | |
} | |
// Convert to JSON bytes to use the struct's JSON tags | |
jsonBytes, err := json.Marshal(raw) | |
if err != nil { | |
return fmt.Errorf("failed to convert to JSON: %v", err) | |
} | |
// Now unmarshal into the struct using JSON unmarshaler | |
// This will respect the json tags in the struct | |
if err = json.Unmarshal(jsonBytes, &application); err != nil { | |
return fmt.Errorf("failed to unmarshal JSON: %v", err) | |
} | |
// Verify the apiVersion was set correctly | |
if application.ApiVersion == "" { | |
// If still empty, try to get it directly from the raw map | |
if apiVer, ok := raw["apiVersion"].(string); ok { | |
application.ApiVersion = apiVer | |
} | |
// Unmarshal YAML directly into the struct | |
if err = yaml.Unmarshal(data, &application); err != nil { | |
return fmt.Errorf("failed to unmarshal YAML: %v", err) | |
} | |
// Verify the apiVersion was set correctly | |
if application.ApiVersion == "" { | |
return fmt.Errorf("apiVersion is missing in the YAML file") |
Copilot uses AI. Check for mistakes.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
b7aff45
to
67c21bf
Compare
This comment has been minimized.
This comment has been minimized.
67c21bf
to
ded01e8
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, looks good.
But it looks like we have some test issues to sort out.
Release Notes
Bug Fixes
flink application create
. It fails with YAML files, because it is unable to readapiVersion
. For somereason, the YAML parser is looking forApiVersion
instead ofapiVersion
, despite the json tag available. The fix for this at the moment is to not use the YAML library but instead first convert it to JSON and then unmarshal it.apiversion
, but now it will be returned asapiVersion
Checklist
What
section below whether this PR applies to Confluent Cloud, Confluent Platform, or both.Test & Review
section below.Blast Radius
section below.What
Blast Radius
References
Test & Review
Has been manually tested for the example mentioned in the ticket
hello.yaml
And it passes