-
Notifications
You must be signed in to change notification settings - Fork 36
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
omitempty
on boolean attributes prevents changing them to false
#45
Comments
This is the fundamental issue with this Golang implementation of of the Hue API. It doesn't allow to update only specific values - it always sending all fields of the struct type. Good example is the bridge := huego.New("192.168.x.y", "asdfasdf...")
c := &huego.Config{
Name: "My Bridge",
}
// This fails because we are pushing fields that are not modifiable (e.g. bridgeid)
_, err := bridge.UpdateConfig(c)
if err != nil {
panic(err)
} The related test is therefore fundamentally invalid as you cannot push the same data as you read from the bridge. In order to fix this, there should be two types - one for read, one for write. The one for write should use |
Thanks for your input. This is actually something that has been pointed out several times and I'm working on finding a solution without breaking compatibility. One solution is to have each struct implement a json marshaler which adds missing fields. For example in func (s *Schedule) MarshalJSON() ([]byte, error) {
type Alias Schedule
d, err := json.Marshal(&struct{
*Alias
AutoDelete *bool `json:"autodelete,omitempty"`
}{
Alias: (*Alias)(s),
AutoDelete: &s.AutoDelete,
})
if err != nil {
return nil, err
}
return d, nil
} And then in data, err := schedule.MarshalJSON()
if err != nil {
return nil, err
} Would love to hear your thoughts on this |
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
inSchedule
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:
and here's what it outputs on my machine:
Checking the Bridge API manually confirms that this schedule has
autodelete: true
, and three seconds later it's gone.The text was updated successfully, but these errors were encountered: