Browse Source

Getting rid of custom field types

v0
maze 4 years ago
parent
commit
776c0643cf
8 changed files with 82 additions and 107 deletions
  1. +20
    -3
      aur/client.go
  2. +8
    -1
      aur/client_test.go
  3. +0
    -4
      field/doc.go
  4. +0
    -29
      field/time.go
  5. +0
    -52
      field/url.go
  6. +44
    -8
      package.go
  7. +3
    -5
      package_test.go
  8. +7
    -5
      repository_test.go

+ 20
- 3
aur/client.go View File

@ -7,9 +7,9 @@ import (
"net/url"
"strconv"
"strings"
"time"
archlinux "maze.io/archlinux.v0"
"maze.io/archlinux.v0/field"
)
// Defaults
@ -146,12 +146,29 @@ type Info struct {
NumVotes int
Popularity float64
Maintainer string
FirstSubmitted field.Timestamp
LastModified field.Timestamp
FirstSubmitted time.Time
LastModified time.Time
URLPath string
Keywords []string
}
func (i *Info) UnmarshalJSON(data []byte) error {
type Alias Info
aux := &struct {
FirstSubmitted int64
LastModified int64
*Alias
}{
Alias: (*Alias)(i),
}
if err := json.Unmarshal(data, &aux); err != nil {
return err
}
i.FirstSubmitted = time.Unix(aux.FirstSubmitted, 0)
i.LastModified = time.Unix(aux.LastModified, 0)
return nil
}
// fixup release information
func fixup(results []*Info) {
// Rectify releases


+ 8
- 1
aur/client_test.go View File

@ -1,8 +1,15 @@
package aur
import "testing"
import (
"os"
"testing"
)
func TestClient(t *testing.T) {
if os.Getenv("TEST_CLIENT") == "" {
t.Skip("TEST_CLIENT not set")
}
var (
c = New()
r []*Info


+ 0
- 4
field/doc.go View File

@ -1,4 +0,0 @@
/*
Package field implements field types used by Arch Linux
*/
package field

+ 0
- 29
field/time.go View File

@ -1,29 +0,0 @@
package field
import (
"encoding/json"
"time"
)
// Timestamp is a UNIX timestamp
type Timestamp time.Time
// Time converts back to time.Time
func (t Timestamp) Time() time.Time {
return time.Time(t)
}
// MarshalJSON converts the timestamp to a JSON encoded UNIX timestamp
func (t *Timestamp) MarshalJSON() ([]byte, error) {
return json.Marshal((*time.Time)(t).Unix())
}
// UnmarshalJSON parses a JSON encoded UNIX timestamp to time
func (t *Timestamp) UnmarshalJSON(data []byte) error {
var i int64
if err := json.Unmarshal(data, &i); err != nil {
return err
}
*t = Timestamp(time.Unix(i, 0))
return nil
}

+ 0
- 52
field/url.go View File

@ -1,52 +0,0 @@
package field
import (
"encoding/json"
"net/url"
)
// URL is a string encoded URL
type URL url.URL
// ParseURL is like url.Parse
func ParseURL(s string) (*URL, error) {
u, err := url.Parse(s)
if err != nil {
return nil, err
}
return (*URL)(u), nil
}
func (u *URL) String() string {
return (*url.URL)(u).String()
}
// URL converts the URL back to an url.URL
func (u *URL) URL() *url.URL {
return (*url.URL)(u)
}
// MarshalJSON marshals the URL into a JSON string
func (u *URL) MarshalJSON() ([]byte, error) {
var s string
if u != nil {
s = u.String()
}
return json.Marshal(s)
}
// UnmarshalJSON unmarshals a string into an URL
func (u *URL) UnmarshalJSON(b []byte) error {
var s string
if err := json.Unmarshal(b, &s); err != nil {
return err
}
if s != "" {
t, err := url.Parse(s)
if err != nil {
return err
}
*u = URL(*t)
}
return nil
}

+ 44
- 8
package.go View File

@ -4,14 +4,15 @@ import (
"bufio"
"encoding/base64"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io"
"net/url"
"strconv"
"strings"
"time"
"maze.io/archlinux.v0/field"
"maze.io/archlinux.v0/tarball"
"maze.io/archlinux.v0/version"
)
@ -53,7 +54,7 @@ type Package struct {
Arch []string
// URL of the project
URL *field.URL
URL *url.URL
// License one or more licenses
License []string
@ -121,7 +122,7 @@ type Package struct {
BuildEnv []string
// BuildDate is the date the package was built
BuildDate field.Timestamp
BuildDate time.Time
// Packager name and email
Packager string
@ -155,6 +156,41 @@ func (pkg Package) String() string {
return strings.Join(s, "-")
}
func (pkg *Package) MarshalJSON() ([]byte, error) {
type Alias Package
return json.Marshal(&struct {
BuildDate int64
URL string
*Alias
}{
BuildDate: pkg.BuildDate.Unix(),
URL: pkg.URL.String(),
Alias: (*Alias)(pkg),
})
}
func (pkg *Package) UnmarshalJSON(data []byte) error {
type Alias Package
var (
aux = &struct {
BuildDate int64
URL string
*Alias
}{
Alias: (*Alias)(pkg),
}
err error
)
if err = json.Unmarshal(data, &aux); err != nil {
return err
}
if pkg.URL, err = url.Parse(aux.URL); err != nil {
return err
}
pkg.BuildDate = time.Unix(aux.BuildDate, 0)
return nil
}
// ReadPackage reads a package description from a package tarball.
func ReadPackage(r io.Reader) (*Package, error) {
t, err := tarball.NewReader(r)
@ -285,7 +321,7 @@ func (pkg *Package) readPackageInfo(r io.Reader) (err error) {
case "pkgdesc":
pkg.Description = fields[1]
case "url":
if pkg.URL, err = field.ParseURL(fields[1]); err != nil {
if pkg.URL, err = url.Parse(fields[1]); err != nil {
return fmt.Errorf("archlinux: invalid url %q: %v", fields[1], err)
}
case "builddate":
@ -293,7 +329,7 @@ func (pkg *Package) readPackageInfo(r io.Reader) (err error) {
if t, err = strconv.ParseInt(fields[1], 10, 64); err != nil {
return fmt.Errorf("archlinux: invalid build date %q: %v", fields[1], err)
}
pkg.BuildDate = field.Timestamp(time.Unix(t, 0))
pkg.BuildDate = time.Unix(t, 0)
case "packager":
pkg.Packager = fields[1]
case "size":
@ -386,7 +422,7 @@ func (pkg *Package) WritePackageInfo(w io.Writer) error {
return err
}
}
case *field.URL:
case *url.URL:
if value == nil {
continue
}
@ -472,7 +508,7 @@ func (pkg *Package) readPackageDesc(r io.Reader) (err error) {
return
}
case "URL":
if pkg.URL, err = field.ParseURL(lines[0]); err != nil {
if pkg.URL, err = url.Parse(lines[0]); err != nil {
return
}
case "DEPENDS":
@ -501,7 +537,7 @@ func (pkg *Package) readPackageDesc(r io.Reader) (err error) {
if i, err = strconv.ParseInt(lines[0], 10, 64); err != nil {
return
}
pkg.BuildDate = field.Timestamp(time.Unix(i, 0))
pkg.BuildDate = time.Unix(i, 0)
case "PACKAGER":
pkg.Packager = lines[0]
default:


+ 3
- 5
package_test.go View File

@ -10,7 +10,6 @@ import (
"testing"
"time"
"maze.io/archlinux.v0/field"
"maze.io/archlinux.v0/version"
)
@ -25,12 +24,12 @@ func (r testReadNamer) Name() string {
func ExampleParsePackageName() {
fmt.Println(ParsePackageName("go-2:1.9-1"))
// Output: go 2:1.9-1 nil
// Output: go 2:1.9-1 <nil>
}
func ExampleParsePackageArchiveName() {
fmt.Println(ParsePackageArchiveName("go-2:1.9-1-x86_64.pkg.tar.xz"))
// Output: go 2:1.9-1 x86_64 .pkg.tar.xz nil
// Output: go 2:1.9-1 x86_64 .pkg.tar.xz <nil>
}
// This example demonstrates how to load a package from disk
@ -49,7 +48,6 @@ func ExampleReadPackage() {
}
fmt.Println(pkg, pkg.Description)
// Output: go-2:1.9-1 Core compiler tools for the Go programming language
}
func testComparePackage(t *testing.T, a, b *Package) {
@ -287,7 +285,7 @@ var (
OptionalDepends: []string{"imagemagick-doc: for additional information", "ghostscript: for Ghostscript support", "openexr: for OpenEXR support", "openjpeg2: for JP2 support", "libwmf: for WMF support", "librsvg: for SVG support", "libxml2: for XML support", "libpng: for PNG support", "libwebp: for WEBP support", "libraw: for DNG support"},
Backup: []string{"etc/ImageMagick-6/coder.xml", "etc/ImageMagick-6/colors.xml", "etc/ImageMagick-6/delegates.xml", "etc/ImageMagick-6/log.xml", "etc/ImageMagick-6/magic.xml", "etc/ImageMagick-6/mime.xml", "etc/ImageMagick-6/policy.xml", "etc/ImageMagick-6/quantization-table.xml", "etc/ImageMagick-6/thresholds.xml", "etc/ImageMagick-6/type.xml", "etc/ImageMagick-6/type-dejavu.xml", "etc/ImageMagick-6/type-ghostscript.xml", "etc/ImageMagick-6/type-windows.xml"},
Size: 9897984,
BuildDate: field.Timestamp(time.Unix(1506203625, 0)),
BuildDate: time.Unix(1506203625, 0),
Packager: "Antonio Rojas <arojas@archlinux.org>",
}
)


+ 7
- 5
repository_test.go View File

@ -4,7 +4,7 @@ import (
"bytes"
"fmt"
"io"
"net/http"
"os"
"testing"
)
@ -18,16 +18,18 @@ func (r testNamedReader) Name() string {
}
func ExampleReadRepositoryDB() {
db := "http://mirrors.kernel.org/archlinux/core/os/x86_64/core.db"
response, _ := http.Get(db)
repo, err := ReadRepositoryDB(response.Body)
f, err := os.Open("/var/lib/pacman/sync/core.db")
if os.IsNotExist(err) {
return
}
repo, err := ReadRepositoryDB(f)
if err != nil {
fmt.Println("error reading repository:", err)
return
}
fmt.Println("core.db has", len(repo.Packages), "packages")
// Output: core.db has 3099 packages
// Output: core.db has 250 packages
}
func TestReadRepositoryDB(t *testing.T) {


Loading…
Cancel
Save