-
-
Save syntaqx/9a73044aa1fa70236d6bc9d7d7ab5a63 to your computer and use it in GitHub Desktop.
Revisions
-
lummie revised this gist
Dec 11, 2018 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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) { buffer := bytes.NewBufferString(`"`) buffer.WriteString(toString[s]) buffer.WriteString(`"`) return buffer.Bytes(), nil } -
lummie revised this gist
Nov 15, 2018 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 not started yet Created TaskState = iota //Running represents the task has started Running -
lummie revised this gist
Nov 15, 2018 . 1 changed file with 31 additions and 38 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -5,61 +5,54 @@ import ( "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 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 (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 } -
lummie created this gist
Feb 7, 2017 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 }