Skip to content
This repository has been archived by the owner on Aug 31, 2021. It is now read-only.

Fix csbs policy Operation Def field type conversion #84

Merged
merged 1 commit into from
Nov 30, 2018
Merged
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
24 changes: 23 additions & 1 deletion openstack/csbs/v1/policies/results.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,31 @@ func (r *OperationDefinitionResp) UnmarshalJSON(b []byte) error {
RetentionDurationDays string `json:"retention_duration_days"`
Permanent string `json:"permanent"`
}

err := json.Unmarshal(b, &s)

if err != nil {
return err
switch err.(type) {
case *json.UnmarshalTypeError: //check if type error occurred (handles if no type conversion is required for cloud like Huawei)

var s struct {
tmp
MaxBackups int `json:"max_backups"`
RetentionDurationDays int `json:"retention_duration_days"`
Permanent bool `json:"permanent"`
}
err := json.Unmarshal(b, &s)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if I don't miss anything, the struct s and method json.Unmarshal are the same. why to call it twice here when error occurring, or what issue are you going to fix?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The context is that Huawei Cloud API response for create backup policy has max_backups, retention_duration_days, permanent as string type but get and list api response has them as int and bool. Also other clouds like OTC, Telefonica, Orange have return type as string for mentioned parameters. First unmarshal call tries to unmarshal for string type but if it fails, then we check for UnmarshalTypeError (which occurs if type conversion is not possible) and the we try to unmarshal again in a struct which has them as int and bool.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@freesky-edward this solution is similar to #85

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Savasw ok, it's ok to me as a temporary solution. thanks for your update

if err != nil {
return err
}
*r = OperationDefinitionResp(s.tmp)
r.MaxBackups = s.MaxBackups
r.RetentionDurationDays = s.RetentionDurationDays
r.Permanent = s.Permanent
return nil
default:
return err
}
}

*r = OperationDefinitionResp(s.tmp)
Expand Down