Skip to content

Commit

Permalink
Fix dropped values during driver unmarshal (#227)
Browse files Browse the repository at this point in the history
* Fix dropped values during driver unmarshal

* Add comments to driver UnmarshalJSON
  • Loading branch information
bfbachmann authored Sep 28, 2023
1 parent 9617627 commit a0a7cc4
Show file tree
Hide file tree
Showing 12 changed files with 90 additions and 21 deletions.
11 changes: 9 additions & 2 deletions drivers/amazonec2/amazonec2.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,15 +377,22 @@ func (d *Driver) getClient() Ec2Client {
return d.clientFactory()
}

// UnmarshalJSON loads driver config from JSON.
// UnmarshalJSON loads driver config from JSON. This function is used by the RPCServerDriver that wraps
// all drivers as a means of populating an already-initialized driver with new configuration.
// See `RPCServerDriver.SetConfigRaw`.
func (d *Driver) UnmarshalJSON(data []byte) error {
// Unmarshal driver config into an aliased type to prevent infinite recursion on UnmarshalJSON.
type targetDriver Driver
target := targetDriver{}

// Copy data from `d` to `target` before unmarshalling. This will ensure that already-initialized values
// from `d` that are left untouched during unmarshal (like functions) are preserved.
target := targetDriver(*d)

if err := json.Unmarshal(data, &target); err != nil {
return fmt.Errorf("error unmarshalling driver config from JSON: %w", err)
}

// Copy unmarshalled data back to `d`.
*d = Driver(target)

// Make sure to reload values that are subject to change from envvars and os.Args.
Expand Down
11 changes: 9 additions & 2 deletions drivers/azure/azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,15 +322,22 @@ func (d *Driver) GetCreateFlags() []mcnflag.Flag {
}
}

// UnmarshalJSON loads driver config from JSON.
// UnmarshalJSON loads driver config from JSON. This function is used by the RPCServerDriver that wraps
// all drivers as a means of populating an already-initialized driver with new configuration.
// See `RPCServerDriver.SetConfigRaw`.
func (d *Driver) UnmarshalJSON(data []byte) error {
// Unmarshal driver config into an aliased type to prevent infinite recursion on UnmarshalJSON.
type targetDriver Driver
target := targetDriver{}

// Copy data from `d` to `target` before unmarshalling. This will ensure that already-initialized values
// from `d` that are left untouched during unmarshal (like functions) are preserved.
target := targetDriver(*d)

if err := json.Unmarshal(data, &target); err != nil {
return fmt.Errorf("error unmarshalling driver config from JSON: %w", err)
}

// Copy unmarshalled data back to `d`.
*d = Driver(target)

// Make sure to reload values that are subject to change from envvars and os.Args.
Expand Down
11 changes: 9 additions & 2 deletions drivers/digitalocean/digitalocean.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,15 +154,22 @@ func (d *Driver) DriverName() string {
return "digitalocean"
}

// UnmarshalJSON loads driver config from JSON.
// UnmarshalJSON loads driver config from JSON. This function is used by the RPCServerDriver that wraps
// all drivers as a means of populating an already-initialized driver with new configuration.
// See `RPCServerDriver.SetConfigRaw`.
func (d *Driver) UnmarshalJSON(data []byte) error {
// Unmarshal driver config into an aliased type to prevent infinite recursion on UnmarshalJSON.
type targetDriver Driver
target := targetDriver{}

// Copy data from `d` to `target` before unmarshalling. This will ensure that already-initialized values
// from `d` that are left untouched during unmarshal (like functions) are preserved.
target := targetDriver(*d)

if err := json.Unmarshal(data, &target); err != nil {
return fmt.Errorf("error unmarshalling driver config from JSON: %w", err)
}

// Copy unmarshalled data back to `d`.
*d = Driver(target)

// Make sure to reload values that are subject to change from envvars and os.Args.
Expand Down
11 changes: 9 additions & 2 deletions drivers/exoscale/exoscale.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,15 +187,22 @@ func (d *Driver) DriverName() string {
return "exoscale"
}

// UnmarshalJSON loads driver config from JSON.
// UnmarshalJSON loads driver config from JSON. This function is used by the RPCServerDriver that wraps
// all drivers as a means of populating an already-initialized driver with new configuration.
// See `RPCServerDriver.SetConfigRaw`.
func (d *Driver) UnmarshalJSON(data []byte) error {
// Unmarshal driver config into an aliased type to prevent infinite recursion on UnmarshalJSON.
type targetDriver Driver
target := targetDriver{}

// Copy data from `d` to `target` before unmarshalling. This will ensure that already-initialized values
// from `d` that are left untouched during unmarshal (like functions) are preserved.
target := targetDriver(*d)

if err := json.Unmarshal(data, &target); err != nil {
return fmt.Errorf("error unmarshalling driver config from JSON: %w", err)
}

// Copy unmarshalled data back to `d`.
*d = Driver(target)

// Make sure to reload values that are subject to change from envvars and os.Args.
Expand Down
11 changes: 9 additions & 2 deletions drivers/google/google.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,15 +201,22 @@ func (d *Driver) DriverName() string {
return "google"
}

// UnmarshalJSON loads driver config from JSON.
// UnmarshalJSON loads driver config from JSON. This function is used by the RPCServerDriver that wraps
// all drivers as a means of populating an already-initialized driver with new configuration.
// See `RPCServerDriver.SetConfigRaw`.
func (d *Driver) UnmarshalJSON(data []byte) error {
// Unmarshal driver config into an aliased type to prevent infinite recursion on UnmarshalJSON.
type targetDriver Driver
target := targetDriver{}

// Copy data from `d` to `target` before unmarshalling. This will ensure that already-initialized values
// from `d` that are left untouched during unmarshal (like functions) are preserved.
target := targetDriver(*d)

if err := json.Unmarshal(data, &target); err != nil {
return fmt.Errorf("error unmarshalling driver config from JSON: %w", err)
}

// Copy unmarshalled data back to `d`.
*d = Driver(target)

// Make sure to reload values that are subject to change from envvars and os.Args.
Expand Down
4 changes: 3 additions & 1 deletion drivers/noop/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,9 @@ func (d *Driver) Restart() error {
return nil
}

// UnmarshalJSON loads driver config from JSON.
// UnmarshalJSON loads driver config from JSON. This function is used by the RPCServerDriver that wraps
// all drivers as a means of populating an already-initialized driver with new configuration.
// See `RPCServerDriver.SetConfigRaw`.
func (d *Driver) UnmarshalJSON(data []byte) error {
// Unmarshal driver config into an aliased type to prevent infinite recursion on UnmarshalJSON.
type targetDriver Driver
Expand Down
11 changes: 9 additions & 2 deletions drivers/openstack/openstack.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,15 +362,22 @@ func (d *Driver) DriverName() string {
return "openstack"
}

// UnmarshalJSON loads driver config from JSON.
// UnmarshalJSON loads driver config from JSON. This function is used by the RPCServerDriver that wraps
// all drivers as a means of populating an already-initialized driver with new configuration.
// See `RPCServerDriver.SetConfigRaw`.
func (d *Driver) UnmarshalJSON(data []byte) error {
// Unmarshal driver config into an aliased type to prevent infinite recursion on UnmarshalJSON.
type targetDriver Driver
target := targetDriver{}

// Copy data from `d` to `target` before unmarshalling. This will ensure that already-initialized values
// from `d` that are left untouched during unmarshal (like functions) are preserved.
target := targetDriver(*d)

if err := json.Unmarshal(data, &target); err != nil {
return fmt.Errorf("error unmarshalling driver config from JSON: %w", err)
}

// Copy unmarshalled data back to `d`.
*d = Driver(target)

// Make sure to reload values that are subject to change from envvars and os.Args.
Expand Down
11 changes: 9 additions & 2 deletions drivers/rackspace/rackspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,15 +123,22 @@ func missingEnvOrOption(setting, envVar, opt string) error {
)
}

// UnmarshalJSON loads driver config from JSON.
// UnmarshalJSON loads driver config from JSON. This function is used by the RPCServerDriver that wraps
// all drivers as a means of populating an already-initialized driver with new configuration.
// See `RPCServerDriver.SetConfigRaw`.
func (d *Driver) UnmarshalJSON(data []byte) error {
// Unmarshal driver config into an aliased type to prevent infinite recursion on UnmarshalJSON.
type targetDriver Driver
target := targetDriver{}

// Copy data from `d` to `target` before unmarshalling. This will ensure that already-initialized values
// from `d` that are left untouched during unmarshal (like functions) are preserved.
target := targetDriver(*d)

if err := json.Unmarshal(data, &target); err != nil {
return fmt.Errorf("error unmarshalling driver config from JSON: %w", err)
}

// Copy unmarshalled data back to `d`.
*d = Driver(target)

// Make sure to reload values that are subject to change from envvars and os.Args.
Expand Down
11 changes: 9 additions & 2 deletions drivers/softlayer/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,15 +220,22 @@ func validateClientConfig(c *Client) error {
return nil
}

// UnmarshalJSON loads driver config from JSON.
// UnmarshalJSON loads driver config from JSON. This function is used by the RPCServerDriver that wraps
// all drivers as a means of populating an already-initialized driver with new configuration.
// See `RPCServerDriver.SetConfigRaw`.
func (d *Driver) UnmarshalJSON(data []byte) error {
// Unmarshal driver config into an aliased type to prevent infinite recursion on UnmarshalJSON.
type targetDriver Driver
target := targetDriver{}

// Copy data from `d` to `target` before unmarshalling. This will ensure that already-initialized values
// from `d` that are left untouched during unmarshal (like functions) are preserved.
target := targetDriver(*d)

if err := json.Unmarshal(data, &target); err != nil {
return fmt.Errorf("error unmarshalling driver config from JSON: %w", err)
}

// Copy unmarshalled data back to `d`.
*d = Driver(target)

// Make sure to reload values that are subject to change from envvars and os.Args.
Expand Down
4 changes: 3 additions & 1 deletion drivers/vmwarefusion/fusion_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,9 @@ func (d *Driver) DriverName() string {
return "vmwarefusion"
}

// UnmarshalJSON loads driver config from JSON.
// UnmarshalJSON loads driver config from JSON. This function is used by the RPCServerDriver that wraps
// all drivers as a means of populating an already-initialized driver with new configuration.
// See `RPCServerDriver.SetConfigRaw`.
func (d *Driver) UnmarshalJSON(data []byte) error {
// Unmarshal driver config into an aliased type to prevent infinite recursion on UnmarshalJSON.
type targetDriver Driver
Expand Down
4 changes: 3 additions & 1 deletion drivers/vmwarevcloudair/vcloudair.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,9 @@ func (d *Driver) DriverName() string {
return "vmwarevcloudair"
}

// UnmarshalJSON loads driver config from JSON.
// UnmarshalJSON loads driver config from JSON. This function is used by the RPCServerDriver that wraps
// all drivers as a means of populating an already-initialized driver with new configuration.
// See `RPCServerDriver.SetConfigRaw`.
func (d *Driver) UnmarshalJSON(data []byte) error {
// Unmarshal driver config into an aliased type to prevent infinite recursion on UnmarshalJSON.
type targetDriver Driver
Expand Down
11 changes: 9 additions & 2 deletions drivers/vmwarevsphere/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,15 +207,22 @@ func (d *Driver) GetCreateFlags() []mcnflag.Flag {
}
}

// UnmarshalJSON loads driver config from JSON.
// UnmarshalJSON loads driver config from JSON. This function is used by the RPCServerDriver that wraps
// all drivers as a means of populating an already-initialized driver with new configuration.
// See `RPCServerDriver.SetConfigRaw`.
func (d *Driver) UnmarshalJSON(data []byte) error {
// Unmarshal driver config into an aliased type to prevent infinite recursion on UnmarshalJSON.
type targetDriver Driver
target := targetDriver{}

// Copy data from `d` to `target` before unmarshalling. This will ensure that already-initialized values
// from `d` that are left untouched during unmarshal (like functions) are preserved.
target := targetDriver(*d)

if err := json.Unmarshal(data, &target); err != nil {
return fmt.Errorf("error unmarshalling driver config from JSON: %w", err)
}

// Copy unmarshalled data back to `d`.
*d = Driver(target)

// Make sure to reload values that are subject to change from envvars and os.Args.
Expand Down

0 comments on commit a0a7cc4

Please sign in to comment.