Skip to content
This repository has been archived by the owner on Jan 11, 2023. It is now read-only.

populate default value in unmarshal #939

Merged
merged 4 commits into from
Jul 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions pkg/api/v20160330/types.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package v20160330

import (
"encoding/json"
"fmt"
neturl "net/url"
"strings"
Expand Down Expand Up @@ -102,6 +103,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 {
// 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.
Expand All @@ -116,6 +134,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 {
Expand Down
43 changes: 43 additions & 0 deletions pkg/api/v20160930/types.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package v20160930

import (
"encoding/json"
"fmt"
neturl "net/url"
"strings"
Expand Down Expand Up @@ -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 {
// 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.
Expand All @@ -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 {
Expand Down
30 changes: 30 additions & 0 deletions pkg/api/v20160930/types_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package v20160930

import (
"encoding/json"
"testing"
)

Expand All @@ -18,3 +19,32 @@ func TestIsDCOS(t *testing.T) {
t.Fatalf("unexpectedly detected DCOS orchestrator profile from OrchestratorType=%s", kubernetesProfile.OrchestratorType)
}
}

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")
}
}
43 changes: 43 additions & 0 deletions pkg/api/v20170131/types.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package v20170131

import (
"encoding/json"
"fmt"
neturl "net/url"
"strings"
Expand Down Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

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

why use alias?

Copy link
Contributor Author

@rjtsdl rjtsdl Jul 7, 2017

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.

if e := json.Unmarshal(b, &mm); e != nil {
		return e
}

mm := aliasMasterProfile{}
if e := json.Unmarshal(b, &mm); e != nil {
return e
}
*m = MasterProfile(mm)
if m.Count == 0 {
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.

Copy link
Contributor

Choose a reason for hiding this comment

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

Does json.Unmarshal fill the empty field with 0?

Copy link
Contributor Author

@rjtsdl rjtsdl Jul 7, 2017

Choose a reason for hiding this comment

The 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.
Expand All @@ -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 {
Expand Down
30 changes: 30 additions & 0 deletions pkg/api/v20170131/types_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package v20170131

import (
"encoding/json"
"testing"
)

Expand All @@ -18,3 +19,32 @@ func TestIsDCOS(t *testing.T) {
t.Fatalf("unexpectedly detected DCOS orchestrator profile from OrchestratorType=%s", kubernetesProfile.OrchestratorType)
}
}

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")
}
}
50 changes: 50 additions & 0 deletions pkg/api/v20170701/types.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package v20170701

import (
"encoding/json"
"fmt"
"strings"
)
Expand Down Expand Up @@ -123,6 +124,27 @@ type MasterProfile struct {
FQDN string `json:"fqdn,omitempty"`
}

// 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 {
// if MasterProfile.Count is missing or 0, set to default 1
m.Count = 1
}

// TODO:
// Need to set default values for
// OSDiskSizeGB, FirstConsecutiveStaticIP, StorageProfile
return nil
}

// AgentPoolProfile represents configuration of VMs running agent
// daemons that register with the master and offer resources to
// host applications in containers.
Expand All @@ -146,6 +168,34 @@ 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
}
// TODO:
// Need to set default values for
// OSDiskSizeGB, StorageProfile
return nil
}

// OrchestratorType defines orchestrators supported by ACS
type OrchestratorType string

Expand Down
35 changes: 35 additions & 0 deletions pkg/api/v20170701/types_test.go
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")
}
}