You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@trafficcontrol.apache.org by GitBox <gi...@apache.org> on 2018/04/16 16:35:06 UTC

[GitHub] dewrich closed pull request #2136: Properly unmarshal null DeepCachingType value as "NEVER"

dewrich closed pull request #2136: Properly unmarshal null DeepCachingType value as "NEVER"
URL: https://github.com/apache/incubator-trafficcontrol/pull/2136
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/lib/go-tc/enum.go b/lib/go-tc/enum.go
index da9dcf173..d7991b727 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 e11e35b19..95fb64ac0 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)
+	}
 }


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services