Browse Source

Consistency no panic (#983)

* frame: add marshaller and unmarshaller for Consistency and SerialConsistency

* authors: update

* frame: ParseConsistencyWrapper renamed to ParseConsistency and ParseConsistency renamed to MustParseConsistency
Michał Matczuk 8 years ago
parent
commit
2416cf340d
3 changed files with 50 additions and 33 deletions
  1. 1 0
      AUTHORS
  2. 49 26
      frame.go
  3. 0 7
      frame_test.go

+ 1 - 0
AUTHORS

@@ -94,4 +94,5 @@ Krishnanand Thommandra <devtkrishna@gmail.com>
 Blake Atkinson <me@blakeatkinson.com>
 Dharmendra Parsaila <d4dharmu@gmail.com>
 Nayef Ghattas <nayef.ghattas@datadoghq.com>
+Michał Matczuk <mmatczuk@gmail.com>
 Ben Krebsbach <ben.krebsbach@gmail.com>

+ 49 - 26
frame.go

@@ -196,45 +196,51 @@ func (c Consistency) String() string {
 	}
 }
 
-func ParseConsistency(s string) Consistency {
-	switch strings.ToUpper(s) {
+func (c Consistency) MarshalText() (text []byte, err error) {
+	return []byte(c.String()), nil
+}
+
+func (c *Consistency) UnmarshalText(text []byte) error {
+	switch string(text) {
 	case "ANY":
-		return Any
+		*c = Any
 	case "ONE":
-		return One
+		*c = One
 	case "TWO":
-		return Two
+		*c = Two
 	case "THREE":
-		return Three
+		*c = Three
 	case "QUORUM":
-		return Quorum
+		*c = Quorum
 	case "ALL":
-		return All
+		*c = All
 	case "LOCAL_QUORUM":
-		return LocalQuorum
+		*c = LocalQuorum
 	case "EACH_QUORUM":
-		return EachQuorum
+		*c = EachQuorum
 	case "LOCAL_ONE":
-		return LocalOne
+		*c = LocalOne
 	default:
-		panic("invalid consistency: " + s)
+		return fmt.Errorf("invalid consistency %q", string(text))
 	}
+
+	return nil
 }
 
-// ParseConsistencyWrapper wraps gocql.ParseConsistency to provide an err
-// return instead of a panic
-func ParseConsistencyWrapper(s string) (consistency Consistency, err error) {
-	defer func() {
-		if r := recover(); r != nil {
-			var ok bool
-			err, ok = r.(error)
-			if !ok {
-				err = fmt.Errorf("ParseConsistencyWrapper: %v", r)
-			}
-		}
-	}()
-	consistency = ParseConsistency(s)
-	return consistency, nil
+func ParseConsistency(s string) (consistency Consistency, err error) {
+	err = consistency.UnmarshalText([]byte(strings.ToUpper(s)))
+	return
+}
+
+// ParseConsistencyWrapper is deprecated use ParseConsistency instead.
+var ParseConsistencyWrapper = ParseConsistency
+
+func MustParseConsistency(s string) Consistency {
+	c, err := ParseConsistency(s)
+	if err != nil {
+		panic(err)
+	}
+	return c
 }
 
 type SerialConsistency uint16
@@ -255,6 +261,23 @@ func (s SerialConsistency) String() string {
 	}
 }
 
+func (s SerialConsistency) MarshalText() (text []byte, err error) {
+	return []byte(s.String()), nil
+}
+
+func (s *SerialConsistency) UnmarshalText(text []byte) error {
+	switch string(text) {
+	case "SERIAL":
+		*s = Serial
+	case "LOCAL_SERIAL":
+		*s = LocalSerial
+	default:
+		return fmt.Errorf("invalid consistency %q", string(text))
+	}
+
+	return nil
+}
+
 const (
 	apacheCassandraTypePrefix = "org.apache.cassandra.db.marshal."
 )

+ 0 - 7
frame_test.go

@@ -97,10 +97,3 @@ func TestFrameReadTooLong(t *testing.T) {
 		t.Fatalf("expected to get header %v got %v", opReady, head.op)
 	}
 }
-
-func TestParseConsistencyErrorInsteadOfPanic(t *testing.T) {
-	_, err := ParseConsistencyWrapper("TEST")
-	if err == nil {
-		t.Fatal("expected ParseConsistencyWrapper error got nil")
-	}
-}