Skip to content

Commit

Permalink
embed relationable to part and package
Browse files Browse the repository at this point in the history
  • Loading branch information
qmuntal committed Jan 3, 2019
1 parent d8a8f27 commit 45b4c2b
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 18 deletions.
2 changes: 2 additions & 0 deletions package.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@ import (
// The package is also capable of storing relationships between parts.
// Defined in ISO/IEC 29500-2 §9.
type Package struct {
relationable
parts map[string]*Part
relationships map[string]*Relationship
}

// NewPackage returns a new initilized Package.
func NewPackage() *Package {
return &Package{
relationable: relationable{"/", make(map[string]*Relationship, 0)},
parts: make(map[string]*Part, 0),
relationships: make(map[string]*Relationship, 0),
}
Expand Down
4 changes: 3 additions & 1 deletion package_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ func createFakePackage(m ...string) *Package {
parts[strings.ToUpper(s)] = new(Part)
}
return &Package{
relationable: relationable{"/", make(map[string]*Relationship, 0)},
parts: parts,
relationships: nil,
}
Expand All @@ -36,7 +37,7 @@ func TestPackage_CreatePart(t *testing.T) {
{"collision1", createFakePackage("/abc.xml", "/xyz/PQR/A.JPG"), args{"/abc.xml/b.xml", "a/b", CompressionNone}, nil, true},
{"collision2", createFakePackage("/ABC.XML", "/XYZ/PQR/A.JPG"), args{"/xyz/pqr", "a/b", CompressionNone}, nil, true},
{"errorPart", NewPackage(), args{"a.xml", "a/b", CompressionNone}, nil, true},
{"base", NewPackage(), args{"/a.xml", "a/b", CompressionNone}, &Part{"/a.xml", "a/b", CompressionNone}, false},
{"base", NewPackage(), args{"/a.xml", "a/b", CompressionNone}, createFakePart("/a.xml", "a/b"), false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand All @@ -58,6 +59,7 @@ func TestNewPackage(t *testing.T) {
want *Package
}{
{"base", &Package{
relationable: relationable{"/", make(map[string]*Relationship, 0)},
parts: make(map[string]*Part, 0),
relationships: make(map[string]*Relationship, 0),
},
Expand Down
7 changes: 6 additions & 1 deletion part.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ const (
// Parts are analogous to a file in a file system or to a resource on an HTTP server.
// Defined in ISO/IEC 29500-2 §9.1.
type Part struct {
relationable
uri string
contentType string
compressionOption CompressionOption
Expand All @@ -129,7 +130,11 @@ func newPart(uri, contentType string, compressionOption CompressionOption) (*Par
return nil, err
}

return &Part{uri: uri, contentType: mime.FormatMediaType(mediatype, params), compressionOption: compressionOption}, err
return &Part{
relationable: relationable{uri, make(map[string]*Relationship, 0)},
uri: uri,
contentType: mime.FormatMediaType(mediatype, params),
compressionOption: compressionOption}, nil
}

// URI returns the URI of the part.
Expand Down
21 changes: 14 additions & 7 deletions part_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ import (

var fakeURL = "/doc/a.xml"

func createFakePart(uri, contentType string) *Part {
return &Part{
relationable: relationable{uri, make(map[string]*Relationship, 0)},
uri: uri,
contentType: contentType,
compressionOption: CompressionNone}
}

func Test_newPart(t *testing.T) {
type args struct {
uri string
Expand All @@ -19,9 +27,9 @@ func Test_newPart(t *testing.T) {
want *Part
wantErr bool
}{
{"base", args{fakeURL, "application/HTML", CompressionNone}, &Part{fakeURL, "application/html", CompressionNone}, false},
{"baseWithParameters", args{fakeURL, "TEXT/html; charset=ISO-8859-4", CompressionNone}, &Part{fakeURL, "text/html; charset=ISO-8859-4", CompressionNone}, false},
{"baseWithTwoParams", args{fakeURL, "TEXT/html; charset=ISO-8859-4;q=2", CompressionNone}, &Part{fakeURL, "text/html; charset=ISO-8859-4; q=2", CompressionNone}, false},
{"base", args{fakeURL, "application/HTML", CompressionNone}, createFakePart(fakeURL, "application/html"), false},
{"baseWithParameters", args{fakeURL, "TEXT/html; charset=ISO-8859-4", CompressionNone}, createFakePart(fakeURL, "text/html; charset=ISO-8859-4"), false},
{"baseWithTwoParams", args{fakeURL, "TEXT/html; charset=ISO-8859-4;q=2", CompressionNone}, createFakePart(fakeURL, "text/html; charset=ISO-8859-4; q=2"), false},
{"invalidMediaParams", args{fakeURL, "TEXT/html; charset=ISO-8859-4 q=2", CompressionNone}, nil, true},
{"mediaParamNoName", args{fakeURL, "TEXT/html; =ISO-8859-4", CompressionNone}, nil, true},
{"duplicateParamName", args{fakeURL, "TEXT/html; charset=ISO-8859-4; charset=ISO-8859-4", CompressionNone}, nil, true},
Expand Down Expand Up @@ -53,7 +61,7 @@ func TestPart_URI(t *testing.T) {
want string
}{
{"base", new(Part), ""},
{"partURI", &Part{fakeURL, "fakeContentType", CompressionNone}, fakeURL},
{"partURI", createFakePart(fakeURL, "fakeContentType"), fakeURL},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand All @@ -71,7 +79,7 @@ func TestPart_ContentType(t *testing.T) {
want string
}{
{"base", new(Part), ""},
{"partContentType", &Part{fakeURL, "fakeContentType", CompressionNone}, "fakeContentType"},
{"partContentType", createFakePart(fakeURL, "fakeContentType"), "fakeContentType"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand All @@ -89,8 +97,7 @@ func TestPart_CompressionOption(t *testing.T) {
want CompressionOption
}{
{"base", new(Part), CompressionNormal},
{"partCompressionOption", &Part{fakeURL, "fakeContentType", CompressionNone}, CompressionNone},
{"partCompressionOption", &Part{fakeURL, "fakeContentType", CompressionMaximum}, CompressionMaximum},
{"partCompressionOption", createFakePart(fakeURL, "fakeContentType"), CompressionNone},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
16 changes: 14 additions & 2 deletions relationship.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,12 @@ func (r *Relationship) writeToXML(e *xml.Encoder) error {
return e.EncodeElement(r.toXML(), xml.StartElement{Name: xml.Name{Space: "", Local: relationshipName}})
}

type relator struct {
type relationable struct {
sourceURI string
relationships map[string]*Relationship
}

func (r *relator) CreateRelationship(id, relType, targetURI string, targetMode TargetMode) (*Relationship, error) {
func (r *relationable) CreateRelationship(id, relType, targetURI string, targetMode TargetMode) (*Relationship, error) {
if _, ok := r.relationships[id]; ok {
return nil, errors.New("OPC: relationship ID shall be unique within the Relationship part")
}
Expand All @@ -172,3 +172,15 @@ func (r *relator) CreateRelationship(id, relType, targetURI string, targetMode T
r.relationships[id] = rel
return rel, nil
}

func (r *relationable) HasRelationship() bool {
return len(r.relationships) > 0
}

func (r *relationable) Relationships() []*Relationship {
v := make([]*Relationship, 0, len(r.relationships))
for _, rel := range r.relationships {
v = append(v, rel)
}
return v
}
49 changes: 42 additions & 7 deletions relationship_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func Test_newRelationship(t *testing.T) {
}
}

func Test_relator_CreateRelationship(t *testing.T) {
func Test_relationable_CreateRelationship(t *testing.T) {
type args struct {
id string
relType string
Expand All @@ -135,24 +135,24 @@ func Test_relator_CreateRelationship(t *testing.T) {
}
tests := []struct {
name string
r *relator
r *relationable
args args
want *Relationship
wantErr bool
}{
{"duplicatedID", &relator{"/", map[string]*Relationship{"/a": new(Relationship)}}, args{"/a", "http://a.com", "/a.xml", ModeInternal}, nil, true},
{"newRelErr", &relator{"/", map[string]*Relationship{}}, args{"", "http://a.com", "/a.xml", ModeInternal}, nil, true},
{"base", &relator{"/", map[string]*Relationship{}}, args{"/a", "http://a.com", "/a.xml", ModeInternal}, &Relationship{"/a", "http://a.com", "", "/a.xml", ModeInternal}, false},
{"duplicatedID", &relationable{"/", map[string]*Relationship{"/a": new(Relationship)}}, args{"/a", "http://a.com", "/a.xml", ModeInternal}, nil, true},
{"newRelErr", &relationable{"/", map[string]*Relationship{}}, args{"", "http://a.com", "/a.xml", ModeInternal}, nil, true},
{"base", &relationable{"/", map[string]*Relationship{}}, args{"/a", "http://a.com", "/a.xml", ModeInternal}, &Relationship{"/a", "http://a.com", "", "/a.xml", ModeInternal}, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := tt.r.CreateRelationship(tt.args.id, tt.args.relType, tt.args.targetURI, tt.args.targetMode)
if (err != nil) != tt.wantErr {
t.Errorf("relator.CreateRelationship() error = %v, wantErr %v", err, tt.wantErr)
t.Errorf("relationable.CreateRelationship() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("relator.CreateRelationship() = %v, want %v", got, tt.want)
t.Errorf("relationable.CreateRelationship() = %v, want %v", got, tt.want)
}
})
}
Expand Down Expand Up @@ -184,3 +184,38 @@ func Test_isRelationshipURI(t *testing.T) {
})
}
}

func Test_relationable_Relationships(t *testing.T) {
tests := []struct {
name string
r *relationable
want []*Relationship
}{
{"base", &relationable{"/", map[string]*Relationship{"/a": new(Relationship)}}, []*Relationship{new(Relationship)}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.r.Relationships(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("relationable.Relationships() = %v, want %v", got, tt.want)
}
})
}
}

func Test_relationable_HasRelationship(t *testing.T) {
tests := []struct {
name string
r *relationable
want bool
}{
{"base", &relationable{"/", map[string]*Relationship{"/a": new(Relationship)}}, true},
{"empty", &relationable{"/", map[string]*Relationship{}}, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.r.HasRelationship(); got != tt.want {
t.Errorf("relationable.HasRelationship() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit 45b4c2b

Please sign in to comment.