Skip to content

Commit

Permalink
use map to avoid double loop
Browse files Browse the repository at this point in the history
  • Loading branch information
igaryo0506 committed Dec 23, 2024
1 parent c71d688 commit c698679
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions clientgenv2/source_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,17 +158,24 @@ func mergeFieldsRecursively(targetFields ResponseFieldList, sourceFields Respons
return responseFieldList, newPreMerged, newPostMerged
}

func structSourcesMapByTypeName(sources []*StructSource) map[string]*StructSource {
res := make(map[string]*StructSource)
for _, source := range sources {
res[source.Name] = source
}
return res
}

func (g *StructGenerator) MergedStructSources(sources []*StructSource) []*StructSource {
res := sources
preMergedStructSourcesMap := structSourcesMapByTypeName(g.preMergedStructSources)
res := make([]*StructSource, 0)
// remove pre-merged struct
for _, preMergedTypeName := range g.preMergedStructSources {
for i, source := range res {
// when name is same, remove it
if source.Name == preMergedTypeName.Name {
res = append(res[:i], res[i+1:]...)
break
}
for _, source := range sources {
// when name is same, remove it
if _, ok := preMergedStructSourcesMap[source.Name]; ok {
continue
}
res = append(res, source)
}

// append post-merged struct
Expand Down

0 comments on commit c698679

Please sign in to comment.