Skip to content

Commit

Permalink
Add String() methods to entity types (#432)
Browse files Browse the repository at this point in the history
* Add String() methods to entity types

* update CHANGELOG
  • Loading branch information
phliar authored Mar 12, 2020
1 parent 139c3e1 commit b44ff93
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 2 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
# Changelog

## v3.4.20 (unreleased)
## v3.4.21 (unreleased)
- Nothing changed yet

## v3.4.20 (2020-03-12)
- Added String() methods to entity structs

## v3.4.19 (2020-02-19)
- Add Docstore reserved words to the exclusion list.
- Fix mem connector bug: with no clustering keys, Remove should delete from any Indexes.
Expand Down
4 changes: 4 additions & 0 deletions connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ type EntityInfo struct {
TTL *time.Duration
}

func (ei *EntityInfo) String() string {
return fmt.Sprintf("[scope=%s prefix=%s %s]", ei.Ref.Scope, ei.Ref.NamePrefix, ei.Def.String())
}

// StringSet is a set of strings.
type StringSet map[string]struct{}

Expand Down
29 changes: 28 additions & 1 deletion entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ func (id *IndexDefinition) String() string {

func (id *IndexDefinition) equal(other *IndexDefinition) error {
if err := id.Key.equal(other.Key); err != nil {
return errors.Errorf("partitionKey mismatch: (%v)", err)
return errors.Errorf("key mismatch: (%v)", err)
}
if !stringSliceEqual(id.Columns, other.Columns) {
return errors.Errorf("columns mismatch: (%v vs %v)", id.Columns, other.Columns)
Expand All @@ -245,6 +245,11 @@ type EntityDefinition struct {
ETL ETLState
}

func (e *EntityDefinition) String() string {
return fmt.Sprintf("[Entity %s PK %s [Columns %s] [Indexes %s]]", e.Name, e.Key.String(),
strColumns(e.Columns), strIndexes(e.Indexes))
}

// Clone returns a deep copy of EntityDefinition
func (e *EntityDefinition) Clone() *EntityDefinition {
newEd := &EntityDefinition{
Expand Down Expand Up @@ -572,3 +577,25 @@ func deterministicPrintMap(m map[string]string) string {
}
return "{" + strings.Join(pairs, ", ") + "}"
}

func strColumns(cols []*ColumnDefinition) string {
s := make([]string, 0, len(cols))
for _, c := range cols {
s = append(s, c.String())
}
return "[" + strings.Join(s, ", ") + "]"
}

func strIndexes(indexes map[string]*IndexDefinition) string {
names := make([]string, 0, len(indexes))
for n := range indexes {
names = append(names, n)
}
sort.Strings(names)

pairs := make([]string, 0, len(indexes))
for _, n := range names {
pairs = append(pairs, fmt.Sprintf("%s: %s", n, indexes[n].String()))
}
return "{" + strings.Join(pairs, ", ") + "}"
}
13 changes: 13 additions & 0 deletions entity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
package dosa_test

import (
"strings"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -385,6 +386,18 @@ func getValidEntityDefinition() *dosa.EntityDefinition {
}
}

func TestEntityDefinitionPrint(t *testing.T) {
e := getValidEntityDefinition()
expected := []string{"[Entity testentity PK (foo, bar DESC)",
"[Columns [{Name: foo, Type: TUUID, IsPointer: false, Tags: {}},",
"{Name: bar, Type: Int64, IsPointer: false, Tags: {}},",
"{Name: qux, Type: Blob, IsPointer: false, Tags: {}}]]",
"[Indexes {index1: {Key:(qux, bar DESC) Columns:[] TableName:},",
"index2: {Key:(bar) Columns:[qux foo] TableName:}}]]",
}
assert.Equal(t, strings.Join(expected, " "), e.String())
}

func TestEntityDefinitionCanBeUpsertedOn(t *testing.T) {
validEd := getValidEntityDefinition()
// entity name not match
Expand Down

0 comments on commit b44ff93

Please sign in to comment.