Merged
Conversation
`DNSRecord` priority was initially an int. This worked most of the time
however in some cases, 0 was intended to be used as a zero value and in
others, a valid value. This causes confusion and in combination with
`omitempty` on the struct, makes it impossible to send 0 as a value.
```go
type DNSRecord struct {
Type string `json:"type,omitempty"`
Priority int `json:"priority,omitempty"`
}
func main() {
// using 0 as a value
var p = int(0)
d := DNSRecord{
Type: "MX",
Priority: p,
}
b, _ := json.Marshal(d)
fmt.Println(string(b))
// => {"type":"MX"}
}
```
To rectify, I've swapped the struct to `*uint16` which accomplishes two
things. The first, aligns with our internal representation of the data
struct. The second, allows us to use 0 in both contexts as a zero value
but also as a value reference where it is needed.
```go
type DNSRecord struct {
Type string `json:"type,omitempty"`
Priority *uint16 `json:"priority,omitempty"`
}
func main() {
var p1 = uint16(0)
d1 := DNSRecord{
Type: "MX",
Priority: &p1,
}
b1, _ := json.Marshal(d1)
fmt.Println(string(b1))
// => {"type":"MX","priority":0}
var p2 = uint16(10)
d2 := DNSRecord{
Type: "MX",
Priority: &p2,
}
b2, _ := json.Marshal(d2)
fmt.Println(string(b2))
// => {"type":"MX","priority":10}
d3 := DNSRecord{
Type: "MX",
Priority: nil,
}
b3, _ := json.Marshal(d3)
fmt.Println(string(b3))
// => {"type":"MX"}
}
```
Fixes SDK-114 and lays the ground work for the Terraform provider fix.
Contributor
Author
|
To add some more context, upstream in Terraform, I'm thinking of the following to allow people to set the @@ -291,9 +291,9 @@ func resourceCloudflareRecord() *schema.Resource {
func resourceCloudflareRecordCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*cloudflare.API)
-
+ rrType := d.Get("type").(string)
newRecord := cloudflare.DNSRecord{
- Type: d.Get("type").(string),
+ Type: rrType,
Name: d.Get("name").(string),
Proxied: d.Get("proxied").(bool),
ZoneID: d.Get("zone_id").(string),
@@ -329,8 +329,9 @@ func resourceCloudflareRecordCreate(d *schema.ResourceData, meta interface{}) er
valueOk, dataOk)
}
- if priority, ok := d.GetOk("priority"); ok {
- newRecord.Priority = priority.(int)
+ if rrType == "MX" {
+ priority := d.Get("priority").(uint16)
+ newRecord.Priority = &priority
}Where |
patryk
approved these changes
Feb 9, 2021
patryk
left a comment
There was a problem hiding this comment.
Yeah, I agree with your reasoning. But we would need to release a breaking change release hre to indicate a backwards-incompatible change.
Contributor
Author
|
i've pushed a |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
DNSRecordpriority was initially an int. This worked most of the timehowever in some cases, 0 was intended to be used as a zero value and in
others, a valid value. This causes confusion and in combination with
omitemptyon the struct, makes it impossible to send 0 as a value.To rectify, I've swapped the struct to
*uint16which accomplishes twothings. The first, aligns with our internal representation of the data
struct. The second, allows us to use 0 in both contexts as a zero value
but also as a value reference where it is needed.
Fixes SDK-114 and lays the ground work for the Terraform provider fix.