1
0
mirror of https://github.com/gluster/glusterd2.git synced 2026-02-05 12:45:38 +01:00

Convert enums to meaningful json strings in responses

Signed-off-by: Prashanth Pai <ppai@redhat.com>
This commit is contained in:
Prashanth Pai
2017-11-15 23:31:04 +05:30
parent 1cb24fd62d
commit 9479122690
6 changed files with 187 additions and 7 deletions

View File

@@ -84,7 +84,7 @@ func createVolinfo(req *api.VolCreateReq) (*volume.Volinfo, error) {
Password: uuid.NewRandom().String(),
}
v.State = volume.VolStopped
v.State = volume.VolCreated
return v, nil
}

27
pkg/api/volstate.go Normal file
View File

@@ -0,0 +1,27 @@
package api
// VolState is the current state of the volume.
//go:generate jsonenums -type=VolState
type VolState uint16
const (
// VolCreated represents a volume that has been just created and never started.
VolCreated VolState = iota
// VolStarted represents a volume in started state.
VolStarted
// VolStopped represents a volume in stopped state (excluding newly created but never started volumes).
VolStopped
)
func (s VolState) String() string {
switch s {
case VolCreated:
return "created"
case VolStarted:
return "started"
case VolStopped:
return "stopped"
default:
return "invalid VolState"
}
}

View File

@@ -0,0 +1,59 @@
// generated by jsonenums -type=VolState; DO NOT EDIT
package api
import (
"encoding/json"
"fmt"
)
var (
_VolStateNameToValue = map[string]VolState{
"VolCreated": VolCreated,
"VolStarted": VolStarted,
"VolStopped": VolStopped,
}
_VolStateValueToName = map[VolState]string{
VolCreated: "VolCreated",
VolStarted: "VolStarted",
VolStopped: "VolStopped",
}
)
func init() {
var v VolState
if _, ok := interface{}(v).(fmt.Stringer); ok {
_VolStateNameToValue = map[string]VolState{
interface{}(VolCreated).(fmt.Stringer).String(): VolCreated,
interface{}(VolStarted).(fmt.Stringer).String(): VolStarted,
interface{}(VolStopped).(fmt.Stringer).String(): VolStopped,
}
}
}
// MarshalJSON is generated so VolState satisfies json.Marshaler.
func (r VolState) MarshalJSON() ([]byte, error) {
if s, ok := interface{}(r).(fmt.Stringer); ok {
return json.Marshal(s.String())
}
s, ok := _VolStateValueToName[r]
if !ok {
return nil, fmt.Errorf("invalid VolState: %d", r)
}
return json.Marshal(s)
}
// UnmarshalJSON is generated so VolState satisfies json.Unmarshaler.
func (r *VolState) UnmarshalJSON(data []byte) error {
var s string
if err := json.Unmarshal(data, &s); err != nil {
return fmt.Errorf("VolState should be a string, got %s", data)
}
v, ok := _VolStateNameToValue[s]
if !ok {
return fmt.Errorf("invalid VolState %q", s)
}
*r = v
return nil
}

35
pkg/api/voltype.go Normal file
View File

@@ -0,0 +1,35 @@
package api
// VolType is the type of volume.
//go:generate jsonenums -type=VolType
type VolType uint16
const (
// Distribute is a plain distribute volume
Distribute VolType = iota
// Replicate is plain replicate volume
Replicate
// Disperse is a plain erasure coded volume
Disperse
// DistReplicate is a distribute-replicate volume
DistReplicate
// DistDisperse is a distribute-'erasure coded' volume
DistDisperse
)
func (t VolType) String() string {
switch t {
case Distribute:
return "distribute"
case Replicate:
return "replicate"
case Disperse:
return "disperse"
case DistReplicate:
return "distribute-replicate"
case DistDisperse:
return "distribute-disperse"
default:
return "invalid VolState"
}
}

View File

@@ -0,0 +1,65 @@
// generated by jsonenums -type=VolType; DO NOT EDIT
package api
import (
"encoding/json"
"fmt"
)
var (
_VolTypeNameToValue = map[string]VolType{
"Distribute": Distribute,
"Replicate": Replicate,
"Disperse": Disperse,
"DistReplicate": DistReplicate,
"DistDisperse": DistDisperse,
}
_VolTypeValueToName = map[VolType]string{
Distribute: "Distribute",
Replicate: "Replicate",
Disperse: "Disperse",
DistReplicate: "DistReplicate",
DistDisperse: "DistDisperse",
}
)
func init() {
var v VolType
if _, ok := interface{}(v).(fmt.Stringer); ok {
_VolTypeNameToValue = map[string]VolType{
interface{}(Distribute).(fmt.Stringer).String(): Distribute,
interface{}(Replicate).(fmt.Stringer).String(): Replicate,
interface{}(Disperse).(fmt.Stringer).String(): Disperse,
interface{}(DistReplicate).(fmt.Stringer).String(): DistReplicate,
interface{}(DistDisperse).(fmt.Stringer).String(): DistDisperse,
}
}
}
// MarshalJSON is generated so VolType satisfies json.Marshaler.
func (r VolType) MarshalJSON() ([]byte, error) {
if s, ok := interface{}(r).(fmt.Stringer); ok {
return json.Marshal(s.String())
}
s, ok := _VolTypeValueToName[r]
if !ok {
return nil, fmt.Errorf("invalid VolType: %d", r)
}
return json.Marshal(s)
}
// UnmarshalJSON is generated so VolType satisfies json.Unmarshaler.
func (r *VolType) UnmarshalJSON(data []byte) error {
var s string
if err := json.Unmarshal(data, &s); err != nil {
return fmt.Errorf("VolType should be a string, got %s", data)
}
v, ok := _VolTypeNameToValue[s]
if !ok {
return fmt.Errorf("invalid VolType %q", s)
}
*r = v
return nil
}

View File

@@ -22,12 +22,6 @@ type BrickStatus struct {
Port int `json:"port"`
}
// VolState is the current state of the volume.
type VolState uint16
// VolType is the type of volume.
type VolType uint16
// VolumeInfo contains static information about the volume.
// Clients should NOT use this struct directly.
type VolumeInfo struct {