Skip to content

Instantly share code, notes, and snippets.

@syntaqx
Forked from lummie/enum.go
Created September 29, 2020 01:36
Show Gist options
  • Select an option

  • Save syntaqx/9a73044aa1fa70236d6bc9d7d7ab5a63 to your computer and use it in GitHub Desktop.

Select an option

Save syntaqx/9a73044aa1fa70236d6bc9d7d7ab5a63 to your computer and use it in GitHub Desktop.

Revisions

  1. @lummie lummie revised this gist Dec 11, 2018. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions enum.go
    Original file line number Diff line number Diff line change
    @@ -38,9 +38,9 @@ var toID = map[string]TaskState{
    }

    // MarshalJSON marshals the enum as a quoted json string
    func (s *TaskState) MarshalJSON() ([]byte, error) {
    func (s TaskState) MarshalJSON() ([]byte, error) {
    buffer := bytes.NewBufferString(`"`)
    buffer.WriteString(toString[*s])
    buffer.WriteString(toString[s])
    buffer.WriteString(`"`)
    return buffer.Bytes(), nil
    }
  2. @lummie lummie revised this gist Nov 15, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion enum.go
    Original file line number Diff line number Diff line change
    @@ -9,7 +9,7 @@ import (
    type TaskState int

    const (
    // Created represents the task has been created but nt started yet
    // Created represents the task has been created but not started yet
    Created TaskState = iota
    //Running represents the task has started
    Running
  3. @lummie lummie revised this gist Nov 15, 2018. 1 changed file with 31 additions and 38 deletions.
    69 changes: 31 additions & 38 deletions enum.go
    Original file line number Diff line number Diff line change
    @@ -5,61 +5,54 @@ import (
    "encoding/json"
    )

    type DataType int

    // TaskState represents the state of task, moving through Created, Running then Finished or Errorred
    type TaskState int

    const (
    DTNull DataType = iota
    DTBool
    DTInt32
    DTInt64
    DTFloat32
    DTFloat64
    DTString
    DTBytes
    // Created represents the task has been created but nt 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 (d DataType) String() string {
    return dataTypesId[d]
    func (s TaskState) String() string {
    return toString[s]
    }

    var dataTypesId = map[DataType]string{
    DTNull: "DTNull",
    DTBool: "DTBool",
    DTInt32: "DTInt32",
    DTInt64: "DTInt64",
    DTFloat32: "DTFloat32",
    DTFloat64: "DTFloat64",
    DTString: "DTString",
    DTBytes: "DTBytes",
    var toString = map[TaskState]string{
    Created: "Created",
    Running: "Running",
    Finished: "Finished",
    Errorred: "Errorred",
    }

    var dataTypesName = map[string]DataType{
    "DTNull": DTNull,
    "DTBool": DTBool,
    "DTInt32": DTInt32,
    "DTInt64": DTInt64,
    "DTFloat32": DTFloat32,
    "DTFloat64": DTFloat64,
    "DTString": DTString,
    "DTBytes": DTBytes,
    var toID = map[string]TaskState{
    "Created": Created,
    "Running": Running,
    "Finished": Finished,
    "Errorred": Errorred,
    }

    func (d *DataType) MarshalJSON() ([]byte, error) {
    // MarshalJSON marshals the enum as a quoted json string
    func (s *TaskState) MarshalJSON() ([]byte, error) {
    buffer := bytes.NewBufferString(`"`)
    buffer.WriteString(dataTypesId[*d])
    buffer.WriteString(toString[*s])
    buffer.WriteString(`"`)
    return buffer.Bytes(), nil
    }

    func (d *DataType) UnmarshalJSON(b []byte) error {
    // unmarshal as string
    var s string
    err := json.Unmarshal(b, &s)
    // 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
    }
    // lookup value
    *d = dataTypesName[s]
    // 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
    }
  4. @lummie lummie created this gist Feb 7, 2017.
    65 changes: 65 additions & 0 deletions enum.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,65 @@
    package enum_example

    import (
    "bytes"
    "encoding/json"
    )

    type DataType int


    const (
    DTNull DataType = iota
    DTBool
    DTInt32
    DTInt64
    DTFloat32
    DTFloat64
    DTString
    DTBytes
    )

    func (d DataType) String() string {
    return dataTypesId[d]
    }

    var dataTypesId = map[DataType]string{
    DTNull: "DTNull",
    DTBool: "DTBool",
    DTInt32: "DTInt32",
    DTInt64: "DTInt64",
    DTFloat32: "DTFloat32",
    DTFloat64: "DTFloat64",
    DTString: "DTString",
    DTBytes: "DTBytes",
    }

    var dataTypesName = map[string]DataType{
    "DTNull": DTNull,
    "DTBool": DTBool,
    "DTInt32": DTInt32,
    "DTInt64": DTInt64,
    "DTFloat32": DTFloat32,
    "DTFloat64": DTFloat64,
    "DTString": DTString,
    "DTBytes": DTBytes,
    }

    func (d *DataType) MarshalJSON() ([]byte, error) {
    buffer := bytes.NewBufferString(`"`)
    buffer.WriteString(dataTypesId[*d])
    buffer.WriteString(`"`)
    return buffer.Bytes(), nil
    }

    func (d *DataType) UnmarshalJSON(b []byte) error {
    // unmarshal as string
    var s string
    err := json.Unmarshal(b, &s)
    if err != nil {
    return err
    }
    // lookup value
    *d = dataTypesName[s]
    return nil
    }