Using go enums

I have this code below but im not sure how the enum's side of things is working with the input like how I can determine the case in the switch to activate. Any thoughts?
package validateInput

import (
"log"
"regexp"
)

const WEATHER_API = "https://api.weatherapi.com/v1"

type InputType int

const (
Zip InputType = iota
AirportCode
)

type WeatherInput struct {
Input string
Type InputType
}

func ValidateInput(wi WeatherInput) bool {
inputCorrect := false
switch wi.Type {
case Zip:
inputCorrect = ValidateZipCode(wi.Input)
return inputCorrect
case AirportCode:
inputCorrect = ValidateAirportCode(wi.Input)
return inputCorrect
default:
inputCorrect = ValidateCity(wi.Input)
return inputCorrect
}
}

func ValidateZipCode(input string) bool {
validateZip, err := regexp.MatchString("/^\\d{5}$", input)

if !validateZip || err != nil {
log.Fatal("zip code is not valid")
return false
}

return true
}

func ValidateAirportCode(input string) bool {
validateAircode, err := regexp.MatchString("/^[A-Za-z]{3}$", input)

if !validateAircode || err != nil {
log.Fatal("airport code is not valid")
return false
}

return true
}

func ValidateCity(input string) bool {
validateCity, err := regexp.MatchString("^[A-Za-z ]*$", input)

if !validateCity || err != nil {
log.Fatal("city is not valid")
return false
}

return true
}
package validateInput

import (
"log"
"regexp"
)

const WEATHER_API = "https://api.weatherapi.com/v1"

type InputType int

const (
Zip InputType = iota
AirportCode
)

type WeatherInput struct {
Input string
Type InputType
}

func ValidateInput(wi WeatherInput) bool {
inputCorrect := false
switch wi.Type {
case Zip:
inputCorrect = ValidateZipCode(wi.Input)
return inputCorrect
case AirportCode:
inputCorrect = ValidateAirportCode(wi.Input)
return inputCorrect
default:
inputCorrect = ValidateCity(wi.Input)
return inputCorrect
}
}

func ValidateZipCode(input string) bool {
validateZip, err := regexp.MatchString("/^\\d{5}$", input)

if !validateZip || err != nil {
log.Fatal("zip code is not valid")
return false
}

return true
}

func ValidateAirportCode(input string) bool {
validateAircode, err := regexp.MatchString("/^[A-Za-z]{3}$", input)

if !validateAircode || err != nil {
log.Fatal("airport code is not valid")
return false
}

return true
}

func ValidateCity(input string) bool {
validateCity, err := regexp.MatchString("^[A-Za-z ]*$", input)

if !validateCity || err != nil {
log.Fatal("city is not valid")
return false
}

return true
}
17 Replies
ErickO
ErickO15mo ago
imma say it, that's garbage don't use it go doesn't have enums, anyone saying "this is how you can have enums" is just doing a scuffed thing go isn't supposed to do will they add them eventually? probably, but it is not a thing now and as such it is not something I'd force specially for 2 damn variables
MD
MD15mo ago
is the validation stuff ok?
ErickO
ErickO15mo ago
type InputType int

const (
Zip InputType = iota
AirportCode
)
type InputType int

const (
Zip InputType = iota
AirportCode
)
all this is doing is saying Zip and AirportCode are of type InputType which is of type int iota starts at 0 and adds 1 to every const declared within the same block so zip is 0 and AirportCode is 1
MD
MD15mo ago
so it's useless in this situation
ErickO
ErickO15mo ago
I mean not entirely useless no tho idk where it comes from where is validateInput being called?
MD
MD15mo ago
ive not got that far yet but the goal is in main.go down the road... validate input, convert input into api call format, get the returned json, pull the json info out and display it in the city page
ErickO
ErickO15mo ago
then it is useless here OMEGALUL
MD
MD15mo ago
im just trying to think of a way to send the input to those based on some condition like some check early on says ok send this to zip code
ErickO
ErickO15mo ago
for that you'd have to parse the user's input I presume
MD
MD15mo ago
I guess check for a starting digit for zip im guessing there are cities in the world with very few letters so input len is a bit odd airport codes are 3 characters
ErickO
ErickO15mo ago
you need to understand better what your inputs are
MD
MD15mo ago
that's all I really know zip is all digits airport is 3 characters city can be any lenghth
ErickO
ErickO15mo ago
if that's all you know...you need to understand them better if the fellas on the API can parse it and use it correctly it's cause it's not arbitrary and each type is unique in some way find the uniqueness
MD
MD15mo ago
yeah this is their doc
MD
MD15mo ago
Weather and Geolocation API - Weather and Geolocation API JSON an...
WeatherAPI.com offer comprehensive Global and Local Weather API, Geo API, Astronomy API and Time Zone API. Simple to use, powerful, fully managed and free!
MD
MD15mo ago
technically they can take multiple forms of input including json (if you pay for it), for me I tried to limit it to cases commonly used like zip code, airport, and city
Jochem
Jochem15mo ago
airport codes are preferably all caps, where city names are just UCFirst also, airport codes conform to a single, known list of abbreviations you can check against where the list of cities is just too long for that
Want results from more Discord servers?
Add your server