Skip to content

Instantly share code, notes, and snippets.

@lummie
Last active September 16, 2024 18:03
Show Gist options
  • Save lummie/7f5c237a17853c031a57277371528e87 to your computer and use it in GitHub Desktop.
Save lummie/7f5c237a17853c031a57277371528e87 to your computer and use it in GitHub Desktop.
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
Copy link

WOW. Thanks for this.

@nilskuhn
Copy link

Really helpful, thanks for sharing!

@mbana
Copy link

mbana commented Nov 22, 2018

Cheers

@wgfm
Copy link

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
Copy link
Author

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
Copy link

Very Cool, 👍

@dvasilen
Copy link

dvasilen commented May 2, 2019

Thanks, very helpful!

@xyalan
Copy link

xyalan commented Jul 9, 2019

cool

@nohwnd
Copy link

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
Copy link

So cooooool👍

@bhansconnect
Copy link

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.

Copy link

ghost commented Jul 6, 2020

Thank you!

@sylv-io
Copy link

sylv-io commented Sep 1, 2020

thanks 👍

@hznut
Copy link

hznut commented Sep 16, 2021

Thanks!

@subutux
Copy link

subutux commented Oct 1, 2021

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
}

@alex21289
Copy link

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.

@jaxsong
Copy link

jaxsong commented Dec 27, 2021

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
}

@adev73
Copy link

adev73 commented Jul 14, 2022

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!

@bycepto
Copy link

bycepto commented Nov 4, 2023

I ran into an edge case today - you need to add the following method if you plan to unmarshal json into a map that uses a custom enum as a key (e.g. map[TaskState]string):

func (s *TaskState) UnmarshalText(b []byte) error {
	return s.UnmarshalJSON(b)
}

Without it you will get an error like this when you unmarshal:

json: cannot unmarshal number Sent into Go struct field <some field with type map[TaskState]string> of type TaskState

FWIW I found the docs on this confusing. The paragraph below from https://pkg.go.dev/encoding/json#Unmarshal seems to indicate that implementing json.Unmarshaler is sufficient (emphasis mine):

To unmarshal a JSON object into a map, Unmarshal first establishes a map to use. If the map is nil, Unmarshal allocates a new map. Otherwise Unmarshal reuses the existing map, keeping existing entries. Unmarshal then stores key-value pairs from the JSON object into the map. The map's key type must either be any string type, an integer, implement json.Unmarshaler, or implement encoding.TextUnmarshaler.

However, this source code comment indicates otherwise: https://cs.opensource.google/go/go/+/refs/tags/go1.21.3:src/encoding/json/decode.go;drc=b9b8cecbfc72168ca03ad586cc2ed52b0e8db409;l=630

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment