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:
@@ -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
27
pkg/api/volstate.go
Normal 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"
|
||||
}
|
||||
}
|
||||
59
pkg/api/volstate_jsonenums.go
Normal file
59
pkg/api/volstate_jsonenums.go
Normal 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
35
pkg/api/voltype.go
Normal 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"
|
||||
}
|
||||
}
|
||||
65
pkg/api/voltype_jsonenums.go
Normal file
65
pkg/api/voltype_jsonenums.go
Normal 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
|
||||
}
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user