Skip to content

Commit

Permalink
removed bool flag from index Add method and created separate AddUniqu…
Browse files Browse the repository at this point in the history
…e method
  • Loading branch information
Erik Aigner committed Jan 24, 2013
1 parent e483064 commit f13af41
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 32 deletions.
6 changes: 3 additions & 3 deletions README.md
Expand Up @@ -91,8 +91,8 @@ type Person struct {
// polluting the table fields.

func (table *Person) Indexes(indexes *hood.Indexes) {
indexes.Add("tag_index", false, "tag") // params: indexName, unique, columns...
indexes.Add("name_index", true, "first_name", "last_name")
indexes.Add("tag_index", "tag") // params: indexName, unique, columns...
indexes.AddUnique("name_index", "first_name", "last_name")
}
```

Expand Down Expand Up @@ -198,7 +198,7 @@ type Users struct {
}

func (table *Users) Indexes(indexes *hood.Indexes) {
indexes.Add("name_index", true, "first", "last")
indexes.AddUnique("name_index", "first", "last")
}
```

Expand Down
2 changes: 1 addition & 1 deletion dialects_test.go
Expand Up @@ -624,7 +624,7 @@ type CreateTableTestModel struct {
}

func (table *CreateTableTestModel) Indexes(indexes *Indexes) {
indexes.Add("create_table_test_model_index", true, "first", "last")
indexes.AddUnique("create_table_test_model_index", "first", "last")
}

func DoTestCreateTable(t *testing.T, info dialectInfo) {
Expand Down
19 changes: 14 additions & 5 deletions hood.go
Expand Up @@ -145,9 +145,14 @@ const (

type Join int

// Add appends an index to the array
func (ix *Indexes) Add(name string, unique bool, columns ...string) {
*ix = append(*ix, &Index{Name: name, Columns: columns, Unique: unique})
// Add adds an index
func (ix *Indexes) Add(name string, columns ...string) {
*ix = append(*ix, &Index{Name: name, Columns: columns, Unique: false})
}

// AddUnique adds an unique index
func (ix *Indexes) AddUnique(name string, columns ...string) {
*ix = append(*ix, &Index{Name: name, Columns: columns, Unique: true})
}

// Quote quotes the path using the given dialects Quote method
Expand Down Expand Up @@ -336,10 +341,14 @@ func validateRegexp(s, reg, field string) error {
}

func (index *Index) GoDeclaration() string {
u := ""
if index.Unique {
u = "Unique"
}
return fmt.Sprintf(
"indexes.Add(\"%s\", %t, \"%s\")",
"indexes.Add%s(\"%s\", \"%s\")",
u,
index.Name,
index.Unique,
strings.Join(index.Columns, "\", \""),
)
}
Expand Down
46 changes: 23 additions & 23 deletions hood_test.go
Expand Up @@ -214,8 +214,8 @@ type indexedTable struct {
}

func (table *indexedTable) Indexes(indexes *Indexes) {
indexes.Add("my_index", false, "col_primary", "col_time")
indexes.Add("my_unique_index", true, "col_var_char", "col_time")
indexes.Add("my_index", "col_primary", "col_time")
indexes.AddUnique("my_unique_index", "col_var_char", "col_time")
}

func TestInterfaceToModel(t *testing.T) {
Expand Down Expand Up @@ -319,7 +319,7 @@ type TestSchemaGenerationUserTable struct {
}

func (table *TestSchemaGenerationUserTable) Indexes(indexes *Indexes) {
indexes.Add("name_index", true, "first", "last")
indexes.AddUnique("name_index", "first", "last")
}

func TestSchemaGeneration(t *testing.T) {
Expand All @@ -335,11 +335,11 @@ func TestSchemaGeneration(t *testing.T) {
"\tLast\tstring\n" +
"}\n" +
"\n" +
"func (table *TestSchemaGenerationUserTable) Indexes(indexes *Indexes) {\n" +
"\tindexes.Add(\"name_index\", true, \"first\", \"last\"),\n" +
"func (table *TestSchemaGenerationUserTable) Indexes(indexes *hood.Indexes) {\n" +
"\tindexes.AddUnique(\"name_index\", \"first\", \"last\")\n" +
"}"
if x := hd.schema.GoDeclaration(); x != decl1 {
t.Fatalf("invalid schema\n%s\n---\n%s", x, decl1)
t.Fatalf("invalid schema\n%s\n---\n%s", makeWhitespaceVisible(x), makeWhitespaceVisible(decl1))
}
type DropMe struct {
Id Id
Expand All @@ -351,8 +351,8 @@ func TestSchemaGeneration(t *testing.T) {
"\tLast\tstring\n" +
"}\n" +
"\n" +
"func (table *TestSchemaGenerationUserTable) Indexes(indexes *Indexes) {\n" +
"\tindexes.Add(\"name_index\", true, \"first\", \"last\"),\n" +
"func (table *TestSchemaGenerationUserTable) Indexes(indexes *hood.Indexes) {\n" +
"\tindexes.AddUnique(\"name_index\", \"first\", \"last\")\n" +
"}\n" +
"\n" +
"type DropMe struct {\n" +
Expand All @@ -372,8 +372,8 @@ func TestSchemaGeneration(t *testing.T) {
"\tLast\tstring\n" +
"}\n" +
"\n" +
"func (table *Customers) Indexes(indexes *Indexes) {\n" +
"\tindexes.Add(\"name_index\", true, \"first\", \"last\"),\n" +
"func (table *Customers) Indexes(indexes *hood.Indexes) {\n" +
"\tindexes.AddUnique(\"name_index\", \"first\", \"last\")\n" +
"}"
if x := hd.schema.GoDeclaration(); x != decl3 {
t.Fatalf("invalid schema\n%s\n\n%s", makeWhitespaceVisible(x), makeWhitespaceVisible(decl3))
Expand All @@ -388,8 +388,8 @@ func TestSchemaGeneration(t *testing.T) {
"\tBalance\tint\n" +
"}\n" +
"\n" +
"func (table *Customers) Indexes(indexes *Indexes) {\n" +
"\tindexes.Add(\"name_index\", true, \"first\", \"last\"),\n" +
"func (table *Customers) Indexes(indexes *hood.Indexes) {\n" +
"\tindexes.AddUnique(\"name_index\", \"first\", \"last\")\n" +
"}"
if x := hd.schema.GoDeclaration(); x != decl4 {
t.Fatalf("invalid schema\n%s\n\n%s", makeWhitespaceVisible(x), makeWhitespaceVisible(decl4))
Expand All @@ -402,8 +402,8 @@ func TestSchemaGeneration(t *testing.T) {
"\tAmount\tint\n" +
"}\n" +
"\n" +
"func (table *Customers) Indexes(indexes *Indexes) {\n" +
"\tindexes.Add(\"name_index\", true, \"first\", \"last\"),\n" +
"func (table *Customers) Indexes(indexes *hood.Indexes) {\n" +
"\tindexes.AddUnique(\"name_index\", \"first\", \"last\")\n" +
"}"
if x := hd.schema.GoDeclaration(); x != decl5 {
t.Fatalf("invalid schema\n%s\n\n%s", makeWhitespaceVisible(x), makeWhitespaceVisible(decl5))
Expand All @@ -418,8 +418,8 @@ func TestSchemaGeneration(t *testing.T) {
"\tAmount\tstring\n" +
"}\n" +
"\n" +
"func (table *Customers) Indexes(indexes *Indexes) {\n" +
"\tindexes.Add(\"name_index\", true, \"first\", \"last\"),\n" +
"func (table *Customers) Indexes(indexes *hood.Indexes) {\n" +
"\tindexes.AddUnique(\"name_index\", \"first\", \"last\")\n" +
"}"
if x := hd.schema.GoDeclaration(); x != decl6 {
t.Fatalf("invalid schema\n%s\n\n%s", makeWhitespaceVisible(x), makeWhitespaceVisible(decl6))
Expand All @@ -433,8 +433,8 @@ func TestSchemaGeneration(t *testing.T) {
"\tAmount\tstring\n" +
"}\n" +
"\n" +
"func (table *Customers) Indexes(indexes *Indexes) {\n" +
"\tindexes.Add(\"name_index\", true, \"first\", \"last\"),\n" +
"func (table *Customers) Indexes(indexes *hood.Indexes) {\n" +
"\tindexes.AddUnique(\"name_index\", \"first\", \"last\")\n" +
"}"
if x := hd.schema.GoDeclaration(); x != decl7 {
t.Fatalf("invalid schema\n%s\n\n%s", makeWhitespaceVisible(x), makeWhitespaceVisible(decl7))
Expand All @@ -445,9 +445,9 @@ func TestSchemaGeneration(t *testing.T) {
"\tAmount\tstring\n" +
"}\n" +
"\n" +
"func (table *Customers) Indexes(indexes *Indexes) {\n" +
"\tindexes.Add(\"name_index\", true, \"first\", \"last\"),\n" +
"\tindexes.Add(\"amount_index\", false, \"amount\"),\n" +
"func (table *Customers) Indexes(indexes *hood.Indexes) {\n" +
"\tindexes.AddUnique(\"name_index\", \"first\", \"last\")\n" +
"\tindexes.Add(\"amount_index\", \"amount\")\n" +
"}"
if x := hd.schema.GoDeclaration(); x != decl8 {
t.Fatalf("invalid schema\n%s\n\n%s", makeWhitespaceVisible(x), makeWhitespaceVisible(decl8))
Expand All @@ -458,8 +458,8 @@ func TestSchemaGeneration(t *testing.T) {
"\tAmount\tstring\n" +
"}\n" +
"\n" +
"func (table *Customers) Indexes(indexes *Indexes) {\n" +
"\tindexes.Add(\"amount_index\", false, \"amount\"),\n" +
"func (table *Customers) Indexes(indexes *hood.Indexes) {\n" +
"\tindexes.Add(\"amount_index\", \"amount\")\n" +
"}"
if x := hd.schema.GoDeclaration(); x != decl9 {
t.Fatalf("invalid schema\n%s\n\n%s", makeWhitespaceVisible(x), makeWhitespaceVisible(decl9))
Expand Down

0 comments on commit f13af41

Please sign in to comment.