From 294824ec0e51c825936447e58bf8b8d931df040b Mon Sep 17 00:00:00 2001 From: Daniel Gonzalez Date: Sat, 3 Dec 2022 22:38:09 +0100 Subject: [PATCH] refactor: Replace deprecated fields and functions --- internal/provider/geofencing_resource.go | 39 +++++++++++++----------- internal/provider/home_data_source.go | 2 +- internal/provider/provider.go | 12 ++++---- internal/provider/util.go | 7 ++--- internal/provider/util_test.go | 2 +- internal/provider/zone_data_source.go | 10 +++--- 6 files changed, 38 insertions(+), 34 deletions(-) diff --git a/internal/provider/geofencing_resource.go b/internal/provider/geofencing_resource.go index 644aabf..469dcd0 100644 --- a/internal/provider/geofencing_resource.go +++ b/internal/provider/geofencing_resource.go @@ -100,13 +100,15 @@ func (r GeofencingResource) Create(ctx context.Context, req resource.CreateReque return } - home, err := me.GetHome(ctx, data.HomeName.Value) + homeName := data.HomeName.ValueString() + home, err := me.GetHome(ctx, homeName) if err != nil { - resp.Diagnostics.AddError("Tado API Error", fmt.Sprintf("Unable to get home '%s': %v", data.HomeName.Value, err)) + resp.Diagnostics.AddError("Tado API Error", fmt.Sprintf("Unable to get home '%s': %v", homeName, err)) return } - switch data.Presence.Value { + presence := data.Presence.ValueString() + switch presence { case "auto": err = home.SetPresenceAuto(ctx) case "home": @@ -114,22 +116,22 @@ func (r GeofencingResource) Create(ctx context.Context, req resource.CreateReque case "away": err = home.SetPresenceAway(ctx) default: - resp.Diagnostics.AddError("Invalid Presence", fmt.Sprintf("Invalid presence value '%s', must be one of 'auto', 'home' or 'away'.", data.Presence.Value)) + resp.Diagnostics.AddError("Invalid Presence", fmt.Sprintf("Invalid presence value '%s', must be one of 'auto', 'home' or 'away'.", presence)) return } if err != nil { - resp.Diagnostics.AddError("Tado API Error", fmt.Sprintf("Unable to set presence to '%s': %v", data.Presence.Value, err)) + resp.Diagnostics.AddError("Tado API Error", fmt.Sprintf("Unable to set presence to '%s': %v", presence, err)) return } homeState, err := home.GetState(ctx) if err != nil { - resp.Diagnostics.AddError("Tado API Error", fmt.Sprintf("Unable to get state of home '%s': %v", data.HomeName.Value, err)) + resp.Diagnostics.AddError("Tado API Error", fmt.Sprintf("Unable to get state of home '%s': %v", homeName, err)) return } - presence := strings.ToLower(string(homeState.Presence)) + presence = strings.ToLower(string(homeState.Presence)) // If presence is not locked, it is set to 'auto'. if !homeState.PresenceLocked { presence = "auto" @@ -159,15 +161,16 @@ func (r GeofencingResource) Read(ctx context.Context, req resource.ReadRequest, return } - home, err := me.GetHome(ctx, data.HomeName.Value) + homeName := data.HomeName.ValueString() + home, err := me.GetHome(ctx, homeName) if err != nil { - resp.Diagnostics.AddError("Tado API Error", fmt.Sprintf("Unable to get home '%s': %v", data.HomeName.Value, err)) + resp.Diagnostics.AddError("Tado API Error", fmt.Sprintf("Unable to get home '%s': %v", homeName, err)) return } homeState, err := home.GetState(ctx) if err != nil { - resp.Diagnostics.AddError("Tado API Error", fmt.Sprintf("Unable to get state of home '%s': %v", data.HomeName.Value, err)) + resp.Diagnostics.AddError("Tado API Error", fmt.Sprintf("Unable to get state of home '%s': %v", homeName, err)) return } @@ -201,13 +204,15 @@ func (r GeofencingResource) Update(ctx context.Context, req resource.UpdateReque return } - home, err := me.GetHome(ctx, data.HomeName.Value) + homeName := data.HomeName.ValueString() + home, err := me.GetHome(ctx, homeName) if err != nil { - resp.Diagnostics.AddError("Tado API Error", fmt.Sprintf("Unable to get home '%s': %v", data.HomeName.Value, err)) + resp.Diagnostics.AddError("Tado API Error", fmt.Sprintf("Unable to get home '%s': %v", homeName, err)) return } - switch data.Presence.Value { + presence := data.Presence.ValueString() + switch presence { case "auto": err = home.SetPresenceAuto(ctx) case "home": @@ -215,22 +220,22 @@ func (r GeofencingResource) Update(ctx context.Context, req resource.UpdateReque case "away": err = home.SetPresenceAway(ctx) default: - resp.Diagnostics.AddError("Invalid Presence", fmt.Sprintf("Invalid presence value '%s', must be one of 'auto', 'home' or 'away'.", data.Presence.Value)) + resp.Diagnostics.AddError("Invalid Presence", fmt.Sprintf("Invalid presence value '%s', must be one of 'auto', 'home' or 'away'.", presence)) return } if err != nil { - resp.Diagnostics.AddError("Tado API Error", fmt.Sprintf("Unable to set presence to '%s': %v", data.Presence.Value, err)) + resp.Diagnostics.AddError("Tado API Error", fmt.Sprintf("Unable to set presence to '%s': %v", presence, err)) return } homeState, err := home.GetState(ctx) if err != nil { - resp.Diagnostics.AddError("Tado API Error", fmt.Sprintf("Unable to get state of home '%s': %v", data.HomeName.Value, err)) + resp.Diagnostics.AddError("Tado API Error", fmt.Sprintf("Unable to get state of home '%s': %v", homeName, err)) return } - presence := strings.ToLower(string(homeState.Presence)) + presence = strings.ToLower(string(homeState.Presence)) // If presence is not locked, it is set to 'auto'. if !homeState.PresenceLocked { presence = "auto" diff --git a/internal/provider/home_data_source.go b/internal/provider/home_data_source.go index c50cca9..e75197e 100644 --- a/internal/provider/home_data_source.go +++ b/internal/provider/home_data_source.go @@ -163,7 +163,7 @@ func (d HomeDataSource) Read(ctx context.Context, req datasource.ReadRequest, re return } - home, err := me.GetHome(ctx, data.Name.Value) + home, err := me.GetHome(ctx, data.Name.ValueString()) if err != nil { resp.Diagnostics.AddError("Tado Authentication Error", fmt.Sprintf("Unable to authenticate with Tado: %v", err)) return diff --git a/internal/provider/provider.go b/internal/provider/provider.go index 89ebb54..fb4546a 100644 --- a/internal/provider/provider.go +++ b/internal/provider/provider.go @@ -78,26 +78,26 @@ func (*TadoProvider) Configure(ctx context.Context, req provider.ConfigureReques } var username string - if data.Username.Unknown { + if data.Username.IsUnknown() { resp.Diagnostics.AddWarning("Tado username is not set", "Tado username is not set. This is required for authentication.") } - if data.Username.Null { + if data.Username.IsNull() { username = os.Getenv("TADO_USERNAME") } else { - username = data.Username.Value + username = data.Username.ValueString() } if username == "" { resp.Diagnostics.AddError("Tado username is not set", "Tado username is not set. This is required for authentication.") } var password string - if data.Password.Unknown { + if data.Password.IsUnknown() { resp.Diagnostics.AddWarning("Tado password is not set", "Tado password is not set. This is required for authentication.") } - if data.Password.Null { + if data.Password.IsNull() { password = os.Getenv("TADO_PASSWORD") } else { - password = data.Password.Value + password = data.Password.ValueString() } if password == "" { resp.Diagnostics.AddError("Tado password is not set", "Tado password is not set. This is required for authentication.") diff --git a/internal/provider/util.go b/internal/provider/util.go index 6004bb1..26e3912 100644 --- a/internal/provider/util.go +++ b/internal/provider/util.go @@ -7,11 +7,8 @@ import ( // toTypesString converts a string pointer to a types.String. // If the pointer is nil, the types.String will be set to Null. func toTypesString(s *string) types.String { - str := types.String{} if s == nil { - str.Null = true - } else { - str.Value = *s + return types.StringNull() } - return str + return types.StringValue(*s) } diff --git a/internal/provider/util_test.go b/internal/provider/util_test.go index e63a365..e81ffd3 100644 --- a/internal/provider/util_test.go +++ b/internal/provider/util_test.go @@ -13,7 +13,7 @@ func TestToTypesStr(t *testing.T) { }{ { s: "test", - expected: types.String{Value: "test"}, + expected: types.StringValue("test"), }, } diff --git a/internal/provider/zone_data_source.go b/internal/provider/zone_data_source.go index 08202e2..d6f8308 100644 --- a/internal/provider/zone_data_source.go +++ b/internal/provider/zone_data_source.go @@ -121,15 +121,17 @@ func (d ZoneDataSource) Read(ctx context.Context, req datasource.ReadRequest, re return } - home, err := me.GetHome(ctx, data.Home.Value) + homeName := data.Home.ValueString() + home, err := me.GetHome(ctx, homeName) if err != nil { - resp.Diagnostics.AddError("Tado API Error", fmt.Sprintf("Unable to get home '%s': %v", data.Home.Value, err)) + resp.Diagnostics.AddError("Tado API Error", fmt.Sprintf("Unable to get home '%s': %v", homeName, err)) return } - zone, err := home.GetZone(ctx, data.Name.Value) + zoneName := data.Name.ValueString() + zone, err := home.GetZone(ctx, zoneName) if err != nil { - resp.Diagnostics.AddError("Tado API Error", fmt.Sprintf("Unable to get zone '%s': %v", data.Name.Value, err)) + resp.Diagnostics.AddError("Tado API Error", fmt.Sprintf("Unable to get zone '%s': %v", zoneName, err)) return }