Skip to content

Commit 16ebbb0

Browse files
committed
translate: use FromTag in From paths added by AddFromCommonSource
AddFromCommonSource was using the ToTag in both paths. Use the FromTag in the From path, and add a test. Fixes: 72825ae ("translate/set.go: Add AddFromCommonObject helper")
1 parent 87ce0d3 commit 16ebbb0

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

translate/set.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ func (ts TranslationSet) AddFromCommonObject(fromPrefix path.ContextPath, toPref
113113
vPaths := getAllPaths(vTo, ts.ToTag, true)
114114

115115
for _, path := range vPaths {
116-
ts.AddTranslation(prefixPath(path, fromPrefix.Path...), prefixPath(path, toPrefix.Path...))
116+
ts.AddTranslation(fromPrefix.Append(path.Path...), toPrefix.Append(path.Path...))
117117
}
118118
ts.AddTranslation(fromPrefix, toPrefix)
119119
}

translate/set_test.go

+20
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,23 @@ func TestTranslationSetMap(t *testing.T) {
7878
fp("a", 0, "b", "i"), fp("A", 0, "B", 1, "F"),
7979
), result, "bad mapping")
8080
}
81+
82+
func TestTranslationSetAddFromCommonSource(t *testing.T) {
83+
type Sub struct {
84+
C int `json:"c"`
85+
}
86+
type Main struct {
87+
A string `json:"a"`
88+
B Sub `json:"b"`
89+
}
90+
91+
expected := NewTranslationSet("yaml", "json")
92+
expected.AddTranslation(path.New("yaml", "y"), path.New("json", "z", 0))
93+
expected.AddTranslation(path.New("yaml", "y", "a"), path.New("json", "z", 0, "a"))
94+
expected.AddTranslation(path.New("yaml", "y", "b"), path.New("json", "z", 0, "b"))
95+
expected.AddTranslation(path.New("yaml", "y", "b", "c"), path.New("json", "z", 0, "b", "c"))
96+
97+
actual := NewTranslationSet("yaml", "json")
98+
actual.AddFromCommonObject(path.New("yaml", "y"), path.New("json", "z", 0), &Main{})
99+
assert.Equal(t, expected, actual)
100+
}

0 commit comments

Comments
 (0)