-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlookup_schemas.go
57 lines (49 loc) · 1.47 KB
/
lookup_schemas.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
package ingest_resolution
import (
"fmt"
"github.com/kwilteam/kwil-db/core/types"
"strings"
)
type ContractSelector struct {
Owner string
Name string
}
func FilterSelectedContracts(selectors []ContractSelector, contracts []types.DatasetIdentifier) []types.DatasetIdentifier {
var selectedContracts []types.DatasetIdentifier
for _, contract := range contracts {
if IsSelected(selectors, contract) {
selectedContracts = append(selectedContracts, contract)
}
}
return selectedContracts
}
func IsSelected(selectors []ContractSelector, contract types.DatasetIdentifier) bool {
for _, selector := range selectors {
isOwnerMatch := selector.Owner == "*" || selector.Owner == string(contract.Owner)
isNameMatch := selector.Name == "*" || selector.Name == contract.Name
if isOwnerMatch && isNameMatch {
return true
}
}
return false
}
// LookupSchemaToSelectors converts the lookup schemas to contract selectors
// lookup schemas are in the format of `owner/name`
func LookupSchemaToSelectors(lookupSchemas []string) []ContractSelector {
var selectors []ContractSelector
for _, schema := range lookupSchemas {
// first slash divides owner and name
// let's find the slash index
slashIndex := strings.Index(schema, "/")
if slashIndex == -1 {
panic(fmt.Sprintf("invalid schema: %s", schema))
}
owner := schema[:slashIndex]
name := schema[slashIndex+1:]
selectors = append(selectors, ContractSelector{
Owner: owner,
Name: name,
})
}
return selectors
}