Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

update to current go + gofmt

  • Loading branch information...
commit 3bde2d97c41e90098c202998ba5a0e76ac81c692 1 parent 8cf3245
@piranha piranha authored
Showing with 34 additions and 34 deletions.
  1. +24 −23 decode.go
  2. +5 −5 encode.go
  3. +5 −6 tnetstring_test.go
View
47 decode.go
@@ -1,17 +1,17 @@
package tnetstring
import (
- "os"
+ "errors"
"reflect"
"strconv"
"strings"
)
-func Unmarshal(data string, v interface{}) os.Error {
+func Unmarshal(data string, v interface{}) error {
val := reflect.ValueOf(v)
val = reflect.Indirect(val)
if !val.CanSet() {
- return os.NewError("tnetstring: Unmarshal requires a settable value")
+ return errors.New("tnetstring: Unmarshal requires a settable value")
}
_, err := unmarshal(data, val)
return err
@@ -60,37 +60,38 @@ var typeLookup = [...]byte{
reflect.UnsafePointer: 0,
}
-func unmarshal(data string, v reflect.Value) (int, os.Error) {
+func unmarshal(data string, v reflect.Value) (int, error) {
typ, content, n := readElement(data)
if n == 0 {
- return 0, os.NewError("tnetstring: invalid data")
+ return 0, errors.New("tnetstring: invalid data")
}
v = indirect(v, true)
kind := v.Kind()
// ~ and interface types are special cases
if typ != '~' && kind != reflect.Interface && typeLookup[kind] != typ {
- return 0, os.NewError("tnetstring: invalid value to unmarshal into")
+ return 0, errors.New("tnetstring: invalid value to unmarshal into")
}
switch typ {
case '!':
v.Set(reflect.ValueOf(content == "true"))
case '#':
switch kind {
- case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
- i, err := strconv.Atoi64(content)
+ case reflect.Int, reflect.Int8, reflect.Int16,
+ reflect.Int32, reflect.Int64:
+ i, err := strconv.ParseInt(content, 10, 64)
if err != nil {
return 0, err
}
v.SetInt(i)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32,
reflect.Uint64, reflect.Uintptr:
- ui, err := strconv.Atoui64(content)
+ ui, err := strconv.ParseUint(content, 10, 64)
if err != nil {
return 0, err
}
v.SetUint(ui)
case reflect.Interface:
- i, err := strconv.Atoi64(content)
+ i, err := strconv.ParseInt(content, 10, 64)
if err != nil {
return 0, err
}
@@ -101,7 +102,7 @@ func unmarshal(data string, v reflect.Value) (int, os.Error) {
case ']':
unmarshalArray(content, v, kind)
case '}':
- var err os.Error
+ var err error
if kind == reflect.Map {
err = unmarshalMap(content, v)
} else {
@@ -115,15 +116,15 @@ func unmarshal(data string, v reflect.Value) (int, os.Error) {
case reflect.Interface, reflect.Map, reflect.Ptr, reflect.Slice:
v.Set(reflect.Zero(v.Type()))
default:
- return 0, os.NewError("tnetstring: invalid value to unmarshal into")
+ return 0, errors.New("tnetstring: invalid value to unmarshal into")
}
default:
- return 0, os.NewError("tnetstring: unknown type")
+ return 0, errors.New("tnetstring: unknown type")
}
return n, nil
}
-func unmarshalArray(data string, v reflect.Value, kind reflect.Kind) os.Error {
+func unmarshalArray(data string, v reflect.Value, kind reflect.Kind) error {
i := 0
elType := v.Type().Elem()
elVal := reflect.Zero(elType)
@@ -146,10 +147,10 @@ func unmarshalArray(data string, v reflect.Value, kind reflect.Kind) os.Error {
return nil
}
-func unmarshalMap(data string, v reflect.Value) os.Error {
+func unmarshalMap(data string, v reflect.Value) error {
mapType := v.Type()
if mapType.Key().Kind() != reflect.String {
- return os.NewError("tnetstring: only maps with string keys can be unmarshaled")
+ return errors.New("tnetstring: only maps with string keys can be unmarshaled")
}
if v.IsNil() {
v.Set(reflect.MakeMap(mapType))
@@ -162,7 +163,7 @@ func unmarshalMap(data string, v reflect.Value) os.Error {
typ, content, n := readElement(data)
data = data[n:]
if typ != ',' {
- return os.NewError("tnetstring: non-string key in dictionary")
+ return errors.New("tnetstring: non-string key in dictionary")
}
s = content
n, err := unmarshal(data, val)
@@ -175,18 +176,18 @@ func unmarshalMap(data string, v reflect.Value) os.Error {
return nil
}
-func unmarshalStruct(data string, v reflect.Value) os.Error {
+func unmarshalStruct(data string, v reflect.Value) error {
structType := v.Type()
var name string
for len(data) > 0 {
typ, content, n := readElement(data)
data = data[n:]
if typ != ',' {
- return os.NewError("tnetstring: non-string key in dictionary")
+ return errors.New("tnetstring: non-string key in dictionary")
}
name = content
field := v.FieldByName(name)
- if field.Internal == nil {
+ if !field.IsValid() {
for i := 0; i < structType.NumField(); i++ {
f := structType.Field(i)
if f.Tag.Get("tnetstring") == name {
@@ -194,7 +195,7 @@ func unmarshalStruct(data string, v reflect.Value) os.Error {
break
}
}
- if field.Internal == nil {
+ if !field.IsValid() {
// skip the field
_, _, n := readElement(data)
data = data[n:]
@@ -218,11 +219,11 @@ func readElement(data string) (typ byte, content string, n int) {
n, err := strconv.Atoi(data[:col])
// use the position after the colon from here on out
col++
- if err != nil || col + n > len(data) {
+ if err != nil || col+n > len(data) {
return
}
n += col
- content = data[col : n]
+ content = data[col:n]
typ = data[n]
n++
return
View
10 encode.go
@@ -1,15 +1,15 @@
package tnetstring
import (
- "os"
+ "errors"
"reflect"
"strconv"
)
-func Marshal(v interface{}) (s string, err os.Error) {
+func Marshal(v interface{}) (s string, err error) {
defer func() {
if s, ok := recover().(string); ok {
- err = os.NewError(s)
+ err = errors.New(s)
}
}()
val := reflect.ValueOf(v)
@@ -62,11 +62,11 @@ func encodeBool(b *outbuf, v reflect.Value) {
}
func encodeInt(b *outbuf, v reflect.Value) {
- b.writeTString('#', strconv.Itoa64(v.Int()))
+ b.writeTString('#', strconv.FormatInt(v.Int(), 10))
}
func encodeUint(b *outbuf, v reflect.Value) {
- b.writeTString('#', strconv.Uitoa64(v.Uint()))
+ b.writeTString('#', strconv.FormatUint(v.Uint(), 10))
}
func encodeString(b *outbuf, v reflect.Value) {
View
11 tnetstring_test.go
@@ -1,8 +1,7 @@
package tnetstring
import (
- "json"
- "os"
+ "encoding/json"
"reflect"
"testing"
)
@@ -40,11 +39,11 @@ var tnetstringTests = []tnetstringTest{
{struct {
A int
B string
- }{1, "hello"},"20:1:A,1:1#1:B,5:hello,}"},
+ }{1, "hello"}, "20:1:A,1:1#1:B,5:hello,}"},
{&struct {
A int
B string
- }{1, "hello"},"20:1:A,1:1#1:B,5:hello,}"},
+ }{1, "hello"}, "20:1:A,1:1#1:B,5:hello,}"},
}
var tests = append(stableTests, tnetstringTests...)
@@ -86,9 +85,9 @@ func init() {
jsonData = make([][]byte, len(stableTests))
benchmarkData = make([]string, len(stableTests))
for i, test := range stableTests {
- var err os.Error
+ var err error
if jsonData[i], err = json.Marshal(test.val); err != nil {
- panic(err.String())
+ panic(err.Error())
}
benchmarkData[i] = test.data
}
Please sign in to comment.
Something went wrong with that request. Please try again.