diff options
Diffstat (limited to 'libgo/go/encoding')
-rw-r--r-- | libgo/go/encoding/asn1/asn1.go | 10 | ||||
-rw-r--r-- | libgo/go/encoding/asn1/asn1_test.go | 2 | ||||
-rw-r--r-- | libgo/go/encoding/asn1/common.go | 3 | ||||
-rw-r--r-- | libgo/go/encoding/asn1/marshal.go | 4 | ||||
-rw-r--r-- | libgo/go/encoding/asn1/marshal_test.go | 6 | ||||
-rw-r--r-- | libgo/go/encoding/binary/binary.go | 11 | ||||
-rw-r--r-- | libgo/go/encoding/csv/reader.go | 3 | ||||
-rw-r--r-- | libgo/go/encoding/gob/decode.go | 3 | ||||
-rw-r--r-- | libgo/go/encoding/gob/doc.go | 2 | ||||
-rw-r--r-- | libgo/go/encoding/gob/encoder_test.go | 4 | ||||
-rw-r--r-- | libgo/go/encoding/gob/gobencdec_test.go | 19 | ||||
-rw-r--r-- | libgo/go/encoding/json/encode.go | 5 |
12 files changed, 59 insertions, 13 deletions
diff --git a/libgo/go/encoding/asn1/asn1.go b/libgo/go/encoding/asn1/asn1.go index 4d1ae38c4ed..3bf81a68cf4 100644 --- a/libgo/go/encoding/asn1/asn1.go +++ b/libgo/go/encoding/asn1/asn1.go @@ -250,10 +250,14 @@ func parseBase128Int(bytes []byte, initOffset int) (ret, offset int, err error) func parseUTCTime(bytes []byte) (ret time.Time, err error) { s := string(bytes) ret, err = time.Parse("0601021504Z0700", s) - if err == nil { - return + if err != nil { + ret, err = time.Parse("060102150405Z0700", s) } - ret, err = time.Parse("060102150405Z0700", s) + if err == nil && ret.Year() >= 2050 { + // UTCTime only encodes times prior to 2050. See https://tools.ietf.org/html/rfc5280#section-4.1.2.5.1 + ret = ret.AddDate(-100, 0, 0) + } + return } diff --git a/libgo/go/encoding/asn1/asn1_test.go b/libgo/go/encoding/asn1/asn1_test.go index 92c9eb62d2c..93803f43532 100644 --- a/libgo/go/encoding/asn1/asn1_test.go +++ b/libgo/go/encoding/asn1/asn1_test.go @@ -321,7 +321,7 @@ var parseFieldParametersTestData []parseFieldParametersTest = []parseFieldParame {"default:42", fieldParameters{defaultValue: newInt64(42)}}, {"tag:17", fieldParameters{tag: newInt(17)}}, {"optional,explicit,default:42,tag:17", fieldParameters{optional: true, explicit: true, defaultValue: newInt64(42), tag: newInt(17)}}, - {"optional,explicit,default:42,tag:17,rubbish1", fieldParameters{true, true, false, newInt64(42), newInt(17), 0, false}}, + {"optional,explicit,default:42,tag:17,rubbish1", fieldParameters{true, true, false, newInt64(42), newInt(17), 0, false, false}}, {"set", fieldParameters{set: true}}, } diff --git a/libgo/go/encoding/asn1/common.go b/libgo/go/encoding/asn1/common.go index f7cb3acbb86..03856bc55c5 100644 --- a/libgo/go/encoding/asn1/common.go +++ b/libgo/go/encoding/asn1/common.go @@ -75,6 +75,7 @@ type fieldParameters struct { tag *int // the EXPLICIT or IMPLICIT tag (maybe nil). stringType int // the string tag to use when marshaling. set bool // true iff this should be encoded as a SET + omitEmpty bool // true iff this should be omitted if empty when marshaling. // Invariants: // if explicit is set, tag is non-nil. @@ -116,6 +117,8 @@ func parseFieldParameters(str string) (ret fieldParameters) { if ret.tag == nil { ret.tag = new(int) } + case part == "omitempty": + ret.omitEmpty = true } } return diff --git a/libgo/go/encoding/asn1/marshal.go b/libgo/go/encoding/asn1/marshal.go index 774bee74baa..163bca575de 100644 --- a/libgo/go/encoding/asn1/marshal.go +++ b/libgo/go/encoding/asn1/marshal.go @@ -463,6 +463,10 @@ func marshalField(out *forkableWriter, v reflect.Value, params fieldParameters) return marshalField(out, v.Elem(), params) } + if v.Kind() == reflect.Slice && v.Len() == 0 && params.omitEmpty { + return + } + if params.optional && reflect.DeepEqual(v.Interface(), reflect.Zero(v.Type()).Interface()) { return } diff --git a/libgo/go/encoding/asn1/marshal_test.go b/libgo/go/encoding/asn1/marshal_test.go index a7447f97812..f43bcae681a 100644 --- a/libgo/go/encoding/asn1/marshal_test.go +++ b/libgo/go/encoding/asn1/marshal_test.go @@ -54,6 +54,10 @@ type optionalRawValueTest struct { A RawValue `asn1:"optional"` } +type omitEmptyTest struct { + A []string `asn1:"omitempty"` +} + type testSET []int var PST = time.FixedZone("PST", -8*60*60) @@ -116,6 +120,8 @@ var marshalTests = []marshalTest{ {rawContentsStruct{[]byte{0x30, 3, 1, 2, 3}, 64}, "3003010203"}, {RawValue{Tag: 1, Class: 2, IsCompound: false, Bytes: []byte{1, 2, 3}}, "8103010203"}, {testSET([]int{10}), "310302010a"}, + {omitEmptyTest{[]string{}}, "3000"}, + {omitEmptyTest{[]string{"1"}}, "30053003130131"}, } func TestMarshal(t *testing.T) { diff --git a/libgo/go/encoding/binary/binary.go b/libgo/go/encoding/binary/binary.go index 02f090d53f3..712e490e656 100644 --- a/libgo/go/encoding/binary/binary.go +++ b/libgo/go/encoding/binary/binary.go @@ -2,12 +2,17 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -// Package binary implements translation between -// unsigned integer values and byte sequences -// and the reading and writing of fixed-size values. +// Package binary implements translation between numbers and byte sequences +// and encoding and decoding of varints. +// +// Numbers are translated by reading and writing fixed-size values. // A fixed-size value is either a fixed-size arithmetic // type (int8, uint8, int16, float32, complex64, ...) // or an array or struct containing only fixed-size values. +// +// Varints are a method of encoding integers using one or more bytes; +// numbers with smaller absolute value take a smaller number of bytes. +// For a specification, see http://code.google.com/apis/protocolbuffers/docs/encoding.html. package binary import ( diff --git a/libgo/go/encoding/csv/reader.go b/libgo/go/encoding/csv/reader.go index 9aa398e58b2..db4d988526f 100644 --- a/libgo/go/encoding/csv/reader.go +++ b/libgo/go/encoding/csv/reader.go @@ -92,7 +92,8 @@ var ( // If FieldsPerRecord is positive, Read requires each record to // have the given number of fields. If FieldsPerRecord is 0, Read sets it to // the number of fields in the first record, so that future records must -// have the same field count. +// have the same field count. If FieldsPerRecord is negative, no check is +// made and records may have a variable number of fields. // // If LazyQuotes is true, a quote may appear in an unquoted field and a // non-doubled quote may appear in a quoted field. diff --git a/libgo/go/encoding/gob/decode.go b/libgo/go/encoding/gob/decode.go index 0708a83c99a..e32a178aba3 100644 --- a/libgo/go/encoding/gob/decode.go +++ b/libgo/go/encoding/gob/decode.go @@ -707,6 +707,9 @@ func (dec *Decoder) decodeInterface(ityp reflect.Type, state *decoderState, p ui if name == "" { // Copy the representation of the nil interface value to the target. // This is horribly unsafe and special. + if indir > 0 { + p = allocate(ityp, p, 1) // All but the last level has been allocated by dec.Indirect + } *(*[2]uintptr)(unsafe.Pointer(p)) = ivalue.InterfaceData() return } diff --git a/libgo/go/encoding/gob/doc.go b/libgo/go/encoding/gob/doc.go index c9ad18e7641..96885f8ded4 100644 --- a/libgo/go/encoding/gob/doc.go +++ b/libgo/go/encoding/gob/doc.go @@ -226,7 +226,7 @@ where * signifies zero or more repetitions and the type id of a value must be predefined or be defined before the value in the stream. See "Gobs of data" for a design discussion of the gob wire format: -http://blog.golang.org/2011/03/gobs-of-data.html +http://golang.org/doc/articles/gobs_of_data.html */ package gob diff --git a/libgo/go/encoding/gob/encoder_test.go b/libgo/go/encoding/gob/encoder_test.go index 050786dfd1f..c4947cbb8d3 100644 --- a/libgo/go/encoding/gob/encoder_test.go +++ b/libgo/go/encoding/gob/encoder_test.go @@ -694,8 +694,8 @@ type Bug3 struct { func TestGobPtrSlices(t *testing.T) { in := []*Bug3{ - &Bug3{1, nil}, - &Bug3{2, nil}, + {1, nil}, + {2, nil}, } b := new(bytes.Buffer) err := NewEncoder(b).Encode(&in) diff --git a/libgo/go/encoding/gob/gobencdec_test.go b/libgo/go/encoding/gob/gobencdec_test.go index 83644c0331b..45240d764dc 100644 --- a/libgo/go/encoding/gob/gobencdec_test.go +++ b/libgo/go/encoding/gob/gobencdec_test.go @@ -573,3 +573,22 @@ func TestGobEncodeIsZero(t *testing.T) { t.Fatalf("%v != %v", x, y) } } + +func TestGobEncodePtrError(t *testing.T) { + var err error + b := new(bytes.Buffer) + enc := NewEncoder(b) + err = enc.Encode(&err) + if err != nil { + t.Fatal("encode:", err) + } + dec := NewDecoder(b) + err2 := fmt.Errorf("foo") + err = dec.Decode(&err2) + if err != nil { + t.Fatal("decode:", err) + } + if err2 != nil { + t.Fatalf("expected nil, got %v", err2) + } +} diff --git a/libgo/go/encoding/json/encode.go b/libgo/go/encoding/json/encode.go index 5425a3a90a1..14957b8487b 100644 --- a/libgo/go/encoding/json/encode.go +++ b/libgo/go/encoding/json/encode.go @@ -6,7 +6,7 @@ // RFC 4627. // // See "JSON and Go" for an introduction to this package: -// http://blog.golang.org/2011/01/json-and-go.html +// http://golang.org/doc/articles/json_and_go.html package json import ( @@ -43,7 +43,8 @@ import ( // to keep some browsers from misinterpreting JSON output as HTML. // // Array and slice values encode as JSON arrays, except that -// []byte encodes as a base64-encoded string. +// []byte encodes as a base64-encoded string, and a nil slice +// encodes as the null JSON object. // // Struct values encode as JSON objects. Each exported struct field // becomes a member of the object unless |