-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathasset.go
64 lines (57 loc) · 1.46 KB
/
asset.go
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
package rum
import "strconv"
type Asset struct {
Unit string `json:"unit" binding:"required"`
Quantity string `json:"quantity" binding:"required"`
}
type Assets []Asset
func (a *Assets) GetLovelace() uint64 {
if a == nil {
return 0
}
for _, asset := range *a {
if asset.Unit == "lovelace" {
quantity, _ := strconv.ParseUint(asset.Quantity, 10, 64)
return quantity
}
}
return 0
}
func (a *Assets) PopAssetByUnit(unit string) *Asset {
var popAsset Asset
var filteredAssets Assets = []Asset{}
found := false
for _, asset := range *a {
if asset.Unit == unit && !found {
popAsset = asset
found = true
} else {
filteredAssets = append(filteredAssets, asset)
}
}
*a = filteredAssets
return &popAsset
}
func (a *Assets) MergeAssets(assets []Asset) *[]Asset {
mergedAssets := make(map[string]Asset)
for _, asset := range assets {
if existingAsset, ok := mergedAssets[asset.Unit]; ok {
existingAsset.Quantity = AddQuantities(existingAsset.Quantity, asset.Quantity)
mergedAssets[asset.Unit] = existingAsset
} else {
mergedAssets[asset.Unit] = asset
}
}
result := make([]Asset, 0, len(mergedAssets))
for _, asset := range mergedAssets {
result = append(result, asset)
}
return &result
}
func AddQuantities(quantity1 string, quantity2 string) string {
intQuantity1, _ := strconv.Atoi(quantity1)
intQuantity2, _ := strconv.Atoi(quantity2)
sum := intQuantity1 + intQuantity2
stringSum := strconv.Itoa(sum)
return stringSum
}