package enum_example | |
import ( | |
"bytes" | |
"encoding/json" | |
) | |
// TaskState represents the state of task, moving through Created, Running then Finished or Errorred | |
type TaskState int | |
const ( | |
// Created represents the task has been created but not started yet | |
Created TaskState = iota | |
//Running represents the task has started | |
Running | |
// Finished represents the task is complete | |
Finished | |
// Errorred represents the task has encountered a problem and is no longer running | |
Errorred | |
) | |
func (s TaskState) String() string { | |
return toString[s] | |
} | |
var toString = map[TaskState]string{ | |
Created: "Created", | |
Running: "Running", | |
Finished: "Finished", | |
Errorred: "Errorred", | |
} | |
var toID = map[string]TaskState{ | |
"Created": Created, | |
"Running": Running, | |
"Finished": Finished, | |
"Errorred": Errorred, | |
} | |
// MarshalJSON marshals the enum as a quoted json string | |
func (s TaskState) MarshalJSON() ([]byte, error) { | |
buffer := bytes.NewBufferString(`"`) | |
buffer.WriteString(toString[s]) | |
buffer.WriteString(`"`) | |
return buffer.Bytes(), nil | |
} | |
// UnmarshalJSON unmashals a quoted json string to the enum value | |
func (s *TaskState) UnmarshalJSON(b []byte) error { | |
var j string | |
err := json.Unmarshal(b, &j) | |
if err != nil { | |
return err | |
} | |
// Note that if the string cannot be found then it will be set to the zero value, 'Created' in this case. | |
*s = toID[j] | |
return nil | |
} |
Really helpful, thanks for sharing!
Cheers
For me, MarshalJSON only works when its receiver is a TaskState
, rather than a *TaskState
:
func (s TaskState) MarshalJSON() ([]byte, error)
For me, MarshalJSON only works when its receiver is a
TaskState
, rather than a*TaskState
:
Good spot, thanks I'd replaced the original gist with a cleaner example and introduced that bug
Gist fixed.
Very Cool,
Thanks, very helpful!
cool
Very useful, thanks. It is also nice to know that editors support regex replace which allows you to generate the contents of toString
and toId
very easily from the list of enum values.
For example toString
can be generated by copying the list of values, and doing this regex replacement on it:
find: (\w+).*
replace with: $1: "$1"
So cooooool
Another option is:
type TaskState string
const (
// Created represents the task has been created but not started yet
Created TaskState = "Created"
//Running represents the task has started
Running = "Running"
// Finished represents the task is complete
Finished = "Finished"
// Errorred represents the task has encountered a problem and is no longer running
Errorred ="Errored"
)
The one disadvantage is that if you do a bunch a comparisons in Go, they will be string comparisons instead of int. This could effect performance, but for most cases is probably insignificant.
Thank you!
thanks
Thanks!
my two cents:
type Kind int
const (
Sent Kind = iota
Received
Failed
Report
)
func (t Kind) String() string {
return [...]string{"Sent", "Received", "Failed", "Report"}[t]
}
func (t *Kind) FromString(kind string) Kind {
return map[string]Kind{
"Sent": Sent,
"Received": Received,
"Failed": Failed,
"Report": Report,
}[kind]
}
func (t Kind) MarshalJSON() ([]byte, error) {
return json.Marshal(t.String())
}
func (t *Kind) UnMarshalJSON(b []byte) error {
var s string
err := json.Unmarshal(b, &s)
if err != nil {
return err
}
*t = t.FromString(s)
return nil
}
my two cents:
type Kind int const ( Sent Kind = iota Received Failed Report ) func (t Kind) String() string { return [...]string{"Sent", "Received", "Failed", "Report"}[t] } func (t *Kind) FromString(kind string) Kind { return map[string]Kind{ "Sent": Sent, "Received": Received, "Failed": Failed, "Report": Report, }[kind] } func (t Kind) MarshalJSON() ([]byte, error) { return json.Marshal(t.String()) } func (t *Kind) UnMarshalJSON(b []byte) error { var s string err := json.Unmarshal(b, &s) if err != nil { return err } *t = t.FromString(s) return nil }
Nice, the same approach comes to my mind. When you put the maps in a function they are immutable. I think that is a better approach.
Thank you, for this example.
type Kind int
const (
Unknown Kind = iota
Sent
Received
Failed
Report
)
func (t Kind) String() string {
return [...]string{"Unknown", "Sent", "Received", "Failed", "Report"}[t]
}
func (t *Kind) FromString(kind string) Kind {
return map[string]Kind{
"Unknown": Unknown,
"Sent": Sent,
"Received": Received,
"Failed": Failed,
"Report": Report,
}[kind]
}
func (t Kind) MarshalJSON() ([]byte, error) {
return json.Marshal(t.String())
}
func (t *Kind) UnmarshalJSON(b []byte) error {
var s string
err := json.Unmarshal(b, &s)
if err != nil {
return err
}
*t = t.FromString(s)
return nil
}
func (t *Kind) UnMarshalJSON(b []byte) error {
Note that this should be UnmarshalJSON
, without the capital "M" in the middle... other than that, works brilliantly, thanks!
WOW. Thanks for this.