This repository has been archived by the owner on Jan 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 558
populate default value in unmarshal #939
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
ebae2a5
populate default mastercount to 1 in unmarshalJSON
d024a03
populate default value for agentpool count and osType for v2017-01-31
117b4e3
add unmarshal function for 2016-09-30 and 2016-03-30
9384fc5
add unmarshal function for 2017-07-01, with TODO comments
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 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 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 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 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 |
---|---|---|
@@ -1,6 +1,7 @@ | ||
package v20170131 | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
neturl "net/url" | ||
"strings" | ||
|
@@ -109,6 +110,23 @@ type MasterProfile struct { | |
subnet string | ||
} | ||
|
||
// UnmarshalJSON unmarshal json using the default behavior | ||
// And do fields manipulation, such as populating default value | ||
func (m *MasterProfile) UnmarshalJSON(b []byte) error { | ||
// Need to have a alias type to avoid circular unmarshal | ||
type aliasMasterProfile MasterProfile | ||
mm := aliasMasterProfile{} | ||
if e := json.Unmarshal(b, &mm); e != nil { | ||
return e | ||
} | ||
*m = MasterProfile(mm) | ||
if m.Count == 0 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If other fields in MasterProfile need to populate default value, it can be done inside this function. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does json.Unmarshal fill the empty field with 0? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, for int 0, for string "" |
||
// if MasterProfile.Count is missing or 0, set to default 1 | ||
m.Count = 1 | ||
} | ||
return nil | ||
} | ||
|
||
// AgentPoolProfile represents configuration of VMs running agent | ||
// daemons that register with the master and offer resources to | ||
// host applications in containers. | ||
|
@@ -129,6 +147,31 @@ type AgentPoolProfile struct { | |
subnet string | ||
} | ||
|
||
// UnmarshalJSON unmarshal json using the default behavior | ||
// And do fields manipulation, such as populating default value | ||
func (a *AgentPoolProfile) UnmarshalJSON(b []byte) error { | ||
// Need to have a alias type to avoid circular unmarshal | ||
type aliasAgentPoolProfile AgentPoolProfile | ||
aa := aliasAgentPoolProfile{} | ||
if e := json.Unmarshal(b, &aa); e != nil { | ||
return e | ||
} | ||
*a = AgentPoolProfile(aa) | ||
if a.Count == 0 { | ||
// if AgentPoolProfile.Count is missing or 0, set it to default 1 | ||
a.Count = 1 | ||
} | ||
|
||
if string(a.OSType) == "" { | ||
// OSType is the operating system type for agents | ||
// Set as nullable to support backward compat because | ||
// this property was added later. | ||
// If the value is null or not set, it defaulted to Linux. | ||
a.OSType = Linux | ||
} | ||
return nil | ||
} | ||
|
||
// JumpboxProfile dscribes properties of the jumpbox setup | ||
// in the ACS container cluster. | ||
type JumpboxProfile struct { | ||
|
This file contains 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 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 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,35 @@ | ||
package v20170701 | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
) | ||
|
||
func TestMasterProfile(t *testing.T) { | ||
MasterProfileText := "{\"count\" : 0}" | ||
mp := &MasterProfile{} | ||
if e := json.Unmarshal([]byte(MasterProfileText), mp); e != nil { | ||
t.Fatalf("unexpectedly detected unmarshal failure for MasterProfile, %+v", e) | ||
} | ||
|
||
if mp.Count != 1 { | ||
t.Fatalf("unexpectedly detected MasterProfile.Count != 1 after unmarshal") | ||
} | ||
} | ||
|
||
func TestAgentPoolProfile(t *testing.T) { | ||
// With osType not specified | ||
AgentPoolProfileText := "{\"count\" : 0}" | ||
ap := &AgentPoolProfile{} | ||
if e := json.Unmarshal([]byte(AgentPoolProfileText), ap); e != nil { | ||
t.Fatalf("unexpectedly detected unmarshal failure for AgentPoolProfile, %+v", e) | ||
} | ||
|
||
if ap.Count != 1 { | ||
t.Fatalf("unexpectedly detected AgentPoolProfile.Count != 1 after unmarshal") | ||
} | ||
|
||
if ap.OSType != Linux { | ||
t.Fatalf("unexpectedly detected AgentPoolProfile.OSType != Linux after unmarshal") | ||
} | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why use alias?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
otherwise, when you call MasterProfile.UnmarshalJSON, it causes a circular call to itself.
Because we have this, if mm is not the alias, it will be MasterProfile. Here is when circular call starts.