Skip to content

Commit

Permalink
translate/set.go: Add AddFromCommonObject helper
Browse files Browse the repository at this point in the history
This is a useful function for copying all the fields of one object to the
identically named fields of another object. We add translations prefixed by
fromPrefix and toPrefix, retrieving the subpaths from to.
  • Loading branch information
saqibali-2k committed Jun 22, 2022
1 parent 8d3b20f commit 72825ae
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions translate/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,19 @@ func (ts TranslationSet) AddFromCommonSource(common path.ContextPath, toPrefix p
ts.AddTranslation(common, toPrefix)
}

// AddFromCommonObject adds translations for all of the paths in to. The paths being translated
// are prefixed by fromPrefix and the translated paths are prefixed by toPrefix.
// This is useful when we want to copy all the fields of an object to another with the same field names.
func (ts TranslationSet) AddFromCommonObject(fromPrefix path.ContextPath, toPrefix path.ContextPath, to interface{}) {
vTo := reflect.ValueOf(to)
vPaths := getAllPaths(vTo, ts.ToTag, true)

for _, path := range vPaths {
ts.AddTranslation(prefixPath(path, fromPrefix.Path...), prefixPath(path, toPrefix.Path...))
}
ts.AddTranslation(fromPrefix, toPrefix)
}

// Merge adds all the entries to the set. It mutates the Set in place.
func (ts TranslationSet) Merge(from TranslationSet) {
for _, t := range from.Set {
Expand Down

0 comments on commit 72825ae

Please sign in to comment.