Skip to content

Commit

Permalink
make it safe to create schema multiple times
Browse files Browse the repository at this point in the history
  • Loading branch information
jaym committed Mar 22, 2024
1 parent 5406445 commit ea8fb01
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion providers-sdk/v1/inventory/asset_url.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,34 @@ func (a *AssetUrlSchema) Add(branch *AssetUrlBranch) error {
}

branch.setDepth(found.Depth + 1)
found.Values[lastKey] = branch
found.Values[lastKey] = copyBranch(branch)
return nil
}

func copyBranch(b *AssetUrlBranch) *AssetUrlBranch {
if b == nil {
return nil
}

res := &AssetUrlBranch{
PathSegments: make([]string, len(b.PathSegments)),
Key: b.Key,
Values: make(map[string]*AssetUrlBranch, len(b.Values)),
Title: b.Title,
References: make([]string, len(b.References)),
Depth: b.Depth,
}

copy(res.PathSegments, b.PathSegments)
copy(res.References, b.References)

for k, v := range b.Values {
res.Values[k] = copyBranch(v)
}

return res
}

func (a *AssetUrlBranch) setDepth(i uint32) {
a.Depth = i
next := i + 1
Expand Down

0 comments on commit ea8fb01

Please sign in to comment.