Susumu Yata
null+****@clear*****
Thu Jul 6 13:45:59 JST 2017
Susumu Yata 2017-07-06 13:45:59 +0900 (Thu, 06 Jul 2017) New Revision: 360086588771d4d85967b60ea56e6657aa106089 https://github.com/groonga/grnci/commit/360086588771d4d85967b60ea56e6657aa106089 Message: Add comments and publish function for JSON encoding. These functions are useful to compose parameters such as --filter. Modified files: v2/command.go v2/command_test.go v2/db.go v2/error.go v2/error_test.go v2/handler.go v2/json.go v2/json_test.go Modified: v2/command.go (+46 -21) =================================================================== --- v2/command.go 2017-07-05 18:46:57 +0900 (4ebf626) +++ v2/command.go 2017-07-06 13:45:59 +0900 (73ac17b) @@ -10,6 +10,9 @@ import ( "strings" ) +// commandSpaces is a set of characters handled as spaces in commands. +const commandSpaces = "\t\n\r " + // formatParamValue is the default function to format a parameter value. func formatParamValue(key string, value interface{}) (string, error) { switch v := reflect.ValueOf(value); v.Kind() { @@ -121,7 +124,7 @@ func formatParamMatchColumns(key string, value interface{}) (string, error) { // formatParamJSON returns the JSON-encoded value (delete --key). func formatParamJSON(key string, value interface{}) (string, error) { - return jsonEncodeValue(reflect.ValueOf(value)), nil + return EncodeJSON(value), nil } type paramFormat struct { @@ -651,14 +654,12 @@ func NewCommand(name string, params map[string]interface{}) (*Command, error) { // unescapeCommandByte returns an unescaped space character. func unescapeCommandByte(b byte) byte { switch b { - case 'b': - return '\b' case 't': return '\t' - case 'r': - return '\r' case 'n': return '\n' + case 'r': + return '\r' default: return b } @@ -670,7 +671,7 @@ func tokenizeCommand(cmd string) ([]string, error) { var token []byte s := cmd for { - s = strings.TrimLeft(s, " \t\r\n") + s = strings.TrimLeft(s, commandSpaces) if s == "" { break } @@ -701,7 +702,7 @@ func tokenizeCommand(cmd string) ([]string, error) { Loop: for ; i < len(s); i++ { switch s[i] { - case ' ', '\t', '\r', '\n', '"', '\'': + case ' ', '\t', '\n', '\r', '"', '\'': break Loop case '\\': i++ @@ -846,7 +847,6 @@ func (c *Command) SetParam(key string, value interface{}) error { if err != nil { return EnhanceError(err, map[string]interface{}{ "name": c.name, - "key": key, }) } c.params[pf.key] = fv @@ -883,18 +883,16 @@ func (c *Command) String() string { cmd = append(cmd, " '"...) for i := 0; i < len(v); i++ { switch v[i] { - case '\'': - cmd = append(cmd, `\'`...) - case '\\': - cmd = append(cmd, `\\`...) - case '\b': - cmd = append(cmd, `\b`...) case '\t': cmd = append(cmd, `\t`...) - case '\r': - cmd = append(cmd, `\r`...) case '\n': cmd = append(cmd, `\n`...) + case '\r': + cmd = append(cmd, `\r`...) + case '\'': + cmd = append(cmd, `\'`...) + case '\\': + cmd = append(cmd, `\\`...) default: cmd = append(cmd, v[i]) } @@ -991,12 +989,31 @@ func (br *commandBodyReader) Read(p []byte) (n int, err error) { return } -// CommandReader is a reader for commands. +// CommandReader is designed to read commands from the underlying io.Reader. +// +// The following is an example of reading commands from a dump file. +// +// f, err := os.Open("db.dump") +// if err != nil { +// // Failed to open the dump file. +// } +// defer f.Close() +// cr := grnci.NewCommandReader(f) +// for { +// cmd, err := cr.Read() +// if err != nil { +// if err != io.EOF { +// // Failed to read or parse a command. +// } +// break +// } +// // Do something using cmd. +// } type CommandReader struct { reader io.Reader // Underlying reader buf []byte // Buffer left []byte // Unprocessed bytes in buf - err error // Last error + err error // Last reader error } // NewCommandReader returns a new CommandReader. @@ -1013,6 +1030,7 @@ func (cr *CommandReader) fill() error { return cr.err } if len(cr.left) == len(cr.buf) { + // Extend the buffer because it is full. cr.buf = make([]byte, len(cr.buf)*2) } copy(cr.buf, cr.left) @@ -1080,8 +1098,15 @@ func (cr *CommandReader) readLine() ([]byte, error) { } } -// Read reads the next command. -// If the command has a body, its whole content must be read before the next Read. +// Read reads the next command and returns the result of ParseCommand. +// If the command has a body, the whole content must be read before the next Read. +// +// The possible errors are as follows: +// - the next command is not available or +// - ParseCommand returns an error. +// If the underlying io.Reader returns io.EOF and the read bytes are exhausted, +// Read returns io.EOF. +// Otherwise, Read returns *Error. func (cr *CommandReader) Read() (*Command, error) { if len(cr.left) == 0 && cr.err != nil { return nil, cr.err @@ -1091,7 +1116,7 @@ func (cr *CommandReader) Read() (*Command, error) { if err != nil { return nil, err } - cmd := bytes.TrimLeft(line, " \t\r\n") + cmd := bytes.TrimLeft(line, commandSpaces) if len(cmd) != 0 { cmd, err := ParseCommand(string(cmd)) if err != nil { Modified: v2/command_test.go (+3 -1) =================================================================== --- v2/command_test.go 2017-07-05 18:46:57 +0900 (3cda9db) +++ v2/command_test.go 2017-07-06 13:45:59 +0900 (c7420e0) @@ -411,7 +411,9 @@ load --table Tbl `; actual != want { t.Fatalf("io.ReadAll failed: actual = %s, want = %s", actual, want) } - if _, err := cr.Read(); err != io.EOF { + if _, err := cr.Read(); err == nil { t.Fatalf("cr.Read wongly succeeded") + } else if err != io.EOF { + t.Fatalf("cr.Read failed: %v", err) } } Modified: v2/db.go (+1 -1) =================================================================== --- v2/db.go 2017-07-05 18:46:57 +0900 (75b0ff0) +++ v2/db.go 2017-07-06 13:45:59 +0900 (b51a824) @@ -493,7 +493,7 @@ func (db *DB) appendRow(body []byte, row reflect.Value, cfs []*ColumnField) []by if i != 0 { body = append(body, ',') } - body = jsonAppendValue(body, row.Field(fi.Index)) + body = AppendJSONValue(body, row.Field(fi.Index)) } body = append(body, ']') return body Modified: v2/error.go (+3 -3) =================================================================== --- v2/error.go 2017-07-05 18:46:57 +0900 (a11e486) +++ v2/error.go 2017-07-06 13:45:59 +0900 (6358757) @@ -215,7 +215,7 @@ type Error struct { } // NewError returns a new Error. -func NewError(code int, data map[string]interface{}) error { +func NewError(code int, data map[string]interface{}) *Error { err := &Error{ Code: code, Text: getCodeText(code), @@ -228,14 +228,14 @@ func NewError(code int, data map[string]interface{}) error { } // EnhanceError adds data to err and returns the modified Error. -func EnhanceError(err error, data map[string]interface{}) error { +func EnhanceError(err error, data map[string]interface{}) *Error { if e, ok := err.(*Error); ok { for k, v := range data { e.Data[k] = v } return e } - e := NewError(UnknownError, data).(*Error) + e := NewError(UnknownError, data) if _, ok := e.Data["error"]; !ok { data["error"] = err.Error() } Modified: v2/error_test.go (+3 -3) =================================================================== --- v2/error_test.go 2017-07-05 18:46:57 +0900 (0069ca8) +++ v2/error_test.go 2017-07-06 13:45:59 +0900 (8cc4292) @@ -7,7 +7,7 @@ func TestNewError(t *testing.T) { "string": "value", "int": 100, } - err := NewError(InvalidAddress, data).(*Error) + err := NewError(InvalidAddress, data) if err.Code != InvalidAddress { t.Fatalf("NewError failed: Code: actual = %d, want = %d", err.Code, InvalidAddress) @@ -33,8 +33,8 @@ func TestEnhanceError(t *testing.T) { "int": 1000, "float": 1.0, } - err := NewError(InvalidAddress, data).(*Error) - err = EnhanceError(err, newData).(*Error) + err := NewError(InvalidAddress, data) + err = EnhanceError(err, newData) if err.Code != InvalidAddress { t.Fatalf("NewError failed: Code: actual = %d, want = %d", err.Code, InvalidAddress) Modified: v2/handler.go (+8 -0) =================================================================== --- v2/handler.go 2017-07-05 18:46:57 +0900 (8fcb3b2) +++ v2/handler.go 2017-07-06 13:45:59 +0900 (7f66ca0) @@ -4,8 +4,16 @@ import "io" // Handler defines the required methods of DB clients and handles. type Handler interface { + // Exec parses cmd, sends the parsed command and returns the response. Exec(cmd string, body io.Reader) (Response, error) + + // Invoke assembles name and params into a command, + // sends the command and returns the response. Invoke(name string, params map[string]interface{}, body io.Reader) (Response, error) + + // Query sends cmd and returns the response. Query(cmd *Command) (Response, error) + + // Close closes the underlying connections or handles. Close() error } Modified: v2/json.go (+60 -57) =================================================================== --- v2/json.go 2017-07-05 18:46:57 +0900 (ac82249) +++ v2/json.go 2017-07-06 13:45:59 +0900 (7f4d237) @@ -6,28 +6,28 @@ import ( "time" ) -// jsonAppendBool appends the JSON-encoded v to buf and returns the extended buffer. -func jsonAppendBool(buf []byte, v bool) []byte { +// AppendJSONBool appends the JSON-encoded v to buf and returns the extended buffer. +func AppendJSONBool(buf []byte, v bool) []byte { return strconv.AppendBool(buf, v) } -// jsonAppendInt appends the JSON-encoded v to buf and returns the extended buffer. -func jsonAppendInt(buf []byte, v int64) []byte { +// AppendJSONInt appends the JSON-encoded v to buf and returns the extended buffer. +func AppendJSONInt(buf []byte, v int64) []byte { return strconv.AppendInt(buf, v, 10) } -// jsonAppendUint appends the JSON-encoded v to buf and returns the extended buffer. -func jsonAppendUint(buf []byte, v uint64) []byte { +// AppendJSONUint appends the JSON-encoded v to buf and returns the extended buffer. +func AppendJSONUint(buf []byte, v uint64) []byte { return strconv.AppendUint(buf, v, 10) } -// jsonAppendFloat appands the JSON-encoded v to buf and returns the extended buffer. -func jsonAppendFloat(buf []byte, v float64, bitSize int) []byte { +// AppendJSONFloat appands the JSON-encoded v to buf and returns the extended buffer. +func AppendJSONFloat(buf []byte, v float64, bitSize int) []byte { return strconv.AppendFloat(buf, v, 'g', -1, bitSize) } -// jsonAppendString appends the JSON-encoded v to buf and returns the extended buffer. -func jsonAppendString(buf []byte, v string) []byte { +// AppendJSONString appends the JSON-encoded v to buf and returns the extended buffer. +func AppendJSONString(buf []byte, v string) []byte { buf = append(buf, '"') for i := 0; i < len(v); i++ { switch v[i] { @@ -52,8 +52,8 @@ func jsonAppendString(buf []byte, v string) []byte { return append(buf, '"') } -// jsonAppendTime appends the JSON-encoded v to buf and returns the extended buffer. -func jsonAppendTime(buf []byte, v time.Time) []byte { +// AppendJSONTime appends the JSON-encoded v to buf and returns the extended buffer. +func AppendJSONTime(buf []byte, v time.Time) []byte { buf = strconv.AppendInt(buf, v.Unix(), 10) usec := v.Nanosecond() / 1000 if usec != 0 { @@ -74,8 +74,8 @@ func jsonAppendTime(buf []byte, v time.Time) []byte { return buf } -// jsonAppendGeo appends the JSON-encoded v to buf and returns the extended buffer. -func jsonAppendGeo(buf []byte, v Geo) []byte { +// AppendJSONGeo appends the JSON-encoded v to buf and returns the extended buffer. +func AppendJSONGeo(buf []byte, v Geo) []byte { buf = append(buf, '"') buf = strconv.AppendInt(buf, int64(v.Lat), 10) buf = append(buf, ',') @@ -83,28 +83,28 @@ func jsonAppendGeo(buf []byte, v Geo) []byte { return append(buf, '"') } -// jsonAppendValue appends the JSON-encoded v to buf and returns the extended buffer. -// If the type of v is unsupported, it appends "null". -func jsonAppendValue(buf []byte, v reflect.Value) []byte { +// AppendJSONValue appends the JSON-encoded v to buf and returns the extended buffer. +// If the type of v is unsupported, AppendJSONValue appends "null". +func AppendJSONValue(buf []byte, v reflect.Value) []byte { switch v.Kind() { case reflect.Bool: - return jsonAppendBool(buf, v.Bool()) + return AppendJSONBool(buf, v.Bool()) case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: - return jsonAppendInt(buf, v.Int()) + return AppendJSONInt(buf, v.Int()) case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: - return jsonAppendUint(buf, v.Uint()) + return AppendJSONUint(buf, v.Uint()) case reflect.Float32: - return jsonAppendFloat(buf, v.Float(), 32) + return AppendJSONFloat(buf, v.Float(), 32) case reflect.Float64: - return jsonAppendFloat(buf, v.Float(), 64) + return AppendJSONFloat(buf, v.Float(), 64) case reflect.String: - return jsonAppendString(buf, v.String()) + return AppendJSONString(buf, v.String()) case reflect.Struct: switch v := v.Interface().(type) { case time.Time: - return jsonAppendTime(buf, v) + return AppendJSONTime(buf, v) case Geo: - return jsonAppendGeo(buf, v) + return AppendJSONGeo(buf, v) default: return append(buf, "null"...) } @@ -112,7 +112,7 @@ func jsonAppendValue(buf []byte, v reflect.Value) []byte { if v.IsNil() { return append(buf, "null"...) } - return jsonAppendValue(buf, v.Elem()) + return AppendJSONValue(buf, v.Elem()) case reflect.Array: buf = append(buf, '[') n := v.Len() @@ -120,7 +120,7 @@ func jsonAppendValue(buf []byte, v reflect.Value) []byte { if i != 0 { buf = append(buf, ',') } - buf = jsonAppendValue(buf, v.Index(i)) + buf = AppendJSONValue(buf, v.Index(i)) } return append(buf, ']') case reflect.Slice: @@ -133,7 +133,7 @@ func jsonAppendValue(buf []byte, v reflect.Value) []byte { if i != 0 { buf = append(buf, ',') } - buf = jsonAppendValue(buf, v.Index(i)) + buf = AppendJSONValue(buf, v.Index(i)) } return append(buf, ']') default: @@ -141,55 +141,58 @@ func jsonAppendValue(buf []byte, v reflect.Value) []byte { } } -// jsonAppend appends the JSON-encoded v to buf and returns the extended buffer. -// If the type of v is unsupported, it appends "null". -func jsonAppend(buf []byte, v interface{}) []byte { - return jsonAppendValue(buf, reflect.ValueOf(v)) +// AppendJSON appends the JSON-encoded v to buf and returns the extended buffer. +// If the type of v is unsupported, AppendJSON appends "null". +func AppendJSON(buf []byte, v interface{}) []byte { + if v == nil { + return append(buf, "null"...) + } + return AppendJSONValue(buf, reflect.ValueOf(v)) } -// jsonEncodeBool returns the JSON-encoded v. -func jsonEncodeBool(v bool) string { +// EncodeJSONBool returns the JSON-encoded v. +func EncodeJSONBool(v bool) string { return strconv.FormatBool(v) } -// jsonEncodeInt returns the JSON-encoded v. -func jsonEncodeInt(v int64) string { +// EncodeJSONInt returns the JSON-encoded v. +func EncodeJSONInt(v int64) string { return strconv.FormatInt(v, 10) } -// jsonEncodeUint returns the JSON-encoded v. -func jsonEncodeUint(v uint64) string { +// EncodeJSONUint returns the JSON-encoded v. +func EncodeJSONUint(v uint64) string { return strconv.FormatUint(v, 10) } -// jsonEncodeFloat returns the JSON-encoded v. -func jsonEncodeFloat(v float64, bitSize int) string { +// EncodeJSONFloat returns the JSON-encoded v. +func EncodeJSONFloat(v float64, bitSize int) string { return strconv.FormatFloat(v, 'g', -1, bitSize) } -// jsonEncodeString returns the JSON-encoded v. -func jsonEncodeString(v string) string { - return string(jsonAppendString(nil, v)) +// EncodeJSONString returns the JSON-encoded v. +func EncodeJSONString(v string) string { + return string(AppendJSONString(nil, v)) } -// jsonEncodeTime returns the JSON-encoded v. -func jsonEncodeTime(v time.Time) string { - return string(jsonAppendTime(nil, v)) +// EncodeJSONTime returns the JSON-encoded v. +func EncodeJSONTime(v time.Time) string { + return string(AppendJSONTime(nil, v)) } -// jsonEncodeGeo returns the JSON-encoded v. -func jsonEncodeGeo(v Geo) string { - return string(jsonAppendGeo(nil, v)) +// EncodeJSONGeo returns the JSON-encoded v. +func EncodeJSONGeo(v Geo) string { + return string(AppendJSONGeo(nil, v)) } -// jsonEncodeValue returns the JSON-encoded v. -// If the type of v is unsupported, it returns "null". -func jsonEncodeValue(v reflect.Value) string { - return string(jsonAppendValue(nil, v)) +// EncodeJSONValue returns the JSON-encoded v. +// If the type of v is unsupported, EncodeJSONValue returns "null". +func EncodeJSONValue(v reflect.Value) string { + return string(AppendJSONValue(nil, v)) } -// jsonEncode returns the JSON-encoded v. -// If the type of v is unsupported, it returns "null". -func jsonEncode(v interface{}) string { - return jsonEncodeValue(reflect.ValueOf(v)) +// EncodeJSON returns the JSON-encoded v. +// If the type of v is unsupported, EncodeJSON returns "null". +func EncodeJSON(v interface{}) string { + return string(AppendJSON(nil, v)) } Modified: v2/json_test.go (+182 -182) =================================================================== --- v2/json_test.go 2017-07-05 18:46:57 +0900 (c1131ed) +++ v2/json_test.go 2017-07-06 13:45:59 +0900 (074f131) @@ -6,385 +6,385 @@ import ( "time" ) -func TestJSONAppendBool(t *testing.T) { +func TestAppendJSONBool(t *testing.T) { var buf []byte var want string - buf = jsonAppendBool(buf, true) + buf = AppendJSONBool(buf, true) if want += "true"; string(buf) != want { - t.Fatalf("jsonAppendBool failed: actual = %s, want = %s", buf, want) + t.Fatalf("AppendJSONBool failed: actual = %s, want = %s", buf, want) } - buf = jsonAppendBool(buf, false) + buf = AppendJSONBool(buf, false) if want += "false"; string(buf) != want { - t.Fatalf("jsonAppendBool failed: actual = %s, want = %s", buf, want) + t.Fatalf("AppendJSONBool failed: actual = %s, want = %s", buf, want) } } -func TestJSONAppendInt(t *testing.T) { +func TestAppendJSONInt(t *testing.T) { var buf []byte var want string - buf = jsonAppendInt(buf, 0) + buf = AppendJSONInt(buf, 0) if want += "0"; string(buf) != want { - t.Fatalf("jsonAppendInt failed: actual = %s, want = %s", buf, want) + t.Fatalf("AppendJSONInt failed: actual = %s, want = %s", buf, want) } - buf = jsonAppendInt(buf, 9223372036854775807) + buf = AppendJSONInt(buf, 9223372036854775807) if want += "9223372036854775807"; string(buf) != want { - t.Fatalf("jsonAppendInt failed: actual = %s, want = %s", buf, want) + t.Fatalf("AppendJSONInt failed: actual = %s, want = %s", buf, want) } - buf = jsonAppendInt(buf, -9223372036854775808) + buf = AppendJSONInt(buf, -9223372036854775808) if want += "-9223372036854775808"; string(buf) != want { - t.Fatalf("jsonAppendInt failed: actual = %s, want = %s", buf, want) + t.Fatalf("AppendJSONInt failed: actual = %s, want = %s", buf, want) } } -func TestJSONAppendUint(t *testing.T) { +func TestAppendJSONUint(t *testing.T) { var buf []byte var want string - buf = jsonAppendUint(buf, 0) + buf = AppendJSONUint(buf, 0) if want += "0"; string(buf) != want { - t.Fatalf("jsonAppendUint failed: actual = %s, want = %s", buf, want) + t.Fatalf("AppendJSONUint failed: actual = %s, want = %s", buf, want) } - buf = jsonAppendUint(buf, 18446744073709551615) + buf = AppendJSONUint(buf, 18446744073709551615) if want += "18446744073709551615"; string(buf) != want { - t.Fatalf("jsonAppendUint failed: actual = %s, want = %s", buf, want) + t.Fatalf("AppendJSONUint failed: actual = %s, want = %s", buf, want) } } -func TestJSONAppendFloat(t *testing.T) { +func TestAppendJSONFloat(t *testing.T) { var buf []byte var want string - buf = jsonAppendFloat(buf, 0.0, 64) + buf = AppendJSONFloat(buf, 0.0, 64) if want += "0"; string(buf) != want { - t.Fatalf("jsonAppendFloat failed: actual = %s, want = %s", buf, want) + t.Fatalf("AppendJSONFloat failed: actual = %s, want = %s", buf, want) } - buf = jsonAppendFloat(buf, 1.25, 64) + buf = AppendJSONFloat(buf, 1.25, 64) if want += "1.25"; string(buf) != want { - t.Fatalf("jsonAppendFloat failed: actual = %s, want = %s", buf, want) + t.Fatalf("AppendJSONFloat failed: actual = %s, want = %s", buf, want) } - buf = jsonAppendFloat(buf, -1.25, 64) + buf = AppendJSONFloat(buf, -1.25, 64) if want += "-1.25"; string(buf) != want { - t.Fatalf("jsonAppendFloat failed: actual = %s, want = %s", buf, want) + t.Fatalf("AppendJSONFloat failed: actual = %s, want = %s", buf, want) } - buf = jsonAppendFloat(buf, math.Pow(2, -16), 64) + buf = AppendJSONFloat(buf, math.Pow(2, -16), 64) if want += "1.52587890625e-05"; string(buf) != want { - t.Fatalf("jsonAppendFloat failed: actual = %s, want = %s", buf, want) + t.Fatalf("AppendJSONFloat failed: actual = %s, want = %s", buf, want) } } -func TestJSONAppendFloat32(t *testing.T) { +func TestAppendJSONFloat32(t *testing.T) { var buf []byte var want string - buf = jsonAppendFloat(buf, 1.234567890123456789, 32) + buf = AppendJSONFloat(buf, 1.234567890123456789, 32) if want += "1.2345679"; string(buf) != want { - t.Fatalf("jsonAppendFloat failed: actual = %s, want = %s", buf, want) + t.Fatalf("AppendJSONFloat failed: actual = %s, want = %s", buf, want) } } -func TestJSONAppendFloat64(t *testing.T) { +func TestAppendJSONFloat64(t *testing.T) { var buf []byte var want string - buf = jsonAppendFloat(buf, 1.234567890123456789, 64) + buf = AppendJSONFloat(buf, 1.234567890123456789, 64) if want += "1.2345678901234567"; string(buf) != want { - t.Fatalf("jsonAppendFloat failed: actual = %s, want = %s", buf, want) + t.Fatalf("AppendJSONFloat failed: actual = %s, want = %s", buf, want) } } -func TestJSONAppendString(t *testing.T) { +func TestAppendJSONString(t *testing.T) { var buf []byte var want string - buf = jsonAppendString(buf, "Hello") + buf = AppendJSONString(buf, "Hello") if want += "\"Hello\""; string(buf) != want { - t.Fatalf("jsonAppendString failed: actual = %s, want = %s", buf, want) + t.Fatalf("AppendJSONString failed: actual = %s, want = %s", buf, want) } - buf = jsonAppendString(buf, "World") + buf = AppendJSONString(buf, "World") if want += "\"World\""; string(buf) != want { - t.Fatalf("jsonAppendString failed: actual = %s, want = %s", buf, want) + t.Fatalf("AppendJSONString failed: actual = %s, want = %s", buf, want) } - buf = jsonAppendString(buf, " \t\n\"") + buf = AppendJSONString(buf, " \t\n\"") if want += "\" \\t\\n\\\"\""; string(buf) != want { - t.Fatalf("jsonAppendString failed: actual = %s, want = %s", buf, want) + t.Fatalf("AppendJSONString failed: actual = %s, want = %s", buf, want) } } -func TestJSONAppendTime(t *testing.T) { +func TestAppendJSONTime(t *testing.T) { var buf []byte var want string - buf = jsonAppendTime(buf, time.Unix(1234567890, 0)) + buf = AppendJSONTime(buf, time.Unix(1234567890, 0)) if want += "1234567890"; string(buf) != want { - t.Fatalf("jsonAppendTime failed: actual = %s, want = %s", buf, want) + t.Fatalf("AppendJSONTime failed: actual = %s, want = %s", buf, want) } - buf = jsonAppendTime(buf, time.Unix(1123456789, 987123654)) + buf = AppendJSONTime(buf, time.Unix(1123456789, 987123654)) if want += "1123456789.987123"; string(buf) != want { - t.Fatalf("jsonAppendTime failed: actual = %s, want = %s", buf, want) + t.Fatalf("AppendJSONTime failed: actual = %s, want = %s", buf, want) } } -func TestJSONAppendGeo(t *testing.T) { +func TestAppendJSONGeo(t *testing.T) { var buf []byte var want string - buf = jsonAppendGeo(buf, Geo{Lat: 123456, Long: 234567}) + buf = AppendJSONGeo(buf, Geo{Lat: 123456, Long: 234567}) if want += "\"123456,234567\""; string(buf) != want { - t.Fatalf("jsonAppendGeo failed: actual = %s, want = %s", buf, want) + t.Fatalf("AppendJSONGeo failed: actual = %s, want = %s", buf, want) } - buf = jsonAppendGeo(buf, Geo{Lat: -123456, Long: -234567}) + buf = AppendJSONGeo(buf, Geo{Lat: -123456, Long: -234567}) if want += "\"-123456,-234567\""; string(buf) != want { - t.Fatalf("jsonAppendTime failed: actual = %s, want = %s", buf, want) + t.Fatalf("AppendJSONTime failed: actual = %s, want = %s", buf, want) } } -func TestJSONAppendScalar(t *testing.T) { +func TestAppendJSONScalar(t *testing.T) { var buf []byte var want string - buf = jsonAppend(buf, true) + buf = AppendJSON(buf, true) if want += "true"; string(buf) != want { - t.Fatalf("jsonAppend failed: actual = %s, want = %s", buf, want) + t.Fatalf("AppendJSON failed: actual = %s, want = %s", buf, want) } - buf = jsonAppend(buf, int8(-128)) + buf = AppendJSON(buf, int8(-128)) if want += "-128"; string(buf) != want { - t.Fatalf("jsonAppend failed: actual = %s, want = %s", buf, want) + t.Fatalf("AppendJSON failed: actual = %s, want = %s", buf, want) } - buf = jsonAppend(buf, int16(-32768)) + buf = AppendJSON(buf, int16(-32768)) if want += "-32768"; string(buf) != want { - t.Fatalf("jsonAppend failed: actual = %s, want = %s", buf, want) + t.Fatalf("AppendJSON failed: actual = %s, want = %s", buf, want) } - buf = jsonAppend(buf, int32(-2147483648)) + buf = AppendJSON(buf, int32(-2147483648)) if want += "-2147483648"; string(buf) != want { - t.Fatalf("jsonAppend failed: actual = %s, want = %s", buf, want) + t.Fatalf("AppendJSON failed: actual = %s, want = %s", buf, want) } - buf = jsonAppend(buf, int64(-9223372036854775808)) + buf = AppendJSON(buf, int64(-9223372036854775808)) if want += "-9223372036854775808"; string(buf) != want { - t.Fatalf("jsonAppend failed: actual = %s, want = %s", buf, want) + t.Fatalf("AppendJSON failed: actual = %s, want = %s", buf, want) } - buf = jsonAppend(buf, int(-9223372036854775808)) + buf = AppendJSON(buf, int(-9223372036854775808)) if want += "-9223372036854775808"; string(buf) != want { - t.Fatalf("jsonAppend failed: actual = %s, want = %s", buf, want) + t.Fatalf("AppendJSON failed: actual = %s, want = %s", buf, want) } - buf = jsonAppend(buf, uint8(255)) + buf = AppendJSON(buf, uint8(255)) if want += "255"; string(buf) != want { - t.Fatalf("jsonAppend failed: actual = %s, want = %s", buf, want) + t.Fatalf("AppendJSON failed: actual = %s, want = %s", buf, want) } - buf = jsonAppend(buf, uint16(65535)) + buf = AppendJSON(buf, uint16(65535)) if want += "65535"; string(buf) != want { - t.Fatalf("jsonAppend failed: actual = %s, want = %s", buf, want) + t.Fatalf("AppendJSON failed: actual = %s, want = %s", buf, want) } - buf = jsonAppend(buf, uint32(4294967295)) + buf = AppendJSON(buf, uint32(4294967295)) if want += "4294967295"; string(buf) != want { - t.Fatalf("jsonAppend failed: actual = %s, want = %s", buf, want) + t.Fatalf("AppendJSON failed: actual = %s, want = %s", buf, want) } - buf = jsonAppend(buf, uint64(18446744073709551615)) + buf = AppendJSON(buf, uint64(18446744073709551615)) if want += "18446744073709551615"; string(buf) != want { - t.Fatalf("jsonAppend failed: actual = %s, want = %s", buf, want) + t.Fatalf("AppendJSON failed: actual = %s, want = %s", buf, want) } - buf = jsonAppend(buf, uint(18446744073709551615)) + buf = AppendJSON(buf, uint(18446744073709551615)) if want += "18446744073709551615"; string(buf) != want { - t.Fatalf("jsonAppend failed: actual = %s, want = %s", buf, want) + t.Fatalf("AppendJSON failed: actual = %s, want = %s", buf, want) } - buf = jsonAppend(buf, float32(1.234567890123456789)) + buf = AppendJSON(buf, float32(1.234567890123456789)) if want += "1.2345679"; string(buf) != want { - t.Fatalf("jsonAppend failed: actual = %s, want = %s", buf, want) + t.Fatalf("AppendJSON failed: actual = %s, want = %s", buf, want) } - buf = jsonAppend(buf, float64(1.234567890123456789)) + buf = AppendJSON(buf, float64(1.234567890123456789)) if want += "1.2345678901234567"; string(buf) != want { - t.Fatalf("jsonAppend failed: actual = %s, want = %s", buf, want) + t.Fatalf("AppendJSON failed: actual = %s, want = %s", buf, want) } - buf = jsonAppend(buf, "String") + buf = AppendJSON(buf, "String") if want += "\"String\""; string(buf) != want { - t.Fatalf("jsonAppend failed: actual = %s, want = %s", buf, want) + t.Fatalf("AppendJSON failed: actual = %s, want = %s", buf, want) } - buf = jsonAppend(buf, time.Unix(1234567890, 123456789)) + buf = AppendJSON(buf, time.Unix(1234567890, 123456789)) if want += "1234567890.123456"; string(buf) != want { - t.Fatalf("jsonAppend failed: actual = %s, want = %s", buf, want) + t.Fatalf("AppendJSON failed: actual = %s, want = %s", buf, want) } - buf = jsonAppend(buf, Geo{Lat: 123456, Long: 234567}) + buf = AppendJSON(buf, Geo{Lat: 123456, Long: 234567}) if want += "\"123456,234567\""; string(buf) != want { - t.Fatalf("jsonAppend failed: actual = %s, want = %s", buf, want) + t.Fatalf("AppendJSON failed: actual = %s, want = %s", buf, want) } } -func TestJSONAppendPtr(t *testing.T) { +func TestAppendJSONPtr(t *testing.T) { var buf []byte var want string v := 123456 - buf = jsonAppend(buf, &v) + buf = AppendJSON(buf, &v) if want += "123456"; string(buf) != want { - t.Fatalf("jsonAppend failed: actual = %s, want = %s", buf, want) + t.Fatalf("AppendJSON failed: actual = %s, want = %s", buf, want) } } -func TestJSONAppendArray(t *testing.T) { +func TestAppendJSONArray(t *testing.T) { var buf []byte var want string v := [3]int{123, 456, 789} - buf = jsonAppend(buf, v) + buf = AppendJSON(buf, v) if want += "[123,456,789]"; string(buf) != want { - t.Fatalf("jsonAppend failed: actual = %s, want = %s", buf, want) + t.Fatalf("AppendJSON failed: actual = %s, want = %s", buf, want) } } -func TestJSONAppendSlice(t *testing.T) { +func TestAppendJSONSlice(t *testing.T) { var buf []byte var want string v := []int{987, 654, 321} - buf = jsonAppend(buf, v) + buf = AppendJSON(buf, v) if want += "[987,654,321]"; string(buf) != want { - t.Fatalf("jsonAppend failed: actual = %s, want = %s", buf, want) + t.Fatalf("AppendJSON failed: actual = %s, want = %s", buf, want) } } -func TestJSONEncodeBool(t *testing.T) { - if want, actual := "true", jsonEncodeBool(true); actual != want { - t.Fatalf("jsonEncodeBool failed: actual = %s, want = %s", actual, want) +func TestEncodeJSONBool(t *testing.T) { + if want, actual := "true", EncodeJSONBool(true); actual != want { + t.Fatalf("EncodeJSONBool failed: actual = %s, want = %s", actual, want) } - if want, actual := "false", jsonEncodeBool(false); actual != want { - t.Fatalf("jsonEncodeBool failed: actual = %s, want = %s", actual, want) + if want, actual := "false", EncodeJSONBool(false); actual != want { + t.Fatalf("EncodeJSONBool failed: actual = %s, want = %s", actual, want) } } -func TestJSONEncodeInt(t *testing.T) { - if want, actual := "0", jsonEncodeInt(0); actual != want { - t.Fatalf("jsonEncodeInt failed: actual = %s, want = %s", actual, want) +func TestEncodeJSONInt(t *testing.T) { + if want, actual := "0", EncodeJSONInt(0); actual != want { + t.Fatalf("EncodeJSONInt failed: actual = %s, want = %s", actual, want) } - if want, actual := "9223372036854775807", jsonEncodeInt(9223372036854775807); actual != want { - t.Fatalf("jsonEncodeInt failed: actual = %s, want = %s", actual, want) + if want, actual := "9223372036854775807", EncodeJSONInt(9223372036854775807); actual != want { + t.Fatalf("EncodeJSONInt failed: actual = %s, want = %s", actual, want) } - if want, actual := "-9223372036854775808", jsonEncodeInt(-9223372036854775808); actual != want { - t.Fatalf("jsonEncodeInt failed: actual = %s, want = %s", actual, want) + if want, actual := "-9223372036854775808", EncodeJSONInt(-9223372036854775808); actual != want { + t.Fatalf("EncodeJSONInt failed: actual = %s, want = %s", actual, want) } } -func TestJSONEncodeUint(t *testing.T) { - if want, actual := "0", jsonEncodeUint(0); actual != want { - t.Fatalf("jsonEncodeUint failed: actual = %s, want = %s", actual, want) +func TestEncodeJSONUint(t *testing.T) { + if want, actual := "0", EncodeJSONUint(0); actual != want { + t.Fatalf("EncodeJSONUint failed: actual = %s, want = %s", actual, want) } - if want, actual := "18446744073709551615", jsonEncodeUint(18446744073709551615); actual != want { - t.Fatalf("jsonEncodeUint failed: actual = %s, want = %s", actual, want) + if want, actual := "18446744073709551615", EncodeJSONUint(18446744073709551615); actual != want { + t.Fatalf("EncodeJSONUint failed: actual = %s, want = %s", actual, want) } } -func TestJSONEncodeFloat(t *testing.T) { - if want, actual := "0", jsonEncodeFloat(0.0, 64); actual != want { - t.Fatalf("jsonEncodeFloat failed: actual = %s, want = %s", actual, want) +func TestEncodeJSONFloat(t *testing.T) { + if want, actual := "0", EncodeJSONFloat(0.0, 64); actual != want { + t.Fatalf("EncodeJSONFloat failed: actual = %s, want = %s", actual, want) } - if want, actual := "1.25", jsonEncodeFloat(1.25, 64); actual != want { - t.Fatalf("jsonEncodeFloat failed: actual = %s, want = %s", actual, want) + if want, actual := "1.25", EncodeJSONFloat(1.25, 64); actual != want { + t.Fatalf("EncodeJSONFloat failed: actual = %s, want = %s", actual, want) } - if want, actual := "-1.25", jsonEncodeFloat(-1.25, 64); actual != want { - t.Fatalf("jsonEncodeFloat failed: actual = %s, want = %s", actual, want) + if want, actual := "-1.25", EncodeJSONFloat(-1.25, 64); actual != want { + t.Fatalf("EncodeJSONFloat failed: actual = %s, want = %s", actual, want) } - if want, actual := "1.52587890625e-05", jsonEncodeFloat(math.Pow(2, -16), 64); actual != want { - t.Fatalf("jsonEncodeFloat failed: actual = %s, want = %s", actual, want) + if want, actual := "1.52587890625e-05", EncodeJSONFloat(math.Pow(2, -16), 64); actual != want { + t.Fatalf("EncodeJSONFloat failed: actual = %s, want = %s", actual, want) } } -func TestJSONEncodeFloat32(t *testing.T) { - if want, actual := "1.2345679", jsonEncodeFloat(1.234567890123456789, 32); actual != want { - t.Fatalf("jsonEncodeFloat failed: actual = %s, want = %s", actual, want) +func TestEncodeJSONFloat32(t *testing.T) { + if want, actual := "1.2345679", EncodeJSONFloat(1.234567890123456789, 32); actual != want { + t.Fatalf("EncodeJSONFloat failed: actual = %s, want = %s", actual, want) } } -func TestJSONEncodeFloat64(t *testing.T) { - if want, actual := "1.2345678901234567", jsonEncodeFloat(1.234567890123456789, 64); actual != want { - t.Fatalf("jsonEncodeFloat failed: actual = %s, want = %s", actual, want) +func TestEncodeJSONFloat64(t *testing.T) { + if want, actual := "1.2345678901234567", EncodeJSONFloat(1.234567890123456789, 64); actual != want { + t.Fatalf("EncodeJSONFloat failed: actual = %s, want = %s", actual, want) } } -func TestJSONEncodeString(t *testing.T) { - if want, actual := "\"Hello\"", jsonEncodeString("Hello"); actual != want { - t.Fatalf("jsonEncodeString failed: actual = %s, want = %s", actual, want) +func TestEncodeJSONString(t *testing.T) { + if want, actual := "\"Hello\"", EncodeJSONString("Hello"); actual != want { + t.Fatalf("EncodeJSONString failed: actual = %s, want = %s", actual, want) } - if want, actual := "\"World\"", jsonEncodeString("World"); actual != want { - t.Fatalf("jsonEncodeString failed: actual = %s, want = %s", actual, want) + if want, actual := "\"World\"", EncodeJSONString("World"); actual != want { + t.Fatalf("EncodeJSONString failed: actual = %s, want = %s", actual, want) } - if want, actual := "\" \\t\\n\\\"\"", jsonEncodeString(" \t\n\""); actual != want { - t.Fatalf("jsonEncodeString failed: actual = %s, want = %s", actual, want) + if want, actual := "\" \\t\\n\\\"\"", EncodeJSONString(" \t\n\""); actual != want { + t.Fatalf("EncodeJSONString failed: actual = %s, want = %s", actual, want) } } -func TestJSONEncodeTime(t *testing.T) { - if want, actual := "1234567890", jsonEncodeTime(time.Unix(1234567890, 0)); actual != want { - t.Fatalf("jsonEncodeTime failed: actual = %s, want = %s", actual, want) +func TestEncodeJSONTime(t *testing.T) { + if want, actual := "1234567890", EncodeJSONTime(time.Unix(1234567890, 0)); actual != want { + t.Fatalf("EncodeJSONTime failed: actual = %s, want = %s", actual, want) } - if want, actual := "1123456789.987123", jsonEncodeTime(time.Unix(1123456789, 987123654)); actual != want { - t.Fatalf("jsonEncodeTime failed: actual = %s, want = %s", actual, want) + if want, actual := "1123456789.987123", EncodeJSONTime(time.Unix(1123456789, 987123654)); actual != want { + t.Fatalf("EncodeJSONTime failed: actual = %s, want = %s", actual, want) } } -func TestJSONEncodeGeo(t *testing.T) { - if want, actual := "\"123456,234567\"", jsonEncodeGeo(Geo{Lat: 123456, Long: 234567}); actual != want { - t.Fatalf("jsonEncodeGeo failed: actual = %s, want = %s", actual, want) +func TestEncodeJSONGeo(t *testing.T) { + if want, actual := "\"123456,234567\"", EncodeJSONGeo(Geo{Lat: 123456, Long: 234567}); actual != want { + t.Fatalf("EncodeJSONGeo failed: actual = %s, want = %s", actual, want) } - if want, actual := "\"-123456,-234567\"", jsonEncodeGeo(Geo{Lat: -123456, Long: -234567}); actual != want { - t.Fatalf("jsonEncodeGeo failed: actual = %s, want = %s", actual, want) + if want, actual := "\"-123456,-234567\"", EncodeJSONGeo(Geo{Lat: -123456, Long: -234567}); actual != want { + t.Fatalf("EncodeJSONGeo failed: actual = %s, want = %s", actual, want) } } -func TestJSONEncodeScalar(t *testing.T) { - if want, actual := "true", jsonEncode(true); actual != want { - t.Fatalf("jsonEncode failed: actual = %s, want = %s", actual, want) +func TestEncodeJSONScalar(t *testing.T) { + if want, actual := "true", EncodeJSON(true); actual != want { + t.Fatalf("EncodeJSON failed: actual = %s, want = %s", actual, want) } - if want, actual := "-128", jsonEncode(int8(-128)); actual != want { - t.Fatalf("jsonEncode failed: actual = %s, want = %s", actual, want) + if want, actual := "-128", EncodeJSON(int8(-128)); actual != want { + t.Fatalf("EncodeJSON failed: actual = %s, want = %s", actual, want) } - if want, actual := "-32768", jsonEncode(int16(-32768)); actual != want { - t.Fatalf("jsonEncode failed: actual = %s, want = %s", actual, want) + if want, actual := "-32768", EncodeJSON(int16(-32768)); actual != want { + t.Fatalf("EncodeJSON failed: actual = %s, want = %s", actual, want) } - if want, actual := "-2147483648", jsonEncode(int32(-2147483648)); actual != want { - t.Fatalf("jsonEncode failed: actual = %s, want = %s", actual, want) + if want, actual := "-2147483648", EncodeJSON(int32(-2147483648)); actual != want { + t.Fatalf("EncodeJSON failed: actual = %s, want = %s", actual, want) } - if want, actual := "-9223372036854775808", jsonEncode(int64(-9223372036854775808)); actual != want { - t.Fatalf("jsonEncode failed: actual = %s, want = %s", actual, want) + if want, actual := "-9223372036854775808", EncodeJSON(int64(-9223372036854775808)); actual != want { + t.Fatalf("EncodeJSON failed: actual = %s, want = %s", actual, want) } - if want, actual := "-9223372036854775808", jsonEncode(int(-9223372036854775808)); actual != want { - t.Fatalf("jsonEncode failed: actual = %s, want = %s", actual, want) + if want, actual := "-9223372036854775808", EncodeJSON(int(-9223372036854775808)); actual != want { + t.Fatalf("EncodeJSON failed: actual = %s, want = %s", actual, want) } - if want, actual := "255", jsonEncode(uint8(255)); actual != want { - t.Fatalf("jsonEncode failed: actual = %s, want = %s", actual, want) + if want, actual := "255", EncodeJSON(uint8(255)); actual != want { + t.Fatalf("EncodeJSON failed: actual = %s, want = %s", actual, want) } - if want, actual := "65535", jsonEncode(uint16(65535)); actual != want { - t.Fatalf("jsonEncode failed: actual = %s, want = %s", actual, want) + if want, actual := "65535", EncodeJSON(uint16(65535)); actual != want { + t.Fatalf("EncodeJSON failed: actual = %s, want = %s", actual, want) } - if want, actual := "4294967295", jsonEncode(uint32(4294967295)); actual != want { - t.Fatalf("jsonEncode failed: actual = %s, want = %s", actual, want) + if want, actual := "4294967295", EncodeJSON(uint32(4294967295)); actual != want { + t.Fatalf("EncodeJSON failed: actual = %s, want = %s", actual, want) } - if want, actual := "18446744073709551615", jsonEncode(uint64(18446744073709551615)); actual != want { - t.Fatalf("jsonEncode failed: actual = %s, want = %s", actual, want) + if want, actual := "18446744073709551615", EncodeJSON(uint64(18446744073709551615)); actual != want { + t.Fatalf("EncodeJSON failed: actual = %s, want = %s", actual, want) } - if want, actual := "18446744073709551615", jsonEncode(uint(18446744073709551615)); actual != want { - t.Fatalf("jsonEncode failed: actual = %s, want = %s", actual, want) + if want, actual := "18446744073709551615", EncodeJSON(uint(18446744073709551615)); actual != want { + t.Fatalf("EncodeJSON failed: actual = %s, want = %s", actual, want) } - if want, actual := "1.2345679", jsonEncode(float32(1.234567890123456789)); actual != want { - t.Fatalf("jsonEncode failed: actual = %s, want = %s", actual, want) + if want, actual := "1.2345679", EncodeJSON(float32(1.234567890123456789)); actual != want { + t.Fatalf("EncodeJSON failed: actual = %s, want = %s", actual, want) } - if want, actual := "1.2345678901234567", jsonEncode(1.234567890123456789); actual != want { - t.Fatalf("jsonEncode failed: actual = %s, want = %s", actual, want) + if want, actual := "1.2345678901234567", EncodeJSON(1.234567890123456789); actual != want { + t.Fatalf("EncodeJSON failed: actual = %s, want = %s", actual, want) } - if want, actual := "\"String\"", jsonEncode("String"); actual != want { - t.Fatalf("jsonEncode failed: actual = %s, want = %s", actual, want) + if want, actual := "\"String\"", EncodeJSON("String"); actual != want { + t.Fatalf("EncodeJSON failed: actual = %s, want = %s", actual, want) } - if want, actual := "1234567890.123456", jsonEncodeTime(time.Unix(1234567890, 123456789)); actual != want { - t.Fatalf("jsonEncode failed: actual = %s, want = %s", actual, want) + if want, actual := "1234567890.123456", EncodeJSONTime(time.Unix(1234567890, 123456789)); actual != want { + t.Fatalf("EncodeJSON failed: actual = %s, want = %s", actual, want) } - if want, actual := "\"123456,234567\"", jsonEncode(Geo{Lat: 123456, Long: 234567}); actual != want { - t.Fatalf("jsonEncode failed: actual = %s, want = %s", actual, want) + if want, actual := "\"123456,234567\"", EncodeJSON(Geo{Lat: 123456, Long: 234567}); actual != want { + t.Fatalf("EncodeJSON failed: actual = %s, want = %s", actual, want) } } -func TestJSONEncodePtr(t *testing.T) { +func TestEncodeJSONPtr(t *testing.T) { v := 123456 - if want, actual := "123456", jsonEncode(&v); actual != want { - t.Fatalf("jsonEncode failed: actual = %s, want = %s", actual, want) + if want, actual := "123456", EncodeJSON(&v); actual != want { + t.Fatalf("EncodeJSON failed: actual = %s, want = %s", actual, want) } } -func TestJSONEncodeArray(t *testing.T) { +func TestEncodeJSONArray(t *testing.T) { v := [3]int{123, 456, 789} - if want, actual := "[123,456,789]", jsonEncode(v); actual != want { - t.Fatalf("jsonEncode failed: actual = %s, want = %s", actual, want) + if want, actual := "[123,456,789]", EncodeJSON(v); actual != want { + t.Fatalf("EncodeJSON failed: actual = %s, want = %s", actual, want) } } -func TestJSONEncodeSlice(t *testing.T) { +func TestEncodeJSONSlice(t *testing.T) { v := []int{987, 654, 321} - if want, actual := "[987,654,321]", jsonEncode(v); actual != want { - t.Fatalf("jsonEncode failed: actual = %s, want = %s", actual, want) + if want, actual := "[987,654,321]", EncodeJSON(v); actual != want { + t.Fatalf("EncodeJSON failed: actual = %s, want = %s", actual, want) } } -------------- next part -------------- HTML����������������������������...下载