Skip to content

Commit

Permalink
Merge pull request #357 from bgilbert/path
Browse files Browse the repository at this point in the history
translate: use `FromTag` in `From` paths added by `AddFromCommonSource`
  • Loading branch information
bgilbert committed Jun 22, 2022
2 parents 87ce0d3 + 16ebbb0 commit d28e6d6
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
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)
}

0 comments on commit d28e6d6

Please sign in to comment.