-
Notifications
You must be signed in to change notification settings - Fork 36
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add domains table #2225
Open
ozym
wants to merge
2
commits into
main
Choose a base branch
from
add-domains
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
feat: add domains table #2225
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,77 @@ | ||
package meta | ||
|
||
import ( | ||
"sort" | ||
"strings" | ||
) | ||
|
||
const ( | ||
domainName int = iota | ||
domainDescription | ||
domainLast | ||
) | ||
|
||
var domainHeaders Header = map[string]int{ | ||
"Domain": domainName, | ||
"Description": domainDescription, | ||
} | ||
|
||
type Domain struct { | ||
Name string | ||
Description string | ||
} | ||
|
||
type DomainList []Domain | ||
|
||
func (d DomainList) Len() int { return len(d) } | ||
func (d DomainList) Swap(i, j int) { d[i], d[j] = d[j], d[i] } | ||
func (d DomainList) Less(i, j int) bool { return d[i].Name < d[j].Name } | ||
|
||
func (d DomainList) encode() [][]string { | ||
var data [][]string | ||
|
||
data = append(data, domainHeaders.Columns()) | ||
|
||
for _, row := range d { | ||
data = append(data, []string{ | ||
row.Name, | ||
row.Description, | ||
}) | ||
} | ||
|
||
return data | ||
} | ||
|
||
func (d *DomainList) decode(data [][]string) error { | ||
if !(len(data) > 1) { | ||
return nil | ||
} | ||
|
||
var domains []Domain | ||
|
||
fields := domainHeaders.Fields(data[0]) | ||
for _, row := range data[1:] { | ||
d := fields.Remap(row) | ||
|
||
domains = append(domains, Domain{ | ||
Name: strings.TrimSpace(d[domainName]), | ||
Description: strings.TrimSpace(d[domainDescription]), | ||
}) | ||
} | ||
|
||
*d = DomainList(domains) | ||
|
||
return nil | ||
} | ||
|
||
func LoadDomains(path string) ([]Domain, error) { | ||
var d []Domain | ||
|
||
if err := LoadList(path, (*DomainList)(&d)); err != nil { | ||
return nil, err | ||
} | ||
|
||
sort.Sort(DomainList(d)) | ||
|
||
return d, nil | ||
} |
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,17 @@ | ||
package meta | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestDomain(t *testing.T) { | ||
t.Run("check domains", testListFunc("testdata/domains.csv", &DomainList{ | ||
Domain{ | ||
Name: "coastal", | ||
Description: "Coastal Tsunami Gauge Network", | ||
}, | ||
Domain{ | ||
Name: "gnss", | ||
}, | ||
})) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -70,6 +70,11 @@ func (s Set) Doases() []InstalledDoas { | |
return s.doases | ||
} | ||
|
||
// Domains is a helper function to return a slice of Domain values. | ||
func (s Set) Domains() []Domain { | ||
return s.domains | ||
} | ||
|
||
// Features is a helper function to return a slice of Feature values. | ||
func (s Set) Features() []Feature { | ||
return s.features | ||
|
@@ -255,6 +260,17 @@ func (s Set) Dart(station string) (Dart, bool) { | |
return Dart{}, false | ||
} | ||
|
||
// Domain is a helper function to return a Domain value and true if one exists. | ||
func (s Set) Domain(name string) (Domain, bool) { | ||
for _, v := range s.domains { | ||
if name != v.Name { | ||
continue | ||
} | ||
return v, true | ||
} | ||
return Domain{}, false | ||
Comment on lines
+265
to
+271
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: have you considered |
||
} | ||
|
||
// Mark is a helper function to return a Mark value and true if one exists. | ||
func (s Set) Mark(code string) (Mark, bool) { | ||
for _, v := range s.marks { | ||
|
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,3 @@ | ||
Domain,Description | ||
coastal,Coastal Tsunami Gauge Network | ||
gnss, |
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,34 @@ | ||
package delta_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/GeoNet/delta" | ||
"github.com/GeoNet/delta/meta" | ||
) | ||
|
||
var domainChecks = map[string]func(*meta.Set) func(t *testing.T){ | ||
"check for duplicate domains": func(set *meta.Set) func(t *testing.T) { | ||
return func(t *testing.T) { | ||
domains := make(map[string]interface{}) | ||
for _, s := range set.Domains() { | ||
if _, ok := domains[s.Name]; ok { | ||
t.Errorf("domain %s is duplicated", s.Name) | ||
} | ||
domains[s.Name] = true | ||
} | ||
} | ||
}, | ||
} | ||
|
||
func TestDomains(t *testing.T) { | ||
|
||
set, err := delta.New() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
for k, v := range domainChecks { | ||
t.Run(k, v(set)) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nitpick: probably never a problem in practice, but in theory: