Add backwards-compatible fix to make handle optional #143
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What is the problem?
According to Datadog's API Reference, REST methods responsible for dealing with the
Comment
entity can have theHandle
property optionally set which in some cases, this can be rather handy. It turns out that the way the API is currently set forces the developer always to specify it regardless of theomitempty
option in theComment struct
Why
omitempty
option fails during the json marshall phase?Although the piece of documentation above lead us to believe that if we set the
handle
argument to an empty string then it would work by itself, there is a caveat due to the Go langString(string)
helper function which creates a pointer for that empty string and subsequently, the criteria theomitempty
no longer applies as shown in this other piece of documentation below:What has been done to fix the problem?
There are a few solutions but it will all depend upon when you guys are planning to release the next major version of this framework. The solution could be simple by changing the related functions' arguments from built in
string type
to astring*
. This alone (along with the changes for making this compilable) would be sufficient to make it work as you can always set astring*
tonil
and the json marshall process wouldn't send it to the the API.Example:
However, this would break backwards-compatibility due to the change of methods' signatures and because of that, I picked a less invasive solution which gives you the control of when to tackle this design improvement at your own pace.
Having said that, the solution I proposed in this PR was simply checking whether or not the
handle
argument is empty and in case it isn't, then the program will setHandle
property in theComment struct
to that value.Best regards,
Paulo Almeida