this seems okay

This commit is contained in:
Bel LaPointe
2019-02-18 16:12:47 -07:00
parent cbf886fb7e
commit 1d854cfff5
17 changed files with 632 additions and 710 deletions

View File

@@ -1,5 +1,11 @@
package packable
import (
"bytes"
"encoding/gob"
"net/url"
)
type Packable interface {
Encode() ([]byte, error)
Decode([]byte) error
@@ -20,7 +26,50 @@ func (s *String) Decode(b []byte) error {
return nil
}
func NewString(s string) *String {
w := String(s)
func NewString(s ...string) *String {
if len(s) == 0 {
s = append(s, "")
}
w := String(s[0])
return &w
}
type URL struct {
Scheme string
Host string
Path string
}
func (u *URL) URL() *url.URL {
return &url.URL{
Scheme: u.Scheme,
Host: u.Host,
Path: u.Path,
}
}
func (u *URL) Encode() ([]byte, error) {
buf := bytes.NewBuffer(nil)
g := gob.NewEncoder(buf)
if err := g.Encode(*u); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
func (u *URL) Decode(b []byte) error {
buf := bytes.NewBuffer(b)
g := gob.NewDecoder(buf)
return g.Decode(&u)
}
func NewURL(u ...*url.URL) *URL {
if len(u) == 0 {
u = append(u, &url.URL{})
}
return &URL{
Scheme: u[0].Scheme,
Host: u[0].Host,
Path: u[0].Path,
}
}