amimof / amimof/huego

`omitempty` on boolean attributes prevents changing them to `false`

Open
#45 3 comments 0 reactions 0 assignees View on GitHub
Dominant language
Go
Stars
262
Forks
35
PR merge metrics
No merged PRs in 30d

Description

Some boolean attributes are annotated with `json:"attributename,omitempty"`, which means they are omitted from requests when set to the default value for their type, i.e. `false`. This means that it is impossible to set a boolean attribute to false using a Create* or Update* method.

`AutoDelete` in `Schedule` is one example of an affected attribute. It defaults to true when not provided, and there currently appears to be no way in huego to set it to false.

Here's a quick program that reproduces this:

```go
package main

import (
"fmt"
"encoding/json"
"github.com/amimof/huego"
)

func main() {
bridge := huego.New("192.168.0.221", "[redacted]")

schedule := huego.Schedule{
Name: "Test schedule",
Command: &huego.Command{
Address: "/api/0/lights/4/state",
Method: "PUT",
Body: map[string]bool{
"on": true,
},
},
LocalTime: "PT00:00:03",
AutoDelete: false,
}

schedule_json, err := json.Marshal(schedule)
if err != nil {
fmt.Printf("error: %v\n", err)
return
}

fmt.Printf("JSONd schedule looks like this:\n%v\n", string(schedule_json))

response, err := bridge.CreateSchedule(&schedule)
if err != nil {
fmt.Printf("error: %v\n", err)
return
}

fmt.Printf("response: %v\n", response)
}
```

and here's what it outputs on my machine:

```
JSONd schedule looks like this:
{"name":"Test schedule","description":"","command":{"address":"/api/0/lights/4/state","method":"PUT","body":{"on":true}},"localtime":"PT00:00:03"}
response: &{map[id:2]}
```

Checking the Bridge API manually confirms that this schedule has `autodelete: true`, and three seconds later it's gone.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.