Skip to content

Instantly share code, notes, and snippets.

@lummie

lummie/enum.go

Last active Sep 1, 2020
Embed
What would you like to do?
Golang Enum pattern that can be serialized to json
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
}
@tonyalaribe

This comment has been minimized.

Copy link

@tonyalaribe tonyalaribe commented Jun 16, 2018

WOW. Thanks for this.

@nilskuhn

This comment has been minimized.

Copy link

@nilskuhn nilskuhn commented Aug 26, 2018

Really helpful, thanks for sharing!

@mbana

This comment has been minimized.

Copy link

@mbana mbana commented Nov 22, 2018

Cheers

@wgfm

This comment has been minimized.

Copy link

@wgfm wgfm commented Nov 28, 2018

For me, MarshalJSON only works when its receiver is a TaskState, rather than a *TaskState:

func (s TaskState) MarshalJSON() ([]byte, error)
@lummie

This comment has been minimized.

Copy link
Owner Author

@lummie lummie commented Dec 11, 2018

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.

@soolaugust

This comment has been minimized.

Copy link

@soolaugust soolaugust commented Mar 28, 2019

Very Cool, 👍

@dvasilen

This comment has been minimized.

Copy link

@dvasilen dvasilen commented May 2, 2019

Thanks, very helpful!

@xyalan

This comment has been minimized.

Copy link

@xyalan xyalan commented Jul 9, 2019

cool

@nohwnd

This comment has been minimized.

Copy link

@nohwnd nohwnd commented Jul 12, 2019

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"

@difficultwork

This comment has been minimized.

Copy link

@difficultwork difficultwork commented Oct 24, 2019

So cooooool👍

@bhansconnect

This comment has been minimized.

Copy link

@bhansconnect bhansconnect commented Nov 21, 2019

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.

@ghost

This comment has been minimized.

Copy link

@ghost ghost commented Jul 6, 2020

Thank you!

@sylv-io

This comment has been minimized.

Copy link

@sylv-io sylv-io commented Sep 1, 2020

thanks 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.