Repository: fatih/gomodifytags
Pull request #56 (2020-02-21 09:46:58)

Fix value parsing
Hello, 
I just try to fix the issue #52 .

@fatih  Can you please check it out.


Patch:
diff --git a/main.go b/main.go
index b01b95c..7fe6e38 100644
--- a/main.go
+++ b/main.go
@@ -392,10 +392,10 @@ func (c *config) addTags(fieldName string, tags *structtag.Tags) (*structtag.Tag
 	}
 
 	for _, key := range c.add {
-		splitted = strings.Split(key, ":")
-		if len(splitted) == 2 {
+		splitted = strings.SplitN(key, ":", 2)
+		if len(splitted) >= 2 {
 			key = splitted[0]
-			name = splitted[1]
+			name = strings.Join(splitted[1:], "")
 		} else if unknown {
 			// the user didn't pass any value but want to use an unknown
 			// transform. We don't return above in the default as the user


Test patch:
diff --git a/main_test.go b/main_test.go
index fdeea58..727760b 100644
--- a/main_test.go
+++ b/main_test.go
@@ -84,6 +84,46 @@ func TestRewrite(t *testing.T) {
 				override:  true,
 			},
 		},
+		{
+			file: "line_add_override_column",
+			cfg: &config{
+				add:       []string{"json:MyBar:bar"},
+				output:    "source",
+				line:      "4,4",
+				transform: "snakecase",
+				override:  true,
+			},
+		},
+		{
+			file: "line_add_override_mixed_column_and_equal",
+			cfg: &config{
+				add:       []string{"json:MyBar:bar:foo=qux"},
+				output:    "source",
+				line:      "4,4",
+				transform: "snakecase",
+				override:  true,
+			},
+		},
+		{
+			file: "line_add_override_multi_equal",
+			cfg: &config{
+				add:       []string{"json:MyBar=bar=foo"},
+				output:    "source",
+				line:      "4,4",
+				transform: "snakecase",
+				override:  true,
+			},
+		},
+		{
+			file: "line_add_override_multi_column",
+			cfg: &config{
+				add:       []string{"json:MyBar:bar:foo"},
+				output:    "source",
+				line:      "4,4",
+				transform: "snakecase",
+				override:  true,
+			},
+		},
 		{
 			file: "line_add_no_override",
 			cfg: &config{
diff --git a/test-fixtures/line_add_override_column.golden b/test-fixtures/line_add_override_column.golden
new file mode 100644
index 0000000..5075271
--- /dev/null
+++ b/test-fixtures/line_add_override_column.golden
@@ -0,0 +1,6 @@
+package foo
+
+type foo struct {
+	bar string `json:"MyBar:bar"`
+	baz string `json:"baz"`
+}
diff --git a/test-fixtures/line_add_override_column.input b/test-fixtures/line_add_override_column.input
new file mode 100644
index 0000000..33a2afd
--- /dev/null
+++ b/test-fixtures/line_add_override_column.input
@@ -0,0 +1,6 @@
+package foo
+
+type foo struct {
+	bar string `json:"bar"`
+	baz string `json:"baz"`
+}
diff --git a/test-fixtures/line_add_override_mixed_column_and_equal.golden b/test-fixtures/line_add_override_mixed_column_and_equal.golden
new file mode 100644
index 0000000..0872a23
--- /dev/null
+++ b/test-fixtures/line_add_override_mixed_column_and_equal.golden
@@ -0,0 +1,6 @@
+package foo
+
+type foo struct {
+	bar string `json:"MyBar:bar:foo=qux"`
+	baz string `json:"baz"`
+}
diff --git a/test-fixtures/line_add_override_mixed_column_and_equal.input b/test-fixtures/line_add_override_mixed_column_and_equal.input
new file mode 100644
index 0000000..33a2afd
--- /dev/null
+++ b/test-fixtures/line_add_override_mixed_column_and_equal.input
@@ -0,0 +1,6 @@
+package foo
+
+type foo struct {
+	bar string `json:"bar"`
+	baz string `json:"baz"`
+}
diff --git a/test-fixtures/line_add_override_multi_column.golden b/test-fixtures/line_add_override_multi_column.golden
new file mode 100644
index 0000000..481494c
--- /dev/null
+++ b/test-fixtures/line_add_override_multi_column.golden
@@ -0,0 +1,6 @@
+package foo
+
+type foo struct {
+	bar string `json:"MyBar:bar:foo"`
+	baz string `json:"baz"`
+}
diff --git a/test-fixtures/line_add_override_multi_column.input b/test-fixtures/line_add_override_multi_column.input
new file mode 100644
index 0000000..33a2afd
--- /dev/null
+++ b/test-fixtures/line_add_override_multi_column.input
@@ -0,0 +1,6 @@
+package foo
+
+type foo struct {
+	bar string `json:"bar"`
+	baz string `json:"baz"`
+}
diff --git a/test-fixtures/line_add_override_multi_equal.golden b/test-fixtures/line_add_override_multi_equal.golden
new file mode 100644
index 0000000..3459420
--- /dev/null
+++ b/test-fixtures/line_add_override_multi_equal.golden
@@ -0,0 +1,6 @@
+package foo
+
+type foo struct {
+	bar string `json:"MyBar=bar=foo"`
+	baz string `json:"baz"`
+}
diff --git a/test-fixtures/line_add_override_multi_equal.input b/test-fixtures/line_add_override_multi_equal.input
new file mode 100644
index 0000000..33a2afd
--- /dev/null
+++ b/test-fixtures/line_add_override_multi_equal.input
@@ -0,0 +1,6 @@
+package foo
+
+type foo struct {
+	bar string `json:"bar"`
+	baz string `json:"baz"`
+}