-
Notifications
You must be signed in to change notification settings - Fork 62
honor explicit empty networks by adding Networks *[]int (deprecate NetworkIDs) #256
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
Open
haydencarroll-NS1
wants to merge
4
commits into
v2
Choose a base branch
from
zone-networks-pointer-empty
base: v2
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+292
−2
Open
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "fmt" | ||
|
|
||
| "gopkg.in/ns1/ns1-go.v2/rest/model/dns" | ||
| ) | ||
|
|
||
| func main() { | ||
| fmt.Println("NS1 SDK Empty Networks Example") | ||
| fmt.Println("------------------------------") | ||
|
|
||
| // Case 1: Default behavior (nil Networks) | ||
| zone1 := dns.NewZone("example.com") | ||
| data1, _ := json.Marshal(zone1) | ||
| fmt.Println("Case 1 - Default (nil Networks):") | ||
| fmt.Printf(" JSON: %s\n", string(data1)) | ||
| fmt.Printf(" Contains 'networks': %v\n\n", contains(string(data1), "networks")) | ||
|
|
||
| // Case 2: Empty Networks (explicit empty array) | ||
| zone2 := dns.NewZone("empty.example.com") | ||
| empty := []int{} | ||
| zone2.Networks = &empty | ||
| data2, _ := json.Marshal(zone2) | ||
| fmt.Println("Case 2 - Empty Networks (explicit empty array):") | ||
| fmt.Printf(" JSON: %s\n", string(data2)) | ||
| fmt.Printf(" Contains 'networks': %v\n", contains(string(data2), "networks")) | ||
| fmt.Printf(" Has empty networks array: %v\n\n", contains(string(data2), `"networks":[]`)) | ||
|
|
||
| // Case 3: Populated Networks | ||
| zone3 := dns.NewZone("populated.example.com") | ||
| networks := []int{1, 2} | ||
| zone3.Networks = &networks | ||
| data3, _ := json.Marshal(zone3) | ||
| fmt.Println("Case 3 - Populated Networks:") | ||
| fmt.Printf(" JSON: %s\n", string(data3)) | ||
| fmt.Printf(" Contains networks: %v\n\n", contains(string(data3), `"networks":[1,2]`)) | ||
|
|
||
| // Case 4: Legacy NetworkIDs with EnsureNetworksFromLegacy | ||
| zone4 := dns.NewZone("legacy.example.com") | ||
| zone4.NetworkIDs = []int{3, 4} | ||
| zone4.EnsureNetworksFromLegacy() | ||
| data4, _ := json.Marshal(zone4) | ||
| fmt.Println("Case 4 - Legacy NetworkIDs with EnsureNetworksFromLegacy:") | ||
| fmt.Printf(" JSON: %s\n", string(data4)) | ||
| fmt.Printf(" Contains networks: %v\n", contains(string(data4), `"networks":[3,4]`)) | ||
| } | ||
|
|
||
| func contains(s, substr string) bool { | ||
haydencarroll-NS1 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| for i := 0; i <= len(s)-len(substr); i++ { | ||
| if s[i:i+len(substr)] == substr { | ||
| return true | ||
| } | ||
| } | ||
| return false | ||
| } | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,142 @@ | ||
| package dns | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| func TestZoneNetworks_MarshalJSON(t *testing.T) { | ||
| // Test Case 1: nil Networks should be omitted | ||
| z := Zone{ | ||
| Zone: "example.com", | ||
| Networks: nil, | ||
| NetworkIDs: nil, | ||
| } | ||
| data, err := json.Marshal(z) | ||
| assert.NoError(t, err) | ||
| assert.NotContains(t, string(data), "networks") | ||
|
|
||
| // Test Case 2: Empty Networks should be included as [] | ||
| empty := []int{} | ||
| z = Zone{ | ||
| Zone: "example.com", | ||
| Networks: &empty, | ||
| NetworkIDs: nil, | ||
| } | ||
| data, err = json.Marshal(z) | ||
| assert.NoError(t, err) | ||
| assert.Contains(t, string(data), `"networks":[]`) | ||
|
|
||
| // Test Case 3: Populated Networks should be included | ||
| networks := []int{1, 2, 3} | ||
| z = Zone{ | ||
| Zone: "example.com", | ||
| Networks: &networks, | ||
| NetworkIDs: nil, | ||
| } | ||
| data, err = json.Marshal(z) | ||
| assert.NoError(t, err) | ||
| assert.Contains(t, string(data), `"networks":[1,2,3]`) | ||
| } | ||
|
|
||
| func TestZoneNetworks_UnmarshalJSON(t *testing.T) { | ||
| // Test Case 1: JSON with networks field should populate both Networks and NetworkIDs | ||
| jsonStr := `{"zone":"example.com","networks":[1,2,3]}` | ||
| var z Zone | ||
| err := json.Unmarshal([]byte(jsonStr), &z) | ||
| assert.NoError(t, err) | ||
| assert.NotNil(t, z.Networks) | ||
| assert.Equal(t, []int{1, 2, 3}, *z.Networks) | ||
| assert.Equal(t, []int{1, 2, 3}, z.NetworkIDs) | ||
|
|
||
| // Test Case 2: JSON with empty networks array should result in empty slices | ||
| jsonStr = `{"zone":"example.com","networks":[]}` | ||
| z = Zone{} | ||
| err = json.Unmarshal([]byte(jsonStr), &z) | ||
| assert.NoError(t, err) | ||
| assert.NotNil(t, z.Networks) | ||
| assert.Equal(t, 0, len(*z.Networks)) | ||
| assert.Equal(t, 0, len(z.NetworkIDs)) | ||
|
|
||
| // Test Case 3: JSON without networks field should result in nil fields | ||
| jsonStr = `{"zone":"example.com"}` | ||
| z = Zone{} | ||
| err = json.Unmarshal([]byte(jsonStr), &z) | ||
| assert.NoError(t, err) | ||
| assert.Nil(t, z.Networks) | ||
| assert.Nil(t, z.NetworkIDs) | ||
| } | ||
|
|
||
| func TestZone_EnsureNetworksFromLegacy(t *testing.T) { | ||
| // Test Case 1: When Networks is nil and NetworkIDs has values | ||
| networkIDs := []int{1, 2, 3} | ||
| z := Zone{ | ||
| Zone: "example.com", | ||
| Networks: nil, | ||
| NetworkIDs: networkIDs, | ||
| } | ||
| z.EnsureNetworksFromLegacy() | ||
| assert.NotNil(t, z.Networks) | ||
| assert.Equal(t, networkIDs, *z.Networks) | ||
|
|
||
| // Test Case 2: When Networks is already set, it shouldn't change | ||
| networks := []int{4, 5, 6} | ||
| z = Zone{ | ||
| Zone: "example.com", | ||
| Networks: &networks, | ||
| NetworkIDs: networkIDs, | ||
| } | ||
| z.EnsureNetworksFromLegacy() | ||
| assert.NotNil(t, z.Networks) | ||
| assert.Equal(t, networks, *z.Networks) | ||
| assert.NotEqual(t, networkIDs, *z.Networks) | ||
|
|
||
| // Test Case 3: When both are empty/nil | ||
| z = Zone{ | ||
| Zone: "example.com", | ||
| Networks: nil, | ||
| NetworkIDs: nil, | ||
| } | ||
| z.EnsureNetworksFromLegacy() | ||
| assert.Nil(t, z.Networks) | ||
| assert.Nil(t, z.NetworkIDs) | ||
| } | ||
|
|
||
| // Integration test to verify the entire flow works as expected | ||
| func TestZoneNetworks_IntegrationFlow(t *testing.T) { | ||
| // Starting with a Zone using legacy NetworkIDs | ||
| z := Zone{ | ||
| Zone: "example.com", | ||
| NetworkIDs: []int{1, 2, 3}, | ||
| } | ||
|
|
||
| // Step 1: Call EnsureNetworksFromLegacy to populate Networks | ||
| z.EnsureNetworksFromLegacy() | ||
| assert.NotNil(t, z.Networks) | ||
| assert.Equal(t, z.NetworkIDs, *z.Networks) | ||
|
|
||
| // Step 2: Marshal to JSON | ||
| data, err := json.Marshal(z) | ||
| assert.NoError(t, err) | ||
| assert.Contains(t, string(data), `"networks":[1,2,3]`) | ||
|
|
||
| // Step 3: Change to empty networks | ||
| empty := []int{} | ||
| z.Networks = &empty | ||
| z.NetworkIDs = nil | ||
|
|
||
| // Step 4: Marshal again to verify empty array is sent | ||
| data, err = json.Marshal(z) | ||
| assert.NoError(t, err) | ||
| assert.Contains(t, string(data), `"networks":[]`) | ||
|
|
||
| // Step 5: Unmarshal from JSON with networks field | ||
| jsonStr := `{"zone":"example.com","networks":[4,5]}` | ||
| err = json.Unmarshal([]byte(jsonStr), &z) | ||
| assert.NoError(t, err) | ||
| assert.NotNil(t, z.Networks) | ||
| assert.Equal(t, []int{4, 5}, *z.Networks) | ||
| assert.Equal(t, []int{4, 5}, z.NetworkIDs) | ||
| } |
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
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.