Skip to content
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
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions meta/domain.go
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,
Comment on lines +37 to +38

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:

d1 := DomainList{ Domain{ Name: "with space "}}
var d2 DomainList
d2.decode(d1.encode())
// d1[0].Name != d2[0].Name

})
}

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
}
17 changes: 17 additions & 0 deletions meta/domains_test.go
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",
},
}))
}
2 changes: 2 additions & 0 deletions meta/generate/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func main() {
"deployedDataloggers": {"DeployedDatalogger"},
"deployedReceivers": {"DeployedReceiver"},
"doases": {"InstalledDoas"},
"domains": {"Domain"},
"features": {"Feature"},
"firmwareHistory": {"FirmwareHistory"},
"gains": {"Gain"},
Expand Down Expand Up @@ -58,6 +59,7 @@ func main() {
"assets": {"Asset", []string{"make", "model", "serial"}},
"citations": {"Citation", []string{"key"}},
"classes": {"Class", []string{"station"}},
"domains": {"Domain", []string{"name"}},
"darts": {"Dart", []string{"station"}},
"marks": {"Mark", []string{"code"}},
"monuments": {"Monument", []string{"mark"}},
Expand Down
3 changes: 3 additions & 0 deletions meta/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ const (
VisibilityFile = "environment/visibility.csv"

CitationsFile = "references/citations.csv"
DomainsFile = "references/domains.csv"
)

// SetPathMap is used to manipulate the filepath inside the Set.
Expand Down Expand Up @@ -105,6 +106,7 @@ type Set struct {
visibilities VisibilityList

citations CitationList
domains DomainList
}

func (s *Set) files() map[string]List {
Expand Down Expand Up @@ -152,6 +154,7 @@ func (s *Set) files() map[string]List {
VisibilityFile: &s.visibilities,

CitationsFile: &s.citations,
DomainsFile: &s.domains,
}
}

Expand Down
16 changes: 16 additions & 0 deletions meta/set_auto.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: have you considered slices package, might be more concise...(7 LOC later)...nevermind 😑

}

// 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 {
Expand Down
3 changes: 3 additions & 0 deletions meta/testdata/domains.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Domain,Description
coastal,Coastal Tsunami Gauge Network
gnss,
34 changes: 34 additions & 0 deletions tests/domains_test.go
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))
}
}
Loading