Skip to content

Commit

Permalink
Extend schema.Schema with lookup by internal name (#525)
Browse files Browse the repository at this point in the history
  • Loading branch information
pivovarit authored Jul 13, 2024
1 parent c84fd2a commit d50c9b6
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 58 deletions.
2 changes: 1 addition & 1 deletion quesma/schema/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (s *schemaRegistry) loadSchemas() (map[TableName]Schema, error) {
s.populateSchemaFromStaticConfiguration(indexConfiguration, fields)
s.populateSchemaFromTableDefinition(definitions, indexName, fields)
s.populateAliases(indexConfiguration, fields, aliases)
schemas[TableName(indexName)] = Schema{Fields: fields, Aliases: aliases}
schemas[TableName(indexName)] = NewSchemaWithAliases(fields, aliases)
}

return schemas, nil
Expand Down
49 changes: 20 additions & 29 deletions quesma/schema/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,10 @@ func Test_schemaRegistry_FindSchema(t *testing.T) {
}},
}},
tableName: "some_table",
want: schema.Schema{Fields: map[schema.FieldName]schema.Field{
want: schema.NewSchema(map[schema.FieldName]schema.Field{
"message": {PropertyName: "message", InternalPropertyName: "message", Type: schema.TypeKeyword},
"event_date": {PropertyName: "event_date", InternalPropertyName: "event_date", Type: schema.TypeTimestamp},
"count": {PropertyName: "count", InternalPropertyName: "count", Type: schema.TypeLong}},
Aliases: map[schema.FieldName]schema.FieldName{}},
"count": {PropertyName: "count", InternalPropertyName: "count", Type: schema.TypeLong}}),
exists: true,
},
{
Expand All @@ -64,11 +63,10 @@ func Test_schemaRegistry_FindSchema(t *testing.T) {
}},
}},
tableName: "some_table",
want: schema.Schema{Fields: map[schema.FieldName]schema.Field{
want: schema.NewSchema(map[schema.FieldName]schema.Field{
"message": {PropertyName: "message", InternalPropertyName: "message", Type: schema.TypeKeyword},
"event_date": {PropertyName: "event_date", InternalPropertyName: "event_date", Type: schema.TypeTimestamp},
"count": {PropertyName: "count", InternalPropertyName: "count", Type: schema.TypeLong}},
Aliases: map[schema.FieldName]schema.FieldName{}},
"count": {PropertyName: "count", InternalPropertyName: "count", Type: schema.TypeLong}}),
exists: true,
},
{
Expand All @@ -85,11 +83,10 @@ func Test_schemaRegistry_FindSchema(t *testing.T) {
}},
}},
tableName: "some_table",
want: schema.Schema{Fields: map[schema.FieldName]schema.Field{
want: schema.NewSchema(map[schema.FieldName]schema.Field{
"message": {PropertyName: "message", InternalPropertyName: "message", Type: schema.TypeKeyword},
"event_date": {PropertyName: "event_date", InternalPropertyName: "event_date", Type: schema.TypeTimestamp},
"count": {PropertyName: "count", InternalPropertyName: "count", Type: schema.TypeLong}},
Aliases: map[schema.FieldName]schema.FieldName{}},
"count": {PropertyName: "count", InternalPropertyName: "count", Type: schema.TypeLong}}),
exists: true,
},
{
Expand All @@ -110,11 +107,10 @@ func Test_schemaRegistry_FindSchema(t *testing.T) {
}},
}},
tableName: "some_table",
want: schema.Schema{Fields: map[schema.FieldName]schema.Field{
want: schema.NewSchema(map[schema.FieldName]schema.Field{
"message": {PropertyName: "message", InternalPropertyName: "message", Type: schema.TypeKeyword},
"event_date": {PropertyName: "event_date", InternalPropertyName: "event_date", Type: schema.TypeTimestamp},
"count": {PropertyName: "count", InternalPropertyName: "count", Type: schema.TypeLong}},
Aliases: map[schema.FieldName]schema.FieldName{}},
"count": {PropertyName: "count", InternalPropertyName: "count", Type: schema.TypeLong}}),
exists: true,
},
{
Expand All @@ -130,10 +126,8 @@ func Test_schemaRegistry_FindSchema(t *testing.T) {
},
tableDiscovery: fixedTableProvider{tables: map[string]schema.Table{}},
tableName: "some_table",
want: schema.Schema{Fields: map[schema.FieldName]schema.Field{
"message": {PropertyName: "message", InternalPropertyName: "message", Type: schema.TypeKeyword}},
Aliases: map[schema.FieldName]schema.FieldName{}},
exists: true,
want: schema.NewSchema(map[schema.FieldName]schema.Field{"message": {PropertyName: "message", InternalPropertyName: "message", Type: schema.TypeKeyword}}),
exists: true,
},
{
name: "schema inferred, with mapping overrides",
Expand All @@ -154,11 +148,10 @@ func Test_schemaRegistry_FindSchema(t *testing.T) {
},
}}},
tableName: "some_table",
want: schema.Schema{Fields: map[schema.FieldName]schema.Field{
want: schema.NewSchema(map[schema.FieldName]schema.Field{
"message": {PropertyName: "message", InternalPropertyName: "message", Type: schema.TypeKeyword},
"event_date": {PropertyName: "event_date", InternalPropertyName: "event_date", Type: schema.TypeTimestamp},
"count": {PropertyName: "count", InternalPropertyName: "count", Type: schema.TypeLong}},
Aliases: map[schema.FieldName]schema.FieldName{}},
"count": {PropertyName: "count", InternalPropertyName: "count", Type: schema.TypeLong}}),
exists: true,
},
{
Expand All @@ -181,13 +174,12 @@ func Test_schemaRegistry_FindSchema(t *testing.T) {
}},
}},
tableName: "some_table",
want: schema.Schema{Fields: map[schema.FieldName]schema.Field{
want: schema.NewSchemaWithAliases(map[schema.FieldName]schema.Field{
"message": {PropertyName: "message", InternalPropertyName: "message", Type: schema.TypeKeyword},
"event_date": {PropertyName: "event_date", InternalPropertyName: "event_date", Type: schema.TypeTimestamp},
"count": {PropertyName: "count", InternalPropertyName: "count", Type: schema.TypeLong}},
Aliases: map[schema.FieldName]schema.FieldName{
"message_alias": "message",
}},
"count": {PropertyName: "count", InternalPropertyName: "count", Type: schema.TypeLong}}, map[schema.FieldName]schema.FieldName{
"message_alias": "message",
}),
exists: true,
},
{
Expand All @@ -208,13 +200,12 @@ func Test_schemaRegistry_FindSchema(t *testing.T) {
}},
}},
tableName: "some_table",
want: schema.Schema{Fields: map[schema.FieldName]schema.Field{
want: schema.NewSchemaWithAliases(map[schema.FieldName]schema.Field{
"message": {PropertyName: "message", InternalPropertyName: "message", Type: schema.TypeKeyword},
"event_date": {PropertyName: "event_date", InternalPropertyName: "event_date", Type: schema.TypeTimestamp},
"count": {PropertyName: "count", InternalPropertyName: "count", Type: schema.TypeLong}},
Aliases: map[schema.FieldName]schema.FieldName{
"message_alias": "message",
}},
"count": {PropertyName: "count", InternalPropertyName: "count", Type: schema.TypeLong}}, map[schema.FieldName]schema.FieldName{
"message_alias": "message",
}),
exists: true,
},
{
Expand Down
29 changes: 27 additions & 2 deletions quesma/schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ package schema

type (
Schema struct {
Fields map[FieldName]Field
Aliases map[FieldName]FieldName
Fields map[FieldName]Field
Aliases map[FieldName]FieldName
internalNameToField map[FieldName]Field
}
Field struct {
// PropertyName is how users refer to the field
Expand All @@ -18,6 +19,22 @@ type (
FieldName string
)

func NewSchemaWithAliases(fields map[FieldName]Field, aliases map[FieldName]FieldName) Schema {
internalNameToField := make(map[FieldName]Field)
for _, field := range fields {
internalNameToField[field.InternalPropertyName] = field
}
return Schema{
Fields: fields,
Aliases: aliases,
internalNameToField: internalNameToField,
}
}

func NewSchema(fields map[FieldName]Field) Schema {
return NewSchemaWithAliases(fields, map[FieldName]FieldName{})
}

func (t FieldName) AsString() string {
return string(t)
}
Expand All @@ -26,6 +43,14 @@ func (t TableName) AsString() string {
return string(t)
}

func (s Schema) ResolveFieldByInternalName(fieldName string) (Field, bool) {
if field, exists := s.internalNameToField[FieldName(fieldName)]; exists {
return field, true
} else {
return Field{}, false
}
}

func (s Schema) ResolveField(fieldName string) (Field, bool) {
if alias, exists := s.Aliases[FieldName(fieldName)]; exists {
field, exists := s.Fields[alias]
Expand Down
82 changes: 56 additions & 26 deletions quesma/schema/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,44 +22,30 @@ func TestSchema_ResolveField(t *testing.T) {
exists: false,
},
{
name: "should resolve field",
fieldName: "message",
schema: Schema{
Fields: map[FieldName]Field{
"message": {PropertyName: "message", InternalPropertyName: "message", Type: TypeText},
},
},
name: "should resolve field",
fieldName: "message",
schema: NewSchema(map[FieldName]Field{"message": {PropertyName: "message", InternalPropertyName: "message", Type: TypeText}}),
resolvedField: Field{PropertyName: "message", InternalPropertyName: "message", Type: TypeText},
exists: true,
},
{
name: "should not resolve field",
fieldName: "foo",
schema: Schema{
Fields: map[FieldName]Field{
"message": {PropertyName: "message", InternalPropertyName: "message", Type: TypeText},
},
},
name: "should not resolve field",
fieldName: "foo",
schema: NewSchema(map[FieldName]Field{"message": {PropertyName: "message", InternalPropertyName: "message", Type: TypeText}}),
resolvedField: Field{},
exists: false,
},
{
name: "should resolve aliased field",
fieldName: "message_alias",
schema: Schema{
Fields: map[FieldName]Field{"message": {PropertyName: "message", InternalPropertyName: "message", Type: TypeText}},
Aliases: map[FieldName]FieldName{"message_alias": "message"},
},
name: "should resolve aliased field",
fieldName: "message_alias",
schema: NewSchemaWithAliases(map[FieldName]Field{"message": {PropertyName: "message", InternalPropertyName: "message", Type: TypeText}}, map[FieldName]FieldName{"message_alias": "message"}),
resolvedField: Field{PropertyName: "message", InternalPropertyName: "message", Type: TypeText},
exists: true,
},
{
name: "should not resolve aliased field",
fieldName: "message_alias",
schema: Schema{
Fields: map[FieldName]Field{"message": {PropertyName: "message", InternalPropertyName: "message", Type: TypeText}},
Aliases: map[FieldName]FieldName{"message_alias": "foo"},
},
name: "should not resolve aliased field",
fieldName: "message_alias",
schema: NewSchemaWithAliases(map[FieldName]Field{"message": {PropertyName: "message", InternalPropertyName: "message", Type: TypeText}}, map[FieldName]FieldName{"message_alias": "foo"}),
resolvedField: Field{},
exists: false,
},
Expand All @@ -76,3 +62,47 @@ func TestSchema_ResolveField(t *testing.T) {
})
}
}

func TestSchema_ResolveFieldByInternalName(t *testing.T) {
tests := []struct {
testName string
schema Schema
fieldName string
want Field
found bool
}{
{
testName: "empty schema",
schema: NewSchemaWithAliases(map[FieldName]Field{}, map[FieldName]FieldName{}),
fieldName: "message",
want: Field{},
found: false,
},
{
testName: "schema with fields with internal separators, lookup by property name",
schema: NewSchema(map[FieldName]Field{"foo.bar": {PropertyName: "foo.bar", InternalPropertyName: "foo::bar", Type: TypeText}}),
fieldName: "foo.bar",
want: Field{},
found: false,
},
{
testName: "schema with fields with internal separators, lookup by internal name",
schema: NewSchema(map[FieldName]Field{"foo.bar": {PropertyName: "foo.bar", InternalPropertyName: "foo::bar", Type: TypeText}}),
fieldName: "foo::bar",
want: Field{PropertyName: "foo.bar", InternalPropertyName: "foo::bar", Type: TypeText},
found: true,
},
}
for _, tt := range tests {
t.Run(tt.testName, func(t *testing.T) {
s := NewSchemaWithAliases(tt.schema.Fields, tt.schema.Aliases)
got, found := s.ResolveFieldByInternalName(tt.fieldName)
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("ResolveFieldByInternalName() got = %v, want %v", got, tt.want)
}
if found != tt.found {
t.Errorf("ResolveFieldByInternalName() got1 = %v, want %v", found, tt.found)
}
})
}
}

0 comments on commit d50c9b6

Please sign in to comment.