|
| 1 | +package consistency |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strings" |
| 6 | +) |
| 7 | + |
| 8 | +type Consistency uint16 |
| 9 | + |
| 10 | +const ( |
| 11 | + Any Consistency = 0x00 |
| 12 | + One Consistency = 0x01 |
| 13 | + Two Consistency = 0x02 |
| 14 | + Three Consistency = 0x03 |
| 15 | + Quorum Consistency = 0x04 |
| 16 | + All Consistency = 0x05 |
| 17 | + LocalQuorum Consistency = 0x06 |
| 18 | + EachQuorum Consistency = 0x07 |
| 19 | + LocalOne Consistency = 0x0A |
| 20 | +) |
| 21 | + |
| 22 | +func (c Consistency) String() string { |
| 23 | + switch c { |
| 24 | + case Any: |
| 25 | + return "ANY" |
| 26 | + case One: |
| 27 | + return "ONE" |
| 28 | + case Two: |
| 29 | + return "TWO" |
| 30 | + case Three: |
| 31 | + return "THREE" |
| 32 | + case Quorum: |
| 33 | + return "QUORUM" |
| 34 | + case All: |
| 35 | + return "ALL" |
| 36 | + case LocalQuorum: |
| 37 | + return "LOCAL_QUORUM" |
| 38 | + case EachQuorum: |
| 39 | + return "EACH_QUORUM" |
| 40 | + case LocalOne: |
| 41 | + return "LOCAL_ONE" |
| 42 | + default: |
| 43 | + return fmt.Sprintf("UNKNOWN_CONS_0x%x", uint16(c)) |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +func (c Consistency) MarshalText() (text []byte, err error) { |
| 48 | + return []byte(c.String()), nil |
| 49 | +} |
| 50 | + |
| 51 | +func (c *Consistency) UnmarshalText(text []byte) error { |
| 52 | + switch string(text) { |
| 53 | + case "ANY": |
| 54 | + *c = Any |
| 55 | + case "ONE": |
| 56 | + *c = One |
| 57 | + case "TWO": |
| 58 | + *c = Two |
| 59 | + case "THREE": |
| 60 | + *c = Three |
| 61 | + case "QUORUM": |
| 62 | + *c = Quorum |
| 63 | + case "ALL": |
| 64 | + *c = All |
| 65 | + case "LOCAL_QUORUM": |
| 66 | + *c = LocalQuorum |
| 67 | + case "EACH_QUORUM": |
| 68 | + *c = EachQuorum |
| 69 | + case "LOCAL_ONE": |
| 70 | + *c = LocalOne |
| 71 | + default: |
| 72 | + return fmt.Errorf("invalid consistency %q", string(text)) |
| 73 | + } |
| 74 | + |
| 75 | + return nil |
| 76 | +} |
| 77 | + |
| 78 | +func ParseConsistency(s string) Consistency { |
| 79 | + var c Consistency |
| 80 | + if err := c.UnmarshalText([]byte(strings.ToUpper(s))); err != nil { |
| 81 | + panic(err) |
| 82 | + } |
| 83 | + return c |
| 84 | +} |
| 85 | + |
| 86 | +// ParseConsistencyWrapper wraps gocql.ParseConsistency to provide an err |
| 87 | +// return instead of a panic |
| 88 | +func ParseConsistencyWrapper(s string) (consistency Consistency, err error) { |
| 89 | + err = consistency.UnmarshalText([]byte(strings.ToUpper(s))) |
| 90 | + return |
| 91 | +} |
| 92 | + |
| 93 | +// MustParseConsistency is the same as ParseConsistency except it returns |
| 94 | +// an error (never). It is kept here since breaking changes are not good. |
| 95 | +// DEPRECATED: use ParseConsistency if you want a panic on parse error. |
| 96 | +func MustParseConsistency(s string) (Consistency, error) { |
| 97 | + c, err := ParseConsistencyWrapper(s) |
| 98 | + if err != nil { |
| 99 | + panic(err) |
| 100 | + } |
| 101 | + return c, nil |
| 102 | +} |
| 103 | + |
| 104 | +type SerialConsistency uint16 |
| 105 | + |
| 106 | +const ( |
| 107 | + Serial SerialConsistency = 0x08 |
| 108 | + LocalSerial SerialConsistency = 0x09 |
| 109 | +) |
| 110 | + |
| 111 | +func (s SerialConsistency) String() string { |
| 112 | + switch s { |
| 113 | + case Serial: |
| 114 | + return "SERIAL" |
| 115 | + case LocalSerial: |
| 116 | + return "LOCAL_SERIAL" |
| 117 | + default: |
| 118 | + return fmt.Sprintf("UNKNOWN_SERIAL_CONS_0x%x", uint16(s)) |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +func (s SerialConsistency) MarshalText() (text []byte, err error) { |
| 123 | + return []byte(s.String()), nil |
| 124 | +} |
| 125 | + |
| 126 | +func (s *SerialConsistency) UnmarshalText(text []byte) error { |
| 127 | + switch string(text) { |
| 128 | + case "SERIAL": |
| 129 | + *s = Serial |
| 130 | + case "LOCAL_SERIAL": |
| 131 | + *s = LocalSerial |
| 132 | + default: |
| 133 | + return fmt.Errorf("invalid consistency %q", string(text)) |
| 134 | + } |
| 135 | + |
| 136 | + return nil |
| 137 | +} |
0 commit comments