From b44ff9347cad3d340873fcfbda46357e76adf368 Mon Sep 17 00:00:00 2001 From: Shamim Mohamed Date: Thu, 12 Mar 2020 13:01:07 -0700 Subject: [PATCH] Add String() methods to entity types (#432) * Add String() methods to entity types * update CHANGELOG --- CHANGELOG.md | 5 ++++- connector.go | 4 ++++ entity.go | 29 ++++++++++++++++++++++++++++- entity_test.go | 13 +++++++++++++ 4 files changed, 49 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 62b833fe..3ea677f6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/connector.go b/connector.go index bf2c0c9a..b92ff272 100644 --- a/connector.go +++ b/connector.go @@ -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{} diff --git a/entity.go b/entity.go index f256520c..30814522 100644 --- a/entity.go +++ b/entity.go @@ -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) @@ -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{ @@ -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, ", ") + "}" +} diff --git a/entity_test.go b/entity_test.go index 87c9e6b5..a2504645 100644 --- a/entity_test.go +++ b/entity_test.go @@ -21,6 +21,7 @@ package dosa_test import ( + "strings" "testing" "github.com/stretchr/testify/assert" @@ -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