Skip to content

Commit

Permalink
feat: parsing dataset using map
Browse files Browse the repository at this point in the history
  • Loading branch information
mj committed May 8, 2023
1 parent 2f44eef commit 2666c27
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 38 deletions.
53 changes: 38 additions & 15 deletions asset.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package assets

import (
"github.com/GoFarsi/assets/entity"
"golang.org/x/exp/maps"
)

type AssetRepo struct {
Chains []*entity.Chain
Chains map[string]*entity.Chain
}

type Pagination struct {
Expand Down Expand Up @@ -34,6 +35,11 @@ func (a *AssetRepo) GetTotalChainsSize() int {
return len(a.Chains)
}

// GetChains return all chains (networks) in assets.yaml
func (a *AssetRepo) GetChains(option *Option) ([]*entity.Chain, error) {
return applyOptionsOnChains(a.Chains, option)
}

// GetTestChains return test chains (networks) in assets.yaml
func (a *AssetRepo) GetTestChains(option *Option) ([]*entity.Chain, error) {
chains := getChainsByType(a.Chains, entity.TestChainType)
Expand All @@ -46,45 +52,62 @@ func (a *AssetRepo) GetMainChains(option *Option) ([]*entity.Chain, error) {
return applyOptionsOnChains(chains, option)
}

// GetChain return chain by its id
// GetChain return chain by its ID
func (a *AssetRepo) GetChain(Id string) *entity.Chain {
for _, c := range a.Chains {
if c.Id == Id {
return c
return a.Chains[Id]
}

// GetChainBySymbol return chain by its symbol
func (a *AssetRepo) GetChainBySymbol(symbol string) *entity.Chain {
for _, v := range a.Chains {
if v.Symbol == symbol {
return v
}
}

return nil
}

// GetChainBySymbol return chain by its symbol
func (a *AssetRepo) GetChainBySymbol(symbol string) *entity.Chain {
// GetChainByName return chain by its name
func (a *AssetRepo) GetChainByName(name string) *entity.Chain {
for _, v := range a.Chains {
if v.Name == name {
return v
}
}

return nil
}

// GetAsset return asset by its ID
func (a *AssetRepo) GetAsset(Id string) *entity.Asset {
for _, c := range a.Chains {
if c.Symbol == symbol {
return c
if c.Assets[Id] != nil {
return c.Assets[Id]
}
}

return nil
}

// applyOptionsOnChains will check Options passed to requests and apply theme to result chains
func applyOptionsOnChains(chains []*entity.Chain, option *Option) ([]*entity.Chain, error) {
func applyOptionsOnChains(chains map[string]*entity.Chain, option *Option) ([]*entity.Chain, error) {

if option.Pagination != nil {
return getPaginatedChainList(chains, option.Pagination.PageNumber, option.Pagination.PageSize)
}

return chains, nil
return maps.Values(chains), nil
}

// getChainsByType return list of chains by selecting type of chain (test, main,...)
func getChainsByType(chains []*entity.Chain, chainType entity.ChainType) (result []*entity.Chain) {
for _, c := range chains {
if c.Type != chainType {
func getChainsByType(chains map[string]*entity.Chain, chainType entity.ChainType) map[string]*entity.Chain {
result := make(map[string]*entity.Chain)
for k, v := range chains {
if v.Type != chainType {
continue
}
result = append(result, c)
result[k] = v
}

return result
Expand Down
2 changes: 2 additions & 0 deletions entity/asset.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ type Asset struct {
LogoPNG string `yaml:"logo_png"`
Types []string `yaml:"types"`
Standards []string `yaml:"standards"`

ChainUUID string
}

func (a *Asset) GetName() string {
Expand Down
20 changes: 9 additions & 11 deletions entity/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,21 @@ const (
)

type Chain struct {
Id string `yaml:"id"`
ParentId string `yaml:"parent_id"`
Name string `yaml:"name"`
Symbol string `yaml:"symbol"`
LogoPNG string `yaml:"logo_png"`
ChainId string `yaml:"chain_id"`
Assets []*Asset `yaml:"assets"`
Type ChainType `yaml:"type"`
ParentId string `yaml:"parent_id"`
Name string `yaml:"name"`
Symbol string `yaml:"symbol"`
LogoPNG string `yaml:"logo_png"`
ChainId string `yaml:"chain_id"`
Assets map[string]*Asset `yaml:"assets"`
Type ChainType `yaml:"type"`
}

func NewChain(name, symbol string, chainType ChainType, chainId string, assets ...*Asset) *Chain {
func NewChain(name, symbol string, chainType ChainType, chainId string) *Chain {
return &Chain{
Name: name,
Symbol: symbol,
ChainId: chainId,
Type: chainType,
Assets: assets,
}
}

Expand Down Expand Up @@ -60,6 +58,6 @@ func (c *Chain) IsTestNet() bool {
return false
}

func (c *Chain) GetChainAssets() []*Asset {
func (c *Chain) GetChainAssets() map[string]*Asset {
return c.Assets
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ module github.com/GoFarsi/assets
go 1.20

require gopkg.in/yaml.v3 v3.0.1

require golang.org/x/exp v0.0.0-20230425010034-47ecfdc1ba53
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
golang.org/x/exp v0.0.0-20230425010034-47ecfdc1ba53 h1:5llv2sWeaMSnA3w2kS57ouQQ4pudlXrR0dCgw51QK9o=
golang.org/x/exp v0.0.0-20230425010034-47ecfdc1ba53/go.mod h1:V1LtkGg67GoY2N1AnLN78QLrzxkLyJw7RJb1gzOOz9w=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down
5 changes: 3 additions & 2 deletions list.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package assets
import (
"errors"
"github.com/GoFarsi/assets/entity"
"golang.org/x/exp/maps"
)

// getPaginatedChainList returns paginated list of chains with pageNumber and pageSize params
func getPaginatedChainList(chains []*entity.Chain, pageNumber, pageSize int) (result []*entity.Chain, err error) {
func getPaginatedChainList(chains map[string]*entity.Chain, pageNumber, pageSize int) (result []*entity.Chain, err error) {
totalChains := len(chains)
pageNumber, err = validatePageNumber(pageNumber)
if err != nil {
Expand All @@ -23,7 +24,7 @@ func getPaginatedChainList(chains []*entity.Chain, pageNumber, pageSize int) (re
end = totalChains
}

return chains[start:end], nil
return maps.Values(chains)[start:end], nil
}

// validatePageNumber will check the value of pageNumber to be greater than 0
Expand Down
2 changes: 1 addition & 1 deletion parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
var assetsByte []byte

// parseAssetsByteToArray parse the byte contents of yaml file to array of entity.Chain
func parseAssetsByteToArray() (chains []*entity.Chain) {
func parseAssetsByteToArray() (chains map[string]*entity.Chain) {
_ = yaml.Unmarshal(assetsByte, &chains)
return chains
}
18 changes: 9 additions & 9 deletions resources/assets.yaml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
- id: bc08bb60-ebfb-11ed-bd6d-bce92fb9ddda
bc08bb60-ebfb-11ed-bd6d-bce92fb9ddda:
parent_id: ""
name: Ethereum
symbol: ETH
logo_png: resources/img/bc08bb60-ebfb-11ed-bd6d-bce92fb9ddda.png
chain_id: 1
type: main
assets:
- id: bc2cd3b5-ebfb-11ed-bd6d-bce92fb9ddda
bc2cd3b5-ebfb-11ed-bd6d-bce92fb9ddda:
name: Tether
symbol: USDT
contracts:
Expand All @@ -20,7 +20,7 @@
types: [ ]
standards:
- ERC20
- id: ae43c0bd-2587-4384-936d-24cc2e5a1b0c
ae43c0bd-2587-4384-936d-24cc2e5a1b0c:
name: Dai
symbol: DAI
contracts:
Expand All @@ -34,7 +34,7 @@
types: [ ]
standards:
- ERC20
- id: db4461d5-4747-436c-9d7c-51c88e597813
db4461d5-4747-436c-9d7c-51c88e597813:
name: Binance Coin
symbol: BNB
contracts:
Expand All @@ -47,7 +47,7 @@
logo_png: resources/img/db4461d5-4747-436c-9d7c-51c88e597813.png
types: [ ]
standards:
- id: fa7bfc08-25dd-477d-bb83-bf0a1039b446
fa7bfc08-25dd-477d-bb83-bf0a1039b446:
name: USD Coin
symbol: USDC
contracts:
Expand All @@ -60,14 +60,14 @@
logo_png: resources/img/fa7bfc08-25dd-477d-bb83-bf0a1039b446.png
types: [ ]
standards:
- id: cfc035ff-52c3-4357-bebd-02ad6813d123
cfc035ff-52c3-4357-bebd-02ad6813d123:
parent_id: ""
name: BNB Smart Chain (BEP20)
symbol: BNB
logo_png: resources/img/db4461d5-4747-436c-9d7c-51c88e597813.png
type: main
assets:
- id: c4ed735b-9c87-4735-a029-fb11fef57ecb
c4ed735b-9c87-4735-a029-fb11fef57ecb:
name: XRP Coin
symbol: XRP
contracts:
Expand All @@ -80,15 +80,15 @@
logo_png: resources/img/c4ed735b-9c87-4735-a029-fb11fef57ecb.png
types: []
standards:
- id: f438b0c8-3b98-4da7-88f3-10ff760b9ff1
f438b0c8-3b98-4da7-88f3-10ff760b9ff1:
parent_id: ""
name: Goerli
symbol: ETH
logo_png: resources/img/bc08bb60-ebfb-11ed-bd6d-bce92fb9ddda.png
chain_id: 1
type: test
assets:
- id: f0e13421-4798-45ed-9ce5-a559cdb5133e
f0e13421-4798-45ed-9ce5-a559cdb5133e:
name: Tether
symbol: USDT
contracts:
Expand Down

0 comments on commit 2666c27

Please sign in to comment.