-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
77 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package schema | ||
|
||
// Field represents a field that has aliases used by other fields. | ||
type Field struct { | ||
name string | ||
aliases []string | ||
} | ||
|
||
// Schema is a list of Fields. | ||
type Schema struct { | ||
fields []Field | ||
} | ||
|
||
// Mapping takes two schemas and returns a map of field names between schemas. | ||
func Mapping(src, target Schema) map[string]string { | ||
aliasMap := make(map[string]string) | ||
for _, field := range target.fields { | ||
aliasMap[field.name] = field.name | ||
for _, alias := range field.aliases { | ||
aliasMap[alias] = field.name | ||
} | ||
} | ||
result := make(map[string]string) | ||
for _, field := range src.fields { | ||
if _, found := aliasMap[field.name]; found { | ||
result[field.name] = aliasMap[field.name] | ||
} | ||
} | ||
return result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package schema | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
var testcases = []struct { | ||
src, target Schema | ||
expected map[string]string | ||
}{ | ||
{src: Schema{[]Field{{"country", []string{"cnt", "countryLong"}}}}, | ||
target: Schema{[]Field{{"countryLong", []string{"country"}}}}, | ||
expected: map[string]string{"country": "countryLong"}}, | ||
{src: Schema{[]Field{ | ||
{"country", []string{"nationality"}}, | ||
{"state", []string{"st", "territory"}}, | ||
{"city", []string{"cty", "burrow"}}, | ||
}}, | ||
target: Schema{[]Field{ | ||
{"st", []string{"not country", "state", "sttt"}}, | ||
{"burrow", []string{"hometown", "city"}}, | ||
{"nationality", []string{"country", "nation", "residency"}}, | ||
}}, | ||
expected: map[string]string{ | ||
"country": "nationality", | ||
"state": "st", | ||
"city": "burrow", | ||
}}, | ||
} | ||
|
||
func TestMapping(t *testing.T) { | ||
t.Parallel() | ||
for _, tc := range testcases { | ||
if result := Mapping(tc.src, tc.target); !reflect.DeepEqual(result, tc.expected) { | ||
t.Errorf("Expected %v but got %v", tc.expected, result) | ||
} | ||
} | ||
} | ||
|
||
func BenchmarkMapping(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
for _, tc := range testcases { | ||
Mapping(tc.src, tc.target) | ||
} | ||
} | ||
} |