Boilerplate code
Let’s prepare some convenient helpers for the journey ahead. We know we will need writing/reading to/from files and that we will need to transform some arguments, passed as strings, into Go types. Let’s deal with the latter first since this is a very trivial task.
Converting string to enum values
We are going to create two functions: strToPhoneType
and strToDepartment
. They look similar since we are going to check the value of the string and derive an enum value from it. Let’s start with strToPhoneType
.
We know that the PhoneType
enum contains the values TYPE_HOME
, TYPE_MOBILE
, TYPE_WORK
, and TYPE_UNSPECIFIED
. TYPE_UNSPECIFIED
is the default value of PhoneType
since enums have 0 as their default value. Conveniently, Golang initializes variables with 0 values. Thus, we will simply check for the values of home
, mobile
, and work
. If the string does not contain anything or a value that isn’t one of these, the phone
type will be considered...