Repository: k1LoW/tbls
Pull request #327 (2021-12-09 22:08:41)

Add `comments.ColumnLabels:`


Patch:
diff --git a/README.md b/README.md
index 660c0a8..40385d3 100644
--- a/README.md
+++ b/README.md
@@ -348,6 +348,19 @@ comments:
       - privacy data
 ```
 
+**label columns:**
+
+``` yaml
+# .tbls.yml
+comments:
+  -
+    table: users
+    columnLabels:
+      email:
+        - secure
+        - encrypted
+```
+
 ### DSN
 
 `dsn:` (Data Source Name) is used to connect to database.
diff --git a/config/config.go b/config/config.go
index 2363da5..ed61fbd 100644
--- a/config/config.go
+++ b/config/config.go
@@ -85,13 +85,14 @@ type AdditionalRelation struct {
 
 // AdditionalComment is the struct for table relation from yaml
 type AdditionalComment struct {
-	Table              string            `yaml:"table"`
-	TableComment       string            `yaml:"tableComment,omitempty"`
-	ColumnComments     map[string]string `yaml:"columnComments,omitempty"`
-	IndexComments      map[string]string `yaml:"indexComments,omitempty"`
-	ConstraintComments map[string]string `yaml:"constraintComments,omitempty"`
-	TriggerComments    map[string]string `yaml:"triggerComments,omitempty"`
-	Labels             []string          `yaml:"labels,omitempty"`
+	Table              string              `yaml:"table"`
+	TableComment       string              `yaml:"tableComment,omitempty"`
+	ColumnComments     map[string]string   `yaml:"columnComments,omitempty"`
+	ColumnLabels       map[string][]string `yaml:"columnLabels,omitempty"`
+	IndexComments      map[string]string   `yaml:"indexComments,omitempty"`
+	ConstraintComments map[string]string   `yaml:"constraintComments,omitempty"`
+	TriggerComments    map[string]string   `yaml:"triggerComments,omitempty"`
+	Labels             []string            `yaml:"labels,omitempty"`
 }
 
 type DetectVirtualRelations struct {
@@ -508,6 +509,15 @@ func mergeAdditionalComments(s *schema.Schema, comments []AdditionalComment) err
 			}
 			column.Comment = comment
 		}
+		for c, labels := range c.ColumnLabels {
+			column, err := table.FindColumnByName(c)
+			if err != nil {
+				return errors.Wrap(err, "failed to add column comment")
+			}
+			for _, l := range labels {
+				column.Labels = column.Labels.Merge(l)
+			}
+		}
 		for i, comment := range c.IndexComments {
 			index, err := table.FindIndexByName(i)
 			if err != nil {
diff --git a/schema/schema.go b/schema/schema.go
index 4d1664a..3298192 100644
--- a/schema/schema.go
+++ b/schema/schema.go
@@ -66,6 +66,7 @@ type Column struct {
 	Default         sql.NullString `json:"default"`
 	Comment         string         `json:"comment"`
 	ExtraDef        string         `json:"extra_def,omitempty" yaml:"extraDef,omitempty"`
+	Labels          Labels         `json:"labels,omitempty"`
 	ParentRelations []*Relation    `json:"-"`
 	ChildRelations  []*Relation    `json:"-"`
 }


Test patch:
diff --git a/config/config_test.go b/config/config_test.go
index be5f83e..726294a 100644
--- a/config/config_test.go
+++ b/config/config_test.go
@@ -37,8 +37,8 @@ func TestLoadDefault(t *testing.T) {
 }
 
 func TestLoadConfigFile(t *testing.T) {
-	_ = os.Setenv("TBLS_TEST_PG_PASS", "pgpass")
-	_ = os.Setenv("TBLS_TEST_PG_DOC_PATH", "sample/pg")
+	t.Setenv("TBLS_TEST_PG_PASS", "pgpass")
+	t.Setenv("TBLS_TEST_PG_DOC_PATH", "sample/pg")
 	configFilepath := filepath.Join(testdataDir(), "config_test_tbls_2.yml")
 	config, err := New()
 	if err != nil {
@@ -393,6 +393,9 @@ func TestModifySchema(t *testing.T) {
 	if want := "post title"; title.Comment != want {
 		t.Errorf("got %v\nwant %v", title.Comment, want)
 	}
+	if want := 2; len(title.Labels) != want {
+		t.Errorf("got %v\nwant %v", len(title.Labels), want)
+	}
 	if want := 2; len(s.Tables) != want {
 		t.Errorf("got %v\nwant %v", len(s.Tables), want)
 	}
diff --git a/testdata/config_test_tbls.yml b/testdata/config_test_tbls.yml
index 57e21c9..acc885f 100644
--- a/testdata/config_test_tbls.yml
+++ b/testdata/config_test_tbls.yml
@@ -26,5 +26,9 @@ comments:
     table: posts
     columnComments:
       title: post title
+    columnLabels:
+      title:
+        - string
+        - required
     triggerComments:
       update_posts_title: update posts title