-
Notifications
You must be signed in to change notification settings - Fork 206
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
255 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package types | ||
|
||
type ChainField string | ||
|
||
// Fields in the Chain struct available for sorting. | ||
const ( | ||
ChainChain ChainField = "chain" | ||
ChainChainId ChainField = "chainId" | ||
ChainIpfsGateway ChainField = "ipfsGateway" | ||
ChainLocalExplorer ChainField = "localExplorer" | ||
ChainRemoteExplorer ChainField = "remoteExplorer" | ||
ChainRpcProvider ChainField = "rpcProvider" | ||
ChainSymbol ChainField = "symbol" | ||
) | ||
|
||
// IsValidChainField returns true if the given field is a valid sortable Chain field. | ||
func IsValidChainField(field string) bool { | ||
switch field { | ||
case "chain", "chainId", "ipfsGateway", "localExplorer", "remoteExplorer", "rpcProvider", "symbol": | ||
return true | ||
} | ||
return false | ||
} | ||
|
||
// ChainBy returns a comparison function for sorting Chain instances by the given field. | ||
// These comparison functions may be strung together by the CmdChains function. | ||
func ChainBy(field ChainField, order SortOrder) func(p1, p2 Chain) bool { | ||
switch field { | ||
case ChainChain: // string | ||
return func(p1, p2 Chain) bool { | ||
if order == Ascending { | ||
return p1.Chain < p2.Chain | ||
} | ||
return p1.Chain > p2.Chain | ||
} | ||
case ChainChainId: // uint64 | ||
return func(p1, p2 Chain) bool { | ||
if order == Ascending { | ||
return p1.ChainId < p2.ChainId | ||
} | ||
return p1.ChainId > p2.ChainId | ||
} | ||
case ChainIpfsGateway: // string | ||
return func(p1, p2 Chain) bool { | ||
if order == Ascending { | ||
return p1.IpfsGateway < p2.IpfsGateway | ||
} | ||
return p1.IpfsGateway > p2.IpfsGateway | ||
} | ||
case ChainLocalExplorer: // string | ||
return func(p1, p2 Chain) bool { | ||
if order == Ascending { | ||
return p1.LocalExplorer < p2.LocalExplorer | ||
} | ||
return p1.LocalExplorer > p2.LocalExplorer | ||
} | ||
case ChainRemoteExplorer: // string | ||
return func(p1, p2 Chain) bool { | ||
if order == Ascending { | ||
return p1.RemoteExplorer < p2.RemoteExplorer | ||
} | ||
return p1.RemoteExplorer > p2.RemoteExplorer | ||
} | ||
case ChainRpcProvider: // string | ||
return func(p1, p2 Chain) bool { | ||
if order == Ascending { | ||
return p1.RpcProvider < p2.RpcProvider | ||
} | ||
return p1.RpcProvider > p2.RpcProvider | ||
} | ||
case ChainSymbol: // string | ||
return func(p1, p2 Chain) bool { | ||
if order == Ascending { | ||
return p1.Symbol < p2.Symbol | ||
} | ||
return p1.Symbol > p2.Symbol | ||
} | ||
|
||
} | ||
panic("Should not happen in ChainBy") | ||
} | ||
|
||
// ChainCmp accepts a slice and variadic comparison functions and returns a functions | ||
// that can be used to sort the slice. | ||
func ChainCmp(slice []Chain, orders ...func(p1, p2 Chain) bool) func(i, j int) bool { | ||
return func(i, j int) bool { | ||
p1, p2 := slice[i], slice[j] | ||
for _, order := range orders { | ||
if order(p1, p2) { | ||
return true | ||
} | ||
if order(p2, p1) { | ||
return false | ||
} | ||
} | ||
return false | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package types | ||
|
||
type MonitorField string | ||
|
||
// Fields in the Monitor struct available for sorting. | ||
const ( | ||
MonitorAddress MonitorField = "address" | ||
MonitorDeleted MonitorField = "deleted" | ||
MonitorFileSize MonitorField = "fileSize" | ||
MonitorIsEmpty MonitorField = "isEmpty" | ||
MonitorIsStaged MonitorField = "isStaged" | ||
MonitorLastScanned MonitorField = "lastScanned" | ||
MonitorNRecords MonitorField = "nRecords" | ||
) | ||
|
||
// IsValidMonitorField returns true if the given field is a valid sortable Monitor field. | ||
func IsValidMonitorField(field string) bool { | ||
switch field { | ||
case "address", "deleted", "fileSize", "isEmpty", "isStaged", "lastScanned", "nRecords": | ||
return true | ||
} | ||
return false | ||
} | ||
|
||
// MonitorBy returns a comparison function for sorting Monitor instances by the given field. | ||
// These comparison functions may be strung together by the CmdMonitors function. | ||
func MonitorBy(field MonitorField, order SortOrder) func(p1, p2 Monitor) bool { | ||
switch field { | ||
case MonitorAddress: // address | ||
return func(p1, p2 Monitor) bool { | ||
cmp := p1.Address.Cmp(p2.Address.Address) | ||
if order == Ascending { | ||
return cmp == -1 | ||
} | ||
return cmp == 1 | ||
} | ||
case MonitorDeleted: // bool | ||
return func(p1, p2 Monitor) bool { | ||
if order == Ascending { | ||
return !p1.Deleted && p2.Deleted | ||
} | ||
return p1.Deleted && !p2.Deleted | ||
} | ||
case MonitorFileSize: // int64 | ||
return func(p1, p2 Monitor) bool { | ||
if order == Ascending { | ||
return p1.FileSize < p2.FileSize | ||
} | ||
return p1.FileSize > p2.FileSize | ||
} | ||
case MonitorIsEmpty: // bool | ||
return func(p1, p2 Monitor) bool { | ||
if order == Ascending { | ||
return !p1.IsEmpty && p2.IsEmpty | ||
} | ||
return p1.IsEmpty && !p2.IsEmpty | ||
} | ||
case MonitorIsStaged: // bool | ||
return func(p1, p2 Monitor) bool { | ||
if order == Ascending { | ||
return !p1.IsStaged && p2.IsStaged | ||
} | ||
return p1.IsStaged && !p2.IsStaged | ||
} | ||
case MonitorLastScanned: // uint32 | ||
return func(p1, p2 Monitor) bool { | ||
if order == Ascending { | ||
return p1.LastScanned < p2.LastScanned | ||
} | ||
return p1.LastScanned > p2.LastScanned | ||
} | ||
case MonitorNRecords: // int64 | ||
return func(p1, p2 Monitor) bool { | ||
if order == Ascending { | ||
return p1.NRecords < p2.NRecords | ||
} | ||
return p1.NRecords > p2.NRecords | ||
} | ||
|
||
} | ||
panic("Should not happen in MonitorBy") | ||
} | ||
|
||
// MonitorCmp accepts a slice and variadic comparison functions and returns a functions | ||
// that can be used to sort the slice. | ||
func MonitorCmp(slice []Monitor, orders ...func(p1, p2 Monitor) bool) func(i, j int) bool { | ||
return func(i, j int) bool { | ||
p1, p2 := slice[i], slice[j] | ||
for _, order := range orders { | ||
if order(p1, p2) { | ||
return true | ||
} | ||
if order(p2, p1) { | ||
return false | ||
} | ||
} | ||
return false | ||
} | ||
} |
14 changes: 7 additions & 7 deletions
14
src/dev_tools/goMaker/templates/classDefinitions/fields/chain.csv
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
name ,type ,strDefault ,attributes ,docOrder ,description | ||
chain ,string , , , 1 ,the common name of the chain | ||
chainId ,uint64 , , , 2 ,the chain id as reported by the RPC | ||
symbol ,string , , , 3 ,the symbol of the base currency on the chain | ||
rpcProvider ,string ,http://localhost:8545 , , 4 ,a valid RPC provider for the chain | ||
remoteExplorer ,string ,http://etherscan.io , , 5 ,a remote explorer for the chain such as Etherscan | ||
localExplorer ,string ,http://localhost:1234 , , 6 ,the local explorer for the chain (typically TrueBlocks Explorer) | ||
ipfsGateway ,string ,http://gateway.ipfs.io/ipfs , , 7 ,an IPFS gateway for pinning the index if enabled | ||
chain ,string , ,sorts , 1 ,the common name of the chain | ||
chainId ,uint64 , ,sorts , 2 ,the chain id as reported by the RPC | ||
symbol ,string , ,sorts , 3 ,the symbol of the base currency on the chain | ||
rpcProvider ,string ,http://localhost:8545 ,sorts , 4 ,a valid RPC provider for the chain | ||
remoteExplorer ,string ,http://etherscan.io ,sorts , 5 ,a remote explorer for the chain such as Etherscan | ||
localExplorer ,string ,http://localhost:1234 ,sorts , 6 ,the local explorer for the chain (typically TrueBlocks Explorer) | ||
ipfsGateway ,string ,http://gateway.ipfs.io/ipfs ,sorts , 7 ,an IPFS gateway for pinning the index if enabled |
14 changes: 7 additions & 7 deletions
14
src/dev_tools/goMaker/templates/classDefinitions/fields/monitor.csv
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
name ,type ,strDefault ,attributes ,upgrades ,docOrder ,description | ||
address ,address , , , , 1 ,the address of this monitor | ||
deleted ,bool , , , , 8 ,`true` if this monitor has been deleted, `false` otherwise | ||
isEmpty ,bool , , ,>3.1.2:bool , 6 ,`true` if the monitor has no appearances, `false` otherwise | ||
isStaged ,bool , , ,>3.1.2:bool , 7 ,`true` if the monitor file in on the stage, `false` otherwise | ||
fileSize ,int64 , , , , 4 ,the size of this monitor on disc | ||
lastScanned ,uint32 , , , , 5 ,the last scanned block number | ||
nRecords ,int64 , , , , 3 ,the number of appearances for this monitor | ||
address ,address , ,sorts , , 1 ,the address of this monitor | ||
deleted ,bool , ,sorts , , 8 ,`true` if this monitor has been deleted, `false` otherwise | ||
isEmpty ,bool , ,sorts ,>3.1.2:bool , 6 ,`true` if the monitor has no appearances, `false` otherwise | ||
isStaged ,bool , ,sorts ,>3.1.2:bool , 7 ,`true` if the monitor file in on the stage, `false` otherwise | ||
fileSize ,int64 , ,sorts , , 4 ,the size of this monitor on disc | ||
lastScanned ,uint32 , ,sorts , , 5 ,the last scanned block number | ||
nRecords ,int64 , ,sorts , , 3 ,the number of appearances for this monitor | ||
name ,string , , , , 2 ,the name of this monitor (if any) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
src/dev_tools/goMaker/templates/generators/codebase/sdk_sorts.go.tmpl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package sdk | ||
|
||
// EXISTING_CODE | ||
// EXISTING_CODE | ||
|
||
type SortOrder = types.SortOrder | ||
|
||
const ( | ||
Asc SortOrder = types.Ascending | ||
Dec SortOrder = types.Descending | ||
) | ||
|
||
type SortSpec struct { | ||
Fields []string `json:"fields"` | ||
Order []SortOrder `json:"orders"` | ||
} | ||
|
||
{{range .Structures}} | ||
{{ if .HasSorts }}{{.Sorts2}}{{end}} | ||
{{end}} | ||
|
||
// EXISTING_CODE | ||
// EXISTING_CODE |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters