Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

translate: use FromTag in From paths added by AddFromCommonSource #357

Merged
merged 1 commit into from
Jun 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion translate/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func (ts TranslationSet) AddFromCommonObject(fromPrefix path.ContextPath, toPref
vPaths := getAllPaths(vTo, ts.ToTag, true)

for _, path := range vPaths {
ts.AddTranslation(prefixPath(path, fromPrefix.Path...), prefixPath(path, toPrefix.Path...))
ts.AddTranslation(fromPrefix.Append(path.Path...), toPrefix.Append(path.Path...))
}
ts.AddTranslation(fromPrefix, toPrefix)
}
Expand Down
20 changes: 20 additions & 0 deletions translate/set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,23 @@ func TestTranslationSetMap(t *testing.T) {
fp("a", 0, "b", "i"), fp("A", 0, "B", 1, "F"),
), result, "bad mapping")
}

func TestTranslationSetAddFromCommonSource(t *testing.T) {
type Sub struct {
C int `json:"c"`
}
type Main struct {
A string `json:"a"`
B Sub `json:"b"`
}

expected := NewTranslationSet("yaml", "json")
expected.AddTranslation(path.New("yaml", "y"), path.New("json", "z", 0))
expected.AddTranslation(path.New("yaml", "y", "a"), path.New("json", "z", 0, "a"))
expected.AddTranslation(path.New("yaml", "y", "b"), path.New("json", "z", 0, "b"))
expected.AddTranslation(path.New("yaml", "y", "b", "c"), path.New("json", "z", 0, "b", "c"))

actual := NewTranslationSet("yaml", "json")
actual.AddFromCommonObject(path.New("yaml", "y"), path.New("json", "z", 0), &Main{})
assert.Equal(t, expected, actual)
}