Add custom JSON representation of PublishedRepo. #116

This commit is contained in:
Andrey Smirnov
2014-12-26 00:58:23 +03:00
parent ee7d84205b
commit 9b1b43c8b4

View File

@@ -3,6 +3,7 @@ package deb
import (
"bytes"
"code.google.com/p/go-uuid/uuid"
"encoding/json"
"fmt"
"github.com/smira/aptly/aptly"
"github.com/smira/aptly/database"
@@ -255,6 +256,40 @@ func NewPublishedRepo(storage, prefix, distribution string, architectures []stri
return result, nil
}
// MarshalJSON requires object to be "loeaded completely"
func (p *PublishedRepo) MarshalJSON() ([]byte, error) {
type sourceInfo struct {
Component, Name string
}
sources := []sourceInfo{}
for component, item := range p.sourceItems {
name := ""
if item.snapshot != nil {
name = item.snapshot.Name
} else if item.localRepo != nil {
name = item.localRepo.Name
} else {
panic("no snapshot/local repo")
}
sources = append(sources, sourceInfo{
Component: component,
Name: name,
})
}
return json.Marshal(map[string]interface{}{
"Architectures": p.Architectures,
"Distribution": p.Distribution,
"Label": p.Label,
"Origin": p.Origin,
"Prefix": p.Prefix,
"SourceKind": p.SourceKind,
"Sources": sources,
"Storage": p.Storage,
})
}
// String returns human-readable represenation of PublishedRepo
func (p *PublishedRepo) String() string {
var sources = []string{}