Repository: fatih/gomodifytags
Pull request #61 (2020-05-10 06:51:32)

Add -all flag to select all structs
closes https://github.com/fatih/gomodifytags/issues/60


Patch:
diff --git a/README.md b/README.md
index f871924..b83e7d1 100644
--- a/README.md
+++ b/README.md
@@ -47,7 +47,7 @@ We have to first pass a file. For that we can use the `-file` flag:
 
 ```sh
 $ gomodifytags -file demo.go
--line, -offset or -struct is not passed
+-line, -offset, -struct or -all is not passed
 ```
 
 What are these? There are three different ways of defining **which** field tags
@@ -62,6 +62,7 @@ to change:
   more granular option see `-line`
 * `-line`: This accepts a string that defines the line or lines of which fields
   should be changed. I.e: `-line 4` or `-line 5,8`
+* `-all`: This is a boolean. The `-all` flag selects all structs of the given file.
 
 Let's continue by using the `-struct` tag:
 
diff --git a/main.go b/main.go
index 5bf305a..c40d601 100644
--- a/main.go
+++ b/main.go
@@ -51,6 +51,7 @@ type config struct {
 	structName string
 	line       string
 	start, end int
+	all        bool
 
 	fset *token.FileSet
 
@@ -92,6 +93,7 @@ func realMain() error {
 		flagLine = flag.String("line", "",
 			"Line number of the field or a range of line. i.e: 4 or 4,8")
 		flagStruct = flag.String("struct", "", "Struct name to be processed")
+		flagAll    = flag.Bool("all", false, "Select all structs to be processed")
 
 		// tag flags
 		flagRemoveTags = flag.String("remove-tags", "",
@@ -134,6 +136,7 @@ func realMain() error {
 		line:                 *flagLine,
 		structName:           *flagStruct,
 		offset:               *flagOffset,
+		all:                  *flagAll,
 		output:               *flagOutput,
 		write:                *flagWrite,
 		clear:                *flagClearTags,
@@ -222,8 +225,10 @@ func (c *config) findSelection(node ast.Node) (int, int, error) {
 		return c.offsetSelection(node)
 	} else if c.structName != "" {
 		return c.structSelection(node)
+	} else if c.all {
+		return c.allSelection(node)
 	} else {
-		return 0, 0, errors.New("-line, -offset or -struct is not passed")
+		return 0, 0, errors.New("-line, -offset, -struct or -all is not passed")
 	}
 }
 
@@ -603,6 +608,14 @@ func (c *config) offsetSelection(file ast.Node) (int, int, error) {
 	return start, end, nil
 }
 
+// allSelection selects all structs inside a file
+func (c *config) allSelection(file ast.Node) (int, int, error) {
+	start := 1
+	end := c.fset.File(file.Pos()).LineCount()
+
+	return start, end, nil
+}
+
 func isPublicName(name string) bool {
 	for _, c := range name {
 		return unicode.IsUpper(c)
@@ -691,8 +704,8 @@ func (c *config) validate() error {
 		return errors.New("no file is passed")
 	}
 
-	if c.line == "" && c.offset == 0 && c.structName == "" {
-		return errors.New("-line, -offset or -struct is not passed")
+	if c.line == "" && c.offset == 0 && c.structName == "" && !c.all {
+		return errors.New("-line, -offset, -struct or -all is not passed")
 	}
 
 	if c.line != "" && c.offset != 0 ||


Test patch:
diff --git a/main_test.go b/main_test.go
index b4ee6dc..bf6204e 100644
--- a/main_test.go
+++ b/main_test.go
@@ -391,6 +391,15 @@ func TestRewrite(t *testing.T) {
 				skipUnexportedFields: true,
 			},
 		},
+		{
+			file: "all_structs",
+			cfg: &config{
+				add:       []string{"json"},
+				output:    "source",
+				all:       true,
+				transform: "snakecase",
+			},
+		},
 	}
 
 	for _, ts := range test {
@@ -549,6 +558,13 @@ func TestJSON(t *testing.T) {
 				line: "2,54",
 			},
 		},
+		{
+			file: "json_all_structs",
+			cfg: &config{
+				add: []string{"json"},
+				all: true,
+			},
+		},
 	}
 
 	for _, ts := range test {
diff --git a/test-fixtures/all_structs.golden b/test-fixtures/all_structs.golden
new file mode 100644
index 0000000..4e7e928
--- /dev/null
+++ b/test-fixtures/all_structs.golden
@@ -0,0 +1,22 @@
+package main
+
+type foo struct {
+	bar       string   `json:"bar"`
+	MyExample bool     `json:"my_example"`
+	MyAnother []string `json:"my_another"`
+}
+
+const exampleVar = "foo"
+
+type bar struct {
+	// loose comment
+
+	// home
+	ankara string `json:"ankara"`
+	yeap   bool   `json:"yeap"` // just a boolean
+
+	// great cities
+	cities []string `json:"cities"`
+
+	// seconed loose comment
+}
diff --git a/test-fixtures/all_structs.input b/test-fixtures/all_structs.input
new file mode 100644
index 0000000..a7632a5
--- /dev/null
+++ b/test-fixtures/all_structs.input
@@ -0,0 +1,22 @@
+package main
+
+type foo struct {
+	bar       string
+	MyExample bool
+	MyAnother []string
+}
+
+const exampleVar = "foo"
+
+type bar struct {
+	// loose comment
+
+	// home
+	ankara string
+	yeap   bool // just a boolean
+
+	// great cities
+	cities []string
+
+	// seconed loose comment
+}
diff --git a/test-fixtures/json_all_structs.golden b/test-fixtures/json_all_structs.golden
new file mode 100644
index 0000000..2770fd7
--- /dev/null
+++ b/test-fixtures/json_all_structs.golden
@@ -0,0 +1,28 @@
+{
+  "start": 1,
+  "end": 22,
+  "lines": [
+    "package main",
+    "",
+    "type foo struct {",
+    "\tbar       string   `json:\"bar\"`",
+    "\tMyExample bool     `json:\"myExample\"`",
+    "\tMyAnother []string `json:\"myAnother\"`",
+    "}",
+    "",
+    "const exampleVar = \"foo\"",
+    "",
+    "type bar struct {",
+    "\t// loose comment",
+    "",
+    "\t// home",
+    "\tankara string `json:\"ankara\"`",
+    "\tyeap   bool   `json:\"yeap\"` // just a boolean",
+    "",
+    "\t// great cities",
+    "\tcities []string `json:\"cities\"`",
+    "",
+    "\t// seconed loose comment",
+    "}"
+  ]
+}
\ No newline at end of file
diff --git a/test-fixtures/json_all_structs.input b/test-fixtures/json_all_structs.input
new file mode 100644
index 0000000..a7632a5
--- /dev/null
+++ b/test-fixtures/json_all_structs.input
@@ -0,0 +1,22 @@
+package main
+
+type foo struct {
+	bar       string
+	MyExample bool
+	MyAnother []string
+}
+
+const exampleVar = "foo"
+
+type bar struct {
+	// loose comment
+
+	// home
+	ankara string
+	yeap   bool // just a boolean
+
+	// great cities
+	cities []string
+
+	// seconed loose comment
+}