Skip to content

Commit

Permalink
fixup! modify care team confirmation context for alerting
Browse files Browse the repository at this point in the history
  • Loading branch information
ewollesen committed Sep 9, 2023
1 parent 4716fb5 commit 4eb5838
Showing 1 changed file with 20 additions and 21 deletions.
41 changes: 20 additions & 21 deletions models/confirmation.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,34 +278,33 @@ type CareTeamContext struct {
// If the API is migrated so this custom unmarshaler isn't necessary, that
// would be a good thing.
func (e *CareTeamContext) UnmarshalJSON(b []byte) error {
const permissionsKey string = "permissions"
const alertsConfigKey string = "alertsConfig"
// noCustomUnmarshaler temporarily disables the custom JSON unmarshaler.
type noCustomUnmarshaler struct {
CareTeamContext
UnmarshalJSON struct{} `json:"-"`
}

generic := map[string]json.RawMessage{}
generic := &noCustomUnmarshaler{}
if err := json.Unmarshal(b, &generic); err != nil {
return fmt.Errorf("unmarshaling Confirmation Context: %w", err)
}
if genericAlertsConfig := generic[alertsConfigKey]; genericAlertsConfig != nil {
if err := json.Unmarshal(genericAlertsConfig, &e.AlertsConfig); err != nil {
return fmt.Errorf("unmarshaling AlertsConfig: %w", err)
}
delete(generic, alertsConfigKey)
if generic.AlertsConfig != nil {
e.AlertsConfig = generic.AlertsConfig
}
if genericPermissions := generic[permissionsKey]; genericPermissions != nil {
if err := json.Unmarshal(genericPermissions, &e.Permissions); err != nil {
return fmt.Errorf("unmarshaling hybrid Permissions: %w", err)
}
delete(generic, permissionsKey)
if generic.Nickname != nil && *generic.Nickname != "" {
e.Nickname = generic.Nickname
}
if generic.Permissions != nil {
e.Permissions = generic.Permissions
} else {
e.Permissions = clients.Permissions{}
for k := range generic {
perm := clients.Permission{}
err := json.Unmarshal(generic[k], &perm)
if err != nil {
return fmt.Errorf("unmarshaling Permissions: %w", err)
}
e.Permissions[k] = perm
// As there's no permissions key, this must be an older context.
if err := json.Unmarshal(b, &e.Permissions); err != nil {
return fmt.Errorf("unmarshaling Permissions: %w", err)
}
// Alternatively, one could unmarshal into a map, and iterate over it,
// copying fields that don't match these below.
delete(e.Permissions, "alertsConfig")
delete(e.Permissions, "nickname")
}

return nil
Expand Down

0 comments on commit 4eb5838

Please sign in to comment.