You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficcontrol.apache.org by de...@apache.org on 2018/04/16 16:35:08 UTC

[incubator-trafficcontrol] branch master updated: Properly unmarshal null DeepCachingType value as "NEVER"

This is an automated email from the ASF dual-hosted git repository.

dewrich pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-trafficcontrol.git


The following commit(s) were added to refs/heads/master by this push:
     new 6fc4c70  Properly unmarshal null DeepCachingType value as "NEVER"
6fc4c70 is described below

commit 6fc4c70d8aacbfff3c846375970c7b924e32bbf5
Author: Rawlin Peters <ra...@comcast.com>
AuthorDate: Fri Apr 13 14:18:56 2018 -0600

    Properly unmarshal null DeepCachingType value as "NEVER"
---
 lib/go-tc/enum.go      |  4 ++++
 lib/go-tc/enum_test.go | 14 ++++++++++++--
 2 files changed, 16 insertions(+), 2 deletions(-)

diff --git a/lib/go-tc/enum.go b/lib/go-tc/enum.go
index da9dcf1..d7991b7 100644
--- a/lib/go-tc/enum.go
+++ b/lib/go-tc/enum.go
@@ -224,6 +224,10 @@ func DeepCachingTypeFromString(s string) DeepCachingType {
 
 // UnmarshalJSON unmarshals a JSON representation of a DeepCachingType (i.e. a string) or returns an error if the DeepCachingType is invalid
 func (t *DeepCachingType) UnmarshalJSON(data []byte) error {
+	if string(data) == "null" {
+		*t = DeepCachingTypeNever
+		return nil
+	}
 	s, err := strconv.Unquote(string(data))
 	if err != nil {
 		return errors.New(string(data) + " JSON not quoted")
diff --git a/lib/go-tc/enum_test.go b/lib/go-tc/enum_test.go
index e11e35b..95fb64a 100644
--- a/lib/go-tc/enum_test.go
+++ b/lib/go-tc/enum_test.go
@@ -63,13 +63,23 @@ func TestDeepCachingType(t *testing.T) {
 
 	// make sure we get the right default when marshalled within a new delivery service
 	var ds DeliveryService
-	txt, err := json.MarshalIndent(ds, ``, `  `)
+	_, err := json.MarshalIndent(ds, ``, `  `)
 	if err != nil {
 		t.Errorf(err.Error())
 	}
-	t.Log(string(txt))
 	c = ds.DeepCachingType.String()
 	if c != "NEVER" {
 		t.Errorf(`Default "%s" expected to be "NEVER"`, c)
 	}
+
+	// make sure null values are handled properly
+	byt := []byte(`{"deepCachingType": null}`)
+	err = json.Unmarshal(byt, &ds)
+	if err != nil {
+		t.Errorf(`Expected to be able to unmarshall a null deepCachingType as "NEVER", instead got error "%v"`, err.Error())
+	}
+	c = ds.DeepCachingType.String()
+	if c != "NEVER" {
+		t.Errorf(`null deepCachingType expected to be "NEVER", got "%s"`, c)
+	}
 }

-- 
To stop receiving notification emails like this one, please contact
dewrich@apache.org.