-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlocale_merge.go
More file actions
139 lines (127 loc) · 3.98 KB
/
Copy pathlocale_merge.go
File metadata and controls
139 lines (127 loc) · 3.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package internal
func mergeUISection(base, over UISection) UISection {
return UISection{
UIStrings: mergeUIStrings(base.UIStrings, over.UIStrings),
EmploymentTypes: mergeStringMap(base.EmploymentTypes, over.EmploymentTypes),
EndDate: mergeStringMap(base.EndDate, over.EndDate),
}
}
func mergeUIStrings(base, over UIStrings) UIStrings {
out := base
for _, p := range []struct{ dst, src *string }{
{&out.Contact, &over.Contact},
{&out.Socials, &over.Socials},
{&out.LanguagesSection, &over.LanguagesSection},
{&out.About, &over.About},
{&out.Experience, &over.Experience},
{&out.PersonalProjects, &over.PersonalProjects},
{&out.Education, &over.Education},
{&out.SavePDF, &over.SavePDF},
{&out.Remote, &over.Remote},
{&out.Email, &over.Email},
{&out.Phone, &over.Phone},
{&out.Location, &over.Location},
} {
if *p.src != "" {
*p.dst = *p.src
}
}
return out
}
func mergeStringMap(base, over map[string]string) map[string]string {
if len(base) == 0 && len(over) == 0 {
return nil
}
out := make(map[string]string, len(base)+len(over))
for k, v := range base {
out[k] = v
}
for k, v := range over {
if v == "" {
continue
}
out[k] = v
}
return out
}
func overlay[T any](p *T, base T) T {
if p == nil {
return base
}
return *p
}
// overlaySlice mirrors overlay for slices: nil pointer keeps base, an explicit
// (possibly empty) slice replaces it.
func overlaySlice[T any](p *[]T, base []T) []T {
if p == nil {
return base
}
return *p
}
func mergeExperienceEntry(base Experience, loc LocalExperience) Experience {
return Experience{
Company: overlay(loc.Company, base.Company),
Position: overlay(loc.Position, base.Position),
Type: overlay(loc.Type, base.Type),
StartDate: overlay(loc.StartDate, base.StartDate),
EndDate: overlay(loc.EndDate, base.EndDate),
Location: overlay(loc.Location, base.Location),
IsRemote: overlay(loc.IsRemote, base.IsRemote),
Description: overlaySlice(loc.Description, base.Description),
Achievements: overlaySlice(loc.Achievements, base.Achievements),
Skills: overlaySlice(loc.Skills, base.Skills),
}
}
func mergePersonalProjectEntry(base PersonalProject, loc LocalPersonalProject) PersonalProject {
return PersonalProject{
Name: overlay(loc.Name, base.Name),
Link: overlay(loc.Link, base.Link),
StartDate: overlay(loc.StartDate, base.StartDate),
EndDate: overlay(loc.EndDate, base.EndDate),
Description: overlaySlice(loc.Description, base.Description),
Skills: overlaySlice(loc.Skills, base.Skills),
}
}
func mergeEducationEntry(base Education, loc LocalEducation) Education {
return Education{
Institution: overlay(loc.Institution, base.Institution),
Degree: overlay(loc.Degree, base.Degree),
StartDate: overlay(loc.StartDate, base.StartDate),
EndDate: overlay(loc.EndDate, base.EndDate),
Location: overlay(loc.Location, base.Location),
}
}
// mergeIndexed merges per-index overlays onto a base list. If overlays is empty
// the base is returned unchanged. Where a base entry is missing the zero value
// is used as the merge target so a locale can still introduce a new row.
func mergeIndexed[B, O any](base []B, overlays []O, merge func(B, O) B) []B {
if len(overlays) == 0 {
return base
}
n := len(base)
if len(overlays) > n {
n = len(overlays)
}
out := make([]B, n)
for i := 0; i < n; i++ {
var b B
if i < len(base) {
b = base[i]
}
if i < len(overlays) {
out[i] = merge(b, overlays[i])
} else {
out[i] = b
}
}
return out
}
func mergeExperienceList(base []Experience, overlays []LocalExperience) []Experience {
return mergeIndexed(base, overlays, mergeExperienceEntry)
}
func mergePersonalProjectsList(base []PersonalProject, overlays []LocalPersonalProject) []PersonalProject {
return mergeIndexed(base, overlays, mergePersonalProjectEntry)
}
func mergeEducationList(base []Education, overlays []LocalEducation) []Education {
return mergeIndexed(base, overlays, mergeEducationEntry)
}