Skip to content
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

Implement custom Json Marshaller for AssessmentReport struct #2205

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion yb-voyager/cmd/assessMigrationCommand.go
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ func createMigrationAssessmentCompletedEvent() *cp.MigrationAssessmentCompletedE
}

assessmentIssues := flattenAssessmentReportToAssessmentIssues(assessmentReport)

// assessmentReport.SerializationMode = "full"
payload := AssessMigrationPayload{
PayloadVersion: ASSESS_MIGRATION_PAYLOAD_VERSION,
VoyagerVersion: assessmentReport.VoyagerVersion,
Expand Down Expand Up @@ -1609,6 +1609,7 @@ func generateAssessmentReportJson(reportDir string) error {
}
log.Infof("migration complexity explanation: %q", assessmentReport.MigrationComplexityExplanation)

// assessmentReport.SerializationMode = "minimal"
strReport, err := json.MarshalIndent(assessmentReport, "", "\t")
if err != nil {
return fmt.Errorf("failed to marshal the assessment report: %w", err)
Expand Down
37 changes: 36 additions & 1 deletion yb-voyager/cmd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -1050,6 +1050,7 @@ func storeTableListInMSR(tableList []sqlname.NameTuple) error {
// =====================================================================

// TODO: consider merging all unsupported field with single AssessmentReport struct member as AssessmentIssue
// NOTE: If you add or remove any field, consider updating MarshalJSON() method if required
type AssessmentReport struct {
VoyagerVersion string `json:"VoyagerVersion"`
TargetDBVersion *ybversion.YBVersion `json:"TargetDBVersion"`
Expand All @@ -1060,7 +1061,7 @@ type AssessmentReport struct {
Issues []AssessmentIssue `json:"-"` // disabled in reports till corresponding UI changes are done(json and html reports)
TableIndexStats *[]migassessment.TableIndexStats `json:"TableIndexStats"`
Notes []string `json:"Notes"`

SerializationMode string `json:"-"` // field to control serialization of json
// fields going to be deprecated
UnsupportedDataTypes []utils.TableColumnsDataTypes `json:"UnsupportedDataTypes"`
UnsupportedDataTypesDesc string `json:"UnsupportedDataTypesDesc"`
Expand Down Expand Up @@ -1191,6 +1192,40 @@ func ParseJSONToAssessmentReport(reportPath string) (*AssessmentReport, error) {
return &report, nil
}

// Implementing json.Marshaler interface by defining MarshalJSON() to have customised serialization
// NOTE: Ensure that AssessmentReport is passed as value in json.Marshal() to invoke custom MarshalJSON logic.
func (ar AssessmentReport) MarshalJSON() ([]byte, error) {
switch ar.SerializationMode {
case "full", "":
type Alias AssessmentReport
return json.Marshal(Alias(ar))
case "minimal":
return json.Marshal(struct {
VoyagerVersion string `json:"VoyagerVersion"`
TargetDBVersion *ybversion.YBVersion `json:"TargetDBVersion"`
MigrationComplexity string `json:"MigrationComplexity"`
MigrationComplexityExplanation string `json:"MigrationComplexityExplanation"`
SchemaSummary utils.SchemaSummary `json:"SchemaSummary"`
Sizing *migassessment.SizingAssessmentReport `json:"Sizing"`
Issues []AssessmentIssue `json:"AssessmentIssues"`
TableIndexStats *[]migassessment.TableIndexStats `json:"TableIndexStats"`
Notes []string `json:"Notes"`
}{
VoyagerVersion: ar.VoyagerVersion,
TargetDBVersion: ar.TargetDBVersion,
MigrationComplexity: ar.MigrationComplexity,
MigrationComplexityExplanation: ar.MigrationComplexityExplanation,
SchemaSummary: ar.SchemaSummary,
Sizing: ar.Sizing,
Issues: ar.Issues,
TableIndexStats: ar.TableIndexStats,
Notes: ar.Notes,
})
default:
return nil, fmt.Errorf("unsupported serialization mode: %s", ar.SerializationMode)
}
}

func (ar *AssessmentReport) AppendIssues(issues ...AssessmentIssue) {
ar.Issues = append(ar.Issues, issues...)
}
Expand Down
1 change: 1 addition & 0 deletions yb-voyager/cmd/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ func TestAssessmentReportStructs(t *testing.T) {
Issues []AssessmentIssue `json:"-"`
TableIndexStats *[]migassessment.TableIndexStats `json:"TableIndexStats"`
Notes []string `json:"Notes"`
SerializationMode string `json:"-"`
UnsupportedDataTypes []utils.TableColumnsDataTypes `json:"UnsupportedDataTypes"`
UnsupportedDataTypesDesc string `json:"UnsupportedDataTypesDesc"`
UnsupportedFeatures []UnsupportedFeature `json:"UnsupportedFeatures"`
Expand Down