-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: maxwellgithinji <maxwellgithinji@gmail.com>
- Loading branch information
1 parent
2a1612c
commit d4648c5
Showing
27 changed files
with
1,778 additions
and
529 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
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 |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package enums | ||
|
||
import "fmt" | ||
|
||
// CountiesOfCountry defines the counties of a country | ||
type CountiesOfCountry struct { | ||
Country CountryType | ||
Counties []CountyType | ||
} | ||
|
||
// Create a struct with a combined list of counties of countries | ||
var countiesOfCountries = []CountiesOfCountry{ | ||
{ | ||
Country: CountryTypeKenya, | ||
Counties: KenyanCounties, | ||
}, | ||
// Other CountiesOfCountries | ||
} | ||
|
||
// ValidateCountiesOfCountries validates the county passed to a country is valid | ||
func ValidateCountiesOfCountries(country CountryType, county CountyType) error { | ||
// ensure we are working with a correct country type to begin with | ||
if !country.IsValid() { | ||
return fmt.Errorf("failed to validate country: %s", country) | ||
} | ||
// Validate the county too | ||
if !county.IsValid() { | ||
return fmt.Errorf("failed to validate county: %s", county) | ||
} | ||
|
||
ok, counties := findSelectedCountryCounties(countiesOfCountries, country) | ||
if !ok { | ||
return fmt.Errorf("failed to find selected country's counties: %s", county) | ||
} | ||
|
||
err := findCounty(counties.Counties, county) | ||
if err != nil { | ||
return fmt.Errorf("failed to find county: %s", err) | ||
} | ||
return nil | ||
} | ||
|
||
// finds the selected country te ensure it's part of the enum, then return the respective counties it has | ||
func findSelectedCountryCounties(countriesCounties []CountiesOfCountry, countryInput CountryType) (bool, *CountiesOfCountry) { | ||
for i, countryCounty := range countriesCounties { | ||
if countryCounty.Country == countryInput { | ||
return true, &countiesOfCountries[i] | ||
} | ||
} | ||
return false, nil | ||
} | ||
|
||
// checks whether the county provided is present in the list of the selected country's counties | ||
func findCounty(counties []CountyType, countyInput CountyType) error { | ||
for _, county := range counties { | ||
if county == countyInput { | ||
return nil | ||
} | ||
} | ||
return fmt.Errorf("failed to find county: %s", countyInput) | ||
} |
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,54 @@ | ||
package enums | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"strconv" | ||
) | ||
|
||
// CountryType defines various countries available | ||
type CountryType string | ||
|
||
const ( | ||
// CountryTypeKenya defines country type KENYA | ||
CountryTypeKenya CountryType = "KENYA" | ||
// Other countries | ||
) | ||
|
||
// AllCountries represents a slice of all countries available | ||
var AllCountries = []CountryType{ | ||
CountryTypeKenya, | ||
} | ||
|
||
// IsValid returns true if a country type is valid | ||
func (e CountryType) IsValid() bool { | ||
switch e { | ||
case CountryTypeKenya: | ||
return true | ||
} | ||
return false | ||
} | ||
|
||
// String converts country type to string. | ||
func (e CountryType) String() string { | ||
return string(e) | ||
} | ||
|
||
// UnmarshalGQL converts the supplied value to a country type. | ||
func (e *CountryType) UnmarshalGQL(v interface{}) error { | ||
str, ok := v.(string) | ||
if !ok { | ||
return fmt.Errorf("enums must be strings") | ||
} | ||
|
||
*e = CountryType(str) | ||
if !e.IsValid() { | ||
return fmt.Errorf("%s is not a valid CountryType", str) | ||
} | ||
return nil | ||
} | ||
|
||
// MarshalGQL writes the country type to the supplied writer | ||
func (e CountryType) MarshalGQL(w io.Writer) { | ||
fmt.Fprint(w, strconv.Quote(e.String())) | ||
} |
Oops, something went wrong.