Add accessor to decode from package String()

This commit is contained in:
Ettore Di Giacinto
2020-02-10 17:14:46 +01:00
parent f068bfdb9b
commit 33b442a832
2 changed files with 27 additions and 6 deletions

View File

@@ -85,6 +85,8 @@ type Package interface {
IsSelector() bool IsSelector() bool
VersionMatchSelector(string) (bool, error) VersionMatchSelector(string) (bool, error)
SelectorMatchVersion(string) (bool, error) SelectorMatchVersion(string) (bool, error)
String() string
} }
type Tree interface { type Tree interface {
@@ -128,7 +130,7 @@ func (t *DefaultPackage) JSON() ([]byte, error) {
// DefaultPackage represent a standard package definition // DefaultPackage represent a standard package definition
type DefaultPackage struct { type DefaultPackage struct {
ID int `storm:"id,increment"` // primary key with auto increment ID int `storm:"id,increment" json:"id"` // primary key with auto increment
Name string `json:"name"` // Affects YAML field names too. Name string `json:"name"` // Affects YAML field names too.
Version string `json:"version"` // Affects YAML field names too. Version string `json:"version"` // Affects YAML field names too.
Category string `json:"category"` // Affects YAML field names too. Category string `json:"category"` // Affects YAML field names too.
@@ -160,7 +162,7 @@ func NewPackage(name, version string, requires []*DefaultPackage, conflicts []*D
func (p *DefaultPackage) String() string { func (p *DefaultPackage) String() string {
b, err := p.JSON() b, err := p.JSON()
if err != nil { if err != nil {
return fmt.Sprintf("{ id: \"%d\", name: \"%s\" }", p.ID, p.Name) return fmt.Sprintf("{ id: \"%d\", name: \"%s\", version: \"%s\", category: \"%s\" }", p.ID, p.Name, p.Version, p.Category)
} }
return fmt.Sprintf("%s", string(b)) return fmt.Sprintf("%s", string(b))
} }
@@ -171,6 +173,16 @@ func (p *DefaultPackage) GetFingerPrint() string {
return fmt.Sprintf("%s-%s-%s", p.Name, p.Category, p.Version) return fmt.Sprintf("%s-%s-%s", p.Name, p.Category, p.Version)
} }
func FromString(s string) Package {
var unescaped DefaultPackage
err := json.Unmarshal([]byte(s), &unescaped)
if err != nil {
return &unescaped
}
return &unescaped
}
func (p *DefaultPackage) GetPackageName() string { func (p *DefaultPackage) GetPackageName() string {
return fmt.Sprintf("%s-%s", p.Name, p.Category) return fmt.Sprintf("%s-%s", p.Name, p.Category)
} }

View File

@@ -23,6 +23,15 @@ import (
var _ = Describe("Package", func() { var _ = Describe("Package", func() {
Context("Encoding/Decoding", func() {
a := &DefaultPackage{Name: "test", Version: "1", Category: "t"}
It("Encodes and decodes correctly", func() {
Expect(a.String()).ToNot(Equal(""))
p := FromString(a.String())
Expect(p).To(Equal(a))
})
})
Context("Simple package", func() { Context("Simple package", func() {
a := NewPackage("A", ">=1.0", []*DefaultPackage{}, []*DefaultPackage{}) a := NewPackage("A", ">=1.0", []*DefaultPackage{}, []*DefaultPackage{})
a1 := NewPackage("A", "1.0", []*DefaultPackage{}, []*DefaultPackage{}) a1 := NewPackage("A", "1.0", []*DefaultPackage{}, []*DefaultPackage{})