Repository: knative/pkg
Pull request #1849 (2020-10-26 22:52:16)

Final Json -> JSON process
I think there's no more

/assign @tcnghia 

knative-prow-robot: [APPROVALNOTIFIER] This PR is **APPROVED**

This pull-request has been approved by: *<a href="https://github.com/knative/pkg/pull/1849#" title="Author self-approved">vagababov</a>*

The full list of commands accepted by this bot can be found [here](https://go.k8s.io/bot-commands?repo=knative%2Fpkg).

The pull request process is described [here](https://git.k8s.io/community/contributors/guide/owners.md#the-code-review-process)

<details >
Needs approval from an approver in each of these files:

- ~~[apis/OWNERS](https://github.com/knative/pkg/blob/master/apis/OWNERS)~~ [vagababov]
- ~~[tracing/config/OWNERS](https://github.com/knative/pkg/blob/master/tracing/config/OWNERS)~~ [vagababov]

Approvers can indicate their approval by writing `/approve` in a comment
Approvers can cancel approval by writing `/approve cancel` in a comment
</details>
<!-- META={"approvers":[]} -->
knative-prow-robot: [APPROVALNOTIFIER] This PR is **APPROVED**

This pull-request has been approved by: *<a href="https://github.com/knative/pkg/pull/1849#pullrequestreview-517245825" title="LGTM">tcnghia</a>*, *<a href="https://github.com/knative/pkg/pull/1849#" title="Author self-approved">vagababov</a>*

The full list of commands accepted by this bot can be found [here](https://go.k8s.io/bot-commands?repo=knative%2Fpkg).

The pull request process is described [here](https://git.k8s.io/community/contributors/guide/owners.md#the-code-review-process)

<details >
Needs approval from an approver in each of these files:

- ~~[apis/OWNERS](https://github.com/knative/pkg/blob/master/apis/OWNERS)~~ [tcnghia,vagababov]
- ~~[tracing/config/OWNERS](https://github.com/knative/pkg/blob/master/tracing/config/OWNERS)~~ [tcnghia,vagababov]

Approvers can indicate their approval by writing `/approve` in a comment
Approvers can cancel approval by writing `/approve cancel` in a comment
</details>
<!-- META={"approvers":[]} -->

Patch:
diff --git a/tracing/config/tracing.go b/tracing/config/tracing.go
index 14f7aa435..0f635f42a 100644
--- a/tracing/config/tracing.go
+++ b/tracing/config/tracing.go
@@ -137,7 +137,12 @@ func NewTracingConfigFromConfigMap(config *corev1.ConfigMap) (*Config, error) {
 // JsonToTracingConfig converts a json string of a Config.
 // Returns a non-nil Config always and an eventual error.
 func JsonToTracingConfig(jsonCfg string) (*Config, error) { //nolint // for backcompat.
+	return JSONToTracingConfig(jsonCfg)
+}
 
+// JSONToTracingConfig converts a json string of a Config.
+// Returns a non-nil Config always and an eventual error.
+func JSONToTracingConfig(jsonCfg string) (*Config, error) {
 	if jsonCfg == "" {
 		return NoopConfig(), errors.New("empty json tracing config")
 	}
@@ -156,6 +161,10 @@ func JsonToTracingConfig(jsonCfg string) (*Config, error) { //nolint // for back
 
 // TracingConfigToJson converts a Config to a json string.
 func TracingConfigToJson(cfg *Config) (string, error) { //nolint // for backcompat.
+	return TracingConfigToJSON(cfg)
+}
+
+func TracingConfigToJSON(cfg *Config) (string, error) { //nolint // for backcompat.
 	if cfg == nil {
 		return "", nil
 	}


Test patch:
diff --git a/apis/url_test.go b/apis/url_test.go
index 39f91c10e..56966384f 100644
--- a/apis/url_test.go
+++ b/apis/url_test.go
@@ -25,47 +25,47 @@ import (
 )
 
 func TestParseURL(t *testing.T) {
-	testCases := map[string]struct {
+	testCases := []struct {
+		name      string
 		t         string
 		want      *URL
 		wantEmpty bool
 		wantErr   bool
-	}{
-		"empty string": {
-			want:      nil,
-			wantEmpty: true,
-		},
-		"invalid format": {
-			t:         "💩://error",
-			want:      nil,
-			wantEmpty: true,
-			wantErr:   true,
-		},
-		"relative": {
-			t: "/path/to/something",
-			want: &URL{
-				Path: "/path/to/something",
-			},
-		},
-		"url": {
-			t: "http://path/to/something",
-			want: &URL{
-				Scheme: "http",
-				Host:   "path",
-				Path:   "/to/something",
-			},
-		},
-		"simplehttp": {
-			t:    "http://foo",
-			want: HTTP("foo"),
-		},
-		"simplehttps": {
-			t:    "https://foo",
-			want: HTTPS("foo"),
-		},
-	}
-	for n, tc := range testCases {
-		t.Run(n, func(t *testing.T) {
+	}{{
+		name:      "empty string",
+		want:      nil,
+		wantEmpty: true,
+	}, {
+		name:      "invalid format",
+		t:         "💩://error",
+		want:      nil,
+		wantEmpty: true,
+		wantErr:   true,
+	}, {
+		name: "relative",
+		t:    "/path/to/something",
+		want: &URL{
+			Path: "/path/to/something",
+		},
+	}, {
+		name: "url",
+		t:    "http://path/to/something",
+		want: &URL{
+			Scheme: "http",
+			Host:   "path",
+			Path:   "/to/something",
+		},
+	}, {
+		name: "simplehttp",
+		t:    "http://foo",
+		want: HTTP("foo"),
+	}, {
+		name: "simplehttps",
+		t:    "https://foo",
+		want: HTTPS("foo"),
+	}}
+	for _, tc := range testCases {
+		t.Run(tc.name, func(t *testing.T) {
 
 			got, err := ParseURL(tc.t)
 			if err != nil {
@@ -82,33 +82,33 @@ func TestParseURL(t *testing.T) {
 			}
 
 			if diff := cmp.Diff(tc.want, got); diff != "" {
-				t.Error("unexpected object (-want, +got) =", diff)
+				t.Errorf("Unexpected object (-want, +got) =\n%s", diff)
 			}
 		})
 	}
 }
 
-func TestJsonMarshalURL(t *testing.T) {
-	testCases := map[string]struct {
+func TestJSONMarshalURL(t *testing.T) {
+	testCases := []struct {
+		name string
 		t    string
 		want []byte
-	}{
-		"empty": {},
-		"empty string": {
-			t: "",
-		},
-		"invalid url": {
-			t:    "not a url",
-			want: []byte(`"not%20a%20url"`),
-		},
-		"relative format": {
-			t:    "/path/to/something",
-			want: []byte(`"/path/to/something"`),
-		},
-	}
-	for n, tc := range testCases {
-		t.Run(n, func(t *testing.T) {
-
+	}{{
+		name: "empty",
+	}, {
+		name: "empty string",
+		t:    "",
+	}, {
+		name: "invalid url",
+		t:    "not a url",
+		want: []byte(`"not%20a%20url"`),
+	}, {
+		name: "relative format",
+		t:    "/path/to/something",
+		want: []byte(`"/path/to/something"`),
+	}}
+	for _, tc := range testCases {
+		t.Run(tc.name, func(t *testing.T) {
 			var got []byte
 			tt, err := ParseURL(tc.t)
 			if err != nil {
@@ -120,13 +120,13 @@ func TestJsonMarshalURL(t *testing.T) {
 
 			if diff := cmp.Diff(tc.want, got); diff != "" {
 				t.Log("got:", string(got))
-				t.Error("unexpected object (-want, +got) =", diff)
+				t.Errorf("unexpected object (-want, +got) =\n%s", diff)
 			}
 		})
 	}
 }
 
-func TestJsonUnmarshalURL(t *testing.T) {
+func TestJSONUnmarshalURL(t *testing.T) {
 	testCases := map[string]struct {
 		b       []byte
 		want    *URL
@@ -178,8 +178,7 @@ func TestJsonUnmarshalURL(t *testing.T) {
 	}
 }
 
-func TestJsonMarshalURLAsMember(t *testing.T) {
-
+func TestJSONMarshalURLAsMember(t *testing.T) {
 	type objectType struct {
 		URL URL `json:"url,omitempty"`
 	}
@@ -237,12 +236,11 @@ func TestJsonMarshalURLAsMember(t *testing.T) {
 	}
 }
 
-func TestJsonMarshalURLAsPointerMember(t *testing.T) {
-
-	type objectType struct {
-		URL *URL `json:"url,omitempty"`
-	}
+type objectType struct {
+	URL *URL `json:"url,omitempty"`
+}
 
+func TestJSONMarshalURLAsPointerMember(t *testing.T) {
 	testCases := map[string]struct {
 		obj     *objectType
 		want    []byte
@@ -296,12 +294,10 @@ func TestJsonMarshalURLAsPointerMember(t *testing.T) {
 	}
 }
 
-func TestJsonUnmarshalURLAsMember(t *testing.T) {
-
+func TestJSONUnmarshalURLAsMember(t *testing.T) {
 	type objectType struct {
 		URL URL `json:"url,omitempty"`
 	}
-
 	testCases := map[string]struct {
 		b       []byte
 		want    *objectType
@@ -359,47 +355,46 @@ func TestJsonUnmarshalURLAsMember(t *testing.T) {
 	}
 }
 
-func TestJsonUnmarshalURLAsMemberPointer(t *testing.T) {
-
+func TestJSONUnmarshalURLAsMemberPointer(t *testing.T) {
 	type objectType struct {
 		URL *URL `json:"url,omitempty"`
 	}
-
-	testCases := map[string]struct {
+	testCases := []struct {
+		name    string
 		b       []byte
 		want    *objectType
 		wantErr string
-	}{
-		"zero": {
-			wantErr: "unexpected end of JSON input",
-		},
-		"empty": {
-			b:    []byte(`{}`),
-			want: &objectType{},
-		},
-		"invalid format": {
-			b:       []byte(`{"url":"%"}`),
-			wantErr: `invalid URL escape "%"`,
-		},
-		"relative": {
-			b:    []byte(`{"url":"/path/to/something"}`),
-			want: &objectType{URL: &URL{Path: "/path/to/something"}},
-		},
-		"url": {
-			b: []byte(`{"url":"http://path/to/something"}`),
-			want: &objectType{URL: &URL{
-				Scheme: "http",
-				Host:   "path",
-				Path:   "/to/something",
-			}},
-		},
-		"empty url": {
-			b:    []byte(`{"url":""}`),
-			want: &objectType{URL: &URL{}},
-		},
+	}{{
+		name:    "zero",
+		wantErr: "unexpected end of JSON input",
+	}, {
+		name: "empty",
+		b:    []byte(`{}`),
+		want: &objectType{},
+	}, {
+		name:    "invalid format",
+		b:       []byte(`{"url":"%"}`),
+		wantErr: `invalid URL escape "%"`,
+	}, {
+		name: "relative",
+		b:    []byte(`{"url":"/path/to/something"}`),
+		want: &objectType{URL: &URL{Path: "/path/to/something"}},
+	}, {
+		name: "url",
+		b:    []byte(`{"url":"http://path/to/something"}`),
+		want: &objectType{URL: &URL{
+			Scheme: "http",
+			Host:   "path",
+			Path:   "/to/something",
+		}},
+	}, {
+		name: "empty url",
+		b:    []byte(`{"url":""}`),
+		want: &objectType{URL: &URL{}},
+	},
 	}
-	for n, tc := range testCases {
-		t.Run(n, func(t *testing.T) {
+	for _, tc := range testCases {
+		t.Run(tc.name, func(t *testing.T) {
 
 			got := &objectType{}
 			err := json.Unmarshal(tc.b, got)
diff --git a/tracing/config/tracing_test.go b/tracing/config/tracing_test.go
index 8251859b1..e30e9f531 100644
--- a/tracing/config/tracing_test.go
+++ b/tracing/config/tracing_test.go
@@ -137,7 +137,7 @@ func TestNewConfigSuccess(t *testing.T) {
 	}
 }
 
-func TestNewConfigJson(t *testing.T) {
+func TestNewConfigJSON(t *testing.T) {
 	tt := []struct {
 		name   string
 		input  map[string]string
@@ -216,12 +216,12 @@ func TestNewConfigJson(t *testing.T) {
 				t.Fatal("Failed to create tracing config:", err)
 			}
 
-			json, err := TracingConfigToJson(config)
+			json, err := TracingConfigToJSON(config)
 			if err != nil {
 				t.Fatal("Failed to create tracing config:", err)
 			}
 
-			haveConfig, err := JsonToTracingConfig(json)
+			haveConfig, err := JSONToTracingConfig(json)
 			if err != nil {
 				t.Fatal("Failed to create tracing config:", err)
 			}