Skip to content

Commit

Permalink
Refactor go.mod to add indirect dependency on github.com/moznion/go-o…
Browse files Browse the repository at this point in the history
…ptional
  • Loading branch information
Walkmana-25 committed Oct 2, 2024
1 parent edfef7c commit 9469912
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 3 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ require (
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/moznion/go-optional v0.12.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/yuin/gopher-lua v1.1.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ github.com/deckarep/golang-set/v2 v2.6.0 h1:XfcQbWM1LlMB8BsJ8N9vW5ehnnPVIw0je80N
github.com/deckarep/golang-set/v2 v2.6.0/go.mod h1:VAky9rY/yGXJOLEDv3OMci+7wtDpOF4IN+y82NBOac4=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
github.com/moznion/go-optional v0.12.0 h1:gM9YSR7kusSQHiaq2IDHU7WoJNGETT1NbuB15XU4ebA=
github.com/moznion/go-optional v0.12.0/go.mod h1:UP85Bc+uliSDFDzN7Zw8D6gBO1bdPChKFpNu1DJfCqE=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/redis/go-redis/v9 v9.6.1 h1:HHDteefn6ZkTtY5fGUE8tj8uy85AHk6zP7CpzIAM0y4=
Expand Down
18 changes: 17 additions & 1 deletion libanemos.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
// Package libanemos provides structures and functions to work with Anemos data.
package libanemos

import (
"time"

"github.com/moznion/go-optional"
)

// AnemosGet is a structure that holds a slice of anemosData of any type.
type AnemosGet struct {
Data []anemosData[any]
Expand All @@ -9,7 +15,7 @@ type AnemosGet struct {
// The anemosData interface defines a generic type T and requires a Filter method
// that returns an anemosData of the same type.
type anemosData[T any] interface {
Filter() anemosData[T]
Filter(FilterOptions) anemosData[T]
}

// NewAnemosGet initializes and returns a pointer to an AnemosGet instance with an
Expand All @@ -20,3 +26,13 @@ func NewAnemosGet() *AnemosGet {
}
return &anemosget
}

// PostCode is a type alias for a string representing a postal code.
type PostCode string

// FilterOptions is a structure that holds optional values for filtering anemosData.
type FilterOptions struct {
PostCode optional.Option[PostCode]
StartTime optional.Option[time.Time]
EndTime optional.Option[time.Time]
}
43 changes: 41 additions & 2 deletions libanemos_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ package libanemos

import (
"testing"
"time"

"github.com/moznion/go-optional"
)

type mockAnemosData struct{}

func (m mockAnemosData) Filter() anemosData[any] {
func (m mockAnemosData) Filter(filterOption FilterOptions) anemosData[any] {
return m
}

Expand All @@ -19,7 +22,6 @@ func TestNewAnemosGet(t *testing.T) {
anemosGet.Data = append(anemosGet.Data, mockAnemosData{})

// Check anemosGet.Data Type

if len(anemosGet.Data) == 0 {
t.Fatalf("Expected Data length not to be 0")
}
Expand All @@ -28,3 +30,40 @@ func TestNewAnemosGet(t *testing.T) {
t.Fatalf("Expected Data[0] to be of type mockAnemosData")
}
}

func TestFilterOptions(t *testing.T) {
postCode := PostCode("12345")
startTime := time.Now().Add(-time.Hour)
endTime := time.Now()

filterOptions := FilterOptions{
PostCode: optional.Some(postCode),
StartTime: optional.Some(startTime),
EndTime: optional.Some(endTime),
}

postcode_value, err := filterOptions.PostCode.Take()
if err != nil {
t.Fatalf("Expected PostCode to be set, got error: %v", err)
}
if postcode_value != postCode {
t.Fatalf("Expected PostCode to be %v, got %v", postCode, postcode_value)
}

starttime_value, err := filterOptions.StartTime.Take()
if err != nil {
t.Fatalf("Expected StartTime to be set, got error: %v", err)
}
if starttime_value != startTime {
t.Fatalf("Expected StartTime to be %v, got %v", startTime, starttime_value)
}

endtime_value, err := filterOptions.EndTime.Take()
if err != nil {
t.Fatalf("Expected EndTime to be set, got error: %v", err)
}

if endtime_value != endTime {
t.Fatalf("Expected EndTime to be %v, got %v", endTime, endtime_value)
}
}

0 comments on commit 9469912

Please sign in to comment.