FAQ
Reviewers: golang-dev1,

Message:
Hello golang-dev@googlegroups.com,

I'd like you to review this change to
https://code.google.com/p/go


Description:
net/url: Add support for encoding/decoding Userinfo objects with gob

This also makes url.URL objects support encoding/decoding with gob

Please review this at https://codereview.appspot.com/8325045/

Affected files:
M src/pkg/net/url/url.go
M src/pkg/net/url/url_test.go


Index: src/pkg/net/url/url.go
===================================================================
--- a/src/pkg/net/url/url.go
+++ b/src/pkg/net/url/url.go
@@ -8,6 +8,7 @@

import (
"bytes"
+ "encoding/binary"
"errors"
"sort"
"strconv"
@@ -287,6 +288,62 @@
return s
}

+// Implement GobEncoder/GobDecoder so UserInfo and URL objects can be
serialized
+func (u *Userinfo) GobEncode() ([]byte, error) {
+ buf := bytes.NewBuffer(nil)
+ ul := int16(len(u.username))
+ if err := binary.Write(buf, binary.BigEndian, ul); err != nil {
+ return nil, err
+ }
+ if _, err := buf.Write([]byte(u.username)); err != nil {
+ return nil, err
+ }
+ if u.passwordSet {
+ pl := int16(len(u.password))
+ if err := binary.Write(buf, binary.BigEndian, pl); err != nil {
+ return nil, err
+ }
+ if _, err := buf.Write([]byte(u.password)); err != nil {
+ return nil, err
+ }
+ } else {
+ if err := binary.Write(buf, binary.BigEndian, int16(-1)); err != nil {
+ return nil, err
+ }
+ }
+ return buf.Bytes(), nil
+}
+
+func (u *Userinfo) GobDecode(b []byte) error {
+ r := bytes.NewReader(b)
+ var ul int16
+ if err := binary.Read(r, binary.BigEndian, &ul); err != nil {
+ return err
+ }
+ if ul > 0 {
+ ub := make([]byte, int(ul))
+ if _, err := r.Read(ub); err != nil {
+ return err
+ }
+ u.username = string(ub)
+ }
+ var pl int16
+ if err := binary.Read(r, binary.BigEndian, &pl); err != nil {
+ return err
+ }
+ if pl >= 0 {
+ u.passwordSet = true
+ if pl > 0 {
+ pb := make([]byte, int(pl))
+ if _, err := r.Read(pb); err != nil {
+ return err
+ }
+ u.password = string(pb)
+ }
+ }
+ return nil
+}
+
// Maybe rawurl is of the form scheme:path.
// (Scheme must be [a-zA-Z][a-zA-Z0-9+-.]*)
// If so, return scheme, path; else return "", rawurl.
Index: src/pkg/net/url/url_test.go
===================================================================
--- a/src/pkg/net/url/url_test.go
+++ b/src/pkg/net/url/url_test.go
@@ -5,6 +5,8 @@
package url

import (
+ "bytes"
+ "encoding/gob"
"fmt"
"reflect"
"strings"
@@ -868,3 +870,28 @@
t.Errorf(`ParseQuery(%q) returned error %q, want something
containing %q"`, url, errStr, "%gh")
}
}
+
+func TestGobEncode(t *testing.T) {
+ gob.Register(&URL{})
+ tests := []string{
+ "http://alberto:zudo@example.com/foo?bar=baz#foobar",
+ "http://alberto@example.com/foo?bar=baz#foobar",
+ }
+ for _, s := range tests {
+ url1, err := Parse(s)
+ if err != nil {
+ t.Fatal(err)
+ }
+ var buf bytes.Buffer
+ if err := gob.NewEncoder(&buf).Encode(url1); err != nil {
+ t.Fatal(err)
+ }
+ var url2 *URL
+ if err := gob.NewDecoder(bytes.NewBuffer(buf.Bytes())).Decode(&url2);
err != nil {
+ t.Fatal(err)
+ }
+ if url1.String() != url2.String() {
+ t.Error("URL was not decoded correctly. Want %q, got %1",
url1.String(), url2.String())
+ }
+ }
+}


--

---
You received this message because you are subscribed to the Google Groups "golang-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-dev+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • Andrew Gerrand at Apr 8, 2013 at 12:38 pm
    Please hold onto this until after Go 1.1.

    --

    ---
    You received this message because you are subscribed to the Google Groups "golang-dev" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-dev+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-dev @
categoriesgo
postedApr 8, '13 at 8:54a
activeApr 8, '13 at 12:38p
posts2
users2
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase