Skip to content
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

dashboards: Add Yaxis.AutoMin, YAxis.AutoMax, and custom Unmarshal #167

Merged
2 commits merged into from
Aug 24, 2018

Conversation

anthony-tsang
Copy link
Contributor

  • Add in the AutoMin and AutoMax fields for the Yaxis struct so we can work around the times when datadog's API returns a string when we expect a float64.
  • Add a custom UnmarshalJSON for Yaxis to utilize the new AutoMin and AutoMax fields.

related error: "json: cannot unmarshal string into Go struct field Yaxis.min of type
float64"

Followed the suggestions from @yfronto in the previous open PR.

Please let me know if this is the right direction and if you have anymore suggestions.

* Add in the AutoMin and AutoMax fields for the Yaxis struct so we can work around the times when datadog's API returns a string when we expect a float64.
* Add a custom UnmarshalJSON for Yaxis to utilize the new AutoMin and AutoMax fields.

related error: "json: cannot unmarshal string into Go struct field Yaxis.min of type
float64"
@jamesmharvey
Copy link
Contributor

This is in reference to issue #103

@ghost
Copy link

ghost commented Aug 22, 2018

Thanks for the submission. Generally looks good. Can you run make generate?

Copy link

@ghost ghost left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few comments.

dashboards.go Outdated
return err
}

if *wrapper.Min == "auto" {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the returned blob didn't contain the field, it would be null, so it's probably safer to check here.

if wrapper.Min != nil && *wrapper.Min == "auto" {
...
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added the additional nil check

dashboards.go Outdated
*y.AutoMin = true
y.Min = nil
} else {
*wrapper.AutoMin = false
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The wrapper will go out of scope at this end of the func, so this isn't necessary.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just for clarification are you saying I do not need to set wrapper.AutoMin or the whole else block is not necessary?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The block is necessary for parsing the float, but this line is unnecessary because wrapper.AutoMin will not be used again.

dashboards.go Outdated
}

if *wrapper.Min == "auto" {
*y.AutoMin = true
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can't dereference this here. You'll need to instantiate it first or don't use pointers for the Auto* fields, since they're local.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed pointers for the Auto* fields

* Convert Yaxis.AutoMin and Yaxis.AutoMax to `bool` rather than `*bool` since they are local fields
* Add json:"-" to `AutoMin` and `AutoMax` fields so the do not get marshalled
* Change the wrapper Min/Max fields to be type *json.Number so that Min/Max can take a string (“auto”) if necessary.
    * I was running into an issue when {“min”: “auto”}, son.Unmarshal would complain `json: cannot unmarshal number into Go value of type string`
* Add Unit Tests for marshaling and unmarshalling Yaxis <-> JSON
Copy link

@ghost ghost left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

dashboards.go Outdated
*y.AutoMin = true
y.Min = nil
} else {
*wrapper.AutoMin = false
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The block is necessary for parsing the float, but this line is unnecessary because wrapper.AutoMin will not be used again.

@ghost ghost removed the awaiting response label Aug 24, 2018
@ghost ghost merged commit 47fbc01 into zorkian:master Aug 24, 2018
dashboards.go Outdated
AutoMax * bool
Scale *string `json:"scale,omitempty"`
Min *float64 `json:"min,omitempty"`
AutoMin bool `json:"-"`
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also FYI, making a field private will also prevent it from being marshalled (although it also prevents consumers from using it, obviously).

@anthony-tsang anthony-tsang deleted the Yaxis-workaround branch August 24, 2018 16:35
ghost pushed a commit that referenced this pull request Aug 24, 2018
This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants