Browse Source

json query int64

ls 3 years ago
parent
commit
f4c2eb2871
2 changed files with 27 additions and 1 deletions
  1. 26 0
      json.go
  2. 1 1
      wechat/client.go

+ 26 - 0
json.go

@@ -81,6 +81,15 @@ func (jq *JSONQuery) Int(s ...string) (int, error) {
 	return intFromInterface(val)
 }
 
+// Int64 extracts an int64 from the JsonQuery
+func (jq *JSONQuery) Int64(s ...string) (int64, error) {
+	val, err := rquery(jq.data, s...)
+	if err != nil {
+		return 0, err
+	}
+	return int64FromInterface(val)
+}
+
 // String extracts a string from the JsonQuery
 func (jq *JSONQuery) String(s ...string) (string, error) {
 	val, err := rquery(jq.data, s...)
@@ -321,6 +330,23 @@ func intFromInterface(val interface{}) (int, error) {
 	return 0, fmt.Errorf("Expected numeric value for Int, got \"%v\"", val)
 }
 
+func int64FromInterface(val interface{}) (int64, error) {
+	switch val.(type) {
+	case float64:
+		return int64(val.(float64)), nil
+	case string:
+		ival, err := strconv.ParseFloat(val.(string), 64)
+		if err == nil {
+			return int64(ival), nil
+		}
+	case int:
+		return int64(val.(int)), nil
+	case int64:
+		return val.(int64), nil
+	}
+	return 0, fmt.Errorf("Expected numeric value for Int, got \"%v\"", val)
+}
+
 // objectFromInterface converts an interface{} to a map[string]interface{} and returns an error if types don't match.
 func objectFromInterface(val interface{}) (map[string]interface{}, error) {
 	switch val.(type) {

+ 1 - 1
wechat/client.go

@@ -306,7 +306,7 @@ func (wc Client) SendTemplateMessage(template TemplateMessage) (jq *myth.JSONQue
 func (wc Client) TemplateMessageReslut(jq *myth.JSONQuery) (code, id int, err error) {
 	code, _ = jq.Int(`errcode`)
 	errmsg, _ := jq.String(`errmsg`)
-	id, _ = jq.Int(`msgid`)
+	id, _ = jq.Int64(`msgid`)
 	if code != ErrReqOk {
 		err = errors.New(errmsg)
 	}