Skip to content

Commit 917e399

Browse files
Merge pull request #32 from thiagomourahp/master
fix encoding to avoid duplicated references
2 parents 6760bcd + 06c13ce commit 917e399

File tree

4 files changed

+110
-1
lines changed

4 files changed

+110
-1
lines changed

encoder.go

+10-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,16 @@ func newXMLEncoder(w io.Writer, floatPresicion int) *xmlEncoder {
3636
// AddRelationship adds a relationship to the encoded model.
3737
// Duplicated relationships will be removed before encoding.
3838
func (enc *xmlEncoder) AddRelationship(r spec.Relationship) {
39-
enc.relationships = append(enc.relationships, Relationship(r))
39+
hasRelationship := false
40+
for _, relationship := range enc.relationships {
41+
if relationship.Path == r.Path {
42+
hasRelationship = true
43+
break
44+
}
45+
}
46+
if !hasRelationship {
47+
enc.relationships = append(enc.relationships, Relationship(r))
48+
}
4049
}
4150

4251
// FloatPresicion returns the float presicion to use

encoder_test.go

+61
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@ import (
88
"encoding/xml"
99
"errors"
1010
"image/color"
11+
"path/filepath"
1112
"reflect"
1213
"strconv"
1314
"testing"
1415

1516
"github.com/go-test/deep"
1617
"github.com/hpinc/go3mf/spec"
18+
"github.com/hpinc/go3mf/utils"
1719
"github.com/stretchr/testify/mock"
1820
)
1921

@@ -309,3 +311,62 @@ func TestEncoder_Encode_Roundtrip(t *testing.T) {
309311
})
310312
}
311313
}
314+
315+
func TestEncoder_Relationship(t *testing.T) {
316+
type request struct {
317+
fileName string
318+
}
319+
type response struct {
320+
relationships []Relationship
321+
}
322+
323+
tests := []struct {
324+
name string
325+
request request
326+
response response
327+
}{ /*Verfy if a file rels is not update in case of relationship exists*/
328+
{"file_with_texture_rel_but_no_texture",
329+
request{fileName: "super_boogoku_tiny.3mf"},
330+
response{relationships: []Relationship{
331+
{ID: "rel2", Type: "http://schemas.microsoft.com/3dmanufacturing/2013/01/3dtexture", Path: "/Thumbnails/super_boo.png"},
332+
{ID: "rel3", Type: "http://schemas.microsoft.com/3dmanufacturing/2013/01/3dtexture", Path: "/Thumbnails/goku_ss.png"},
333+
}},
334+
},
335+
}
336+
for _, tt := range tests {
337+
t.Run(tt.name, func(t *testing.T) {
338+
utils.WithProjectDirAndTestTempDirRemoveAtEnd(
339+
filepath.Join("testdata"), func(testFileDir string, testTempDir string) {
340+
testFile := filepath.Join(testFileDir, tt.request.fileName)
341+
outputFile := filepath.Join(testTempDir, "SW3DBUG-2700_changed.3mf")
342+
var model Model
343+
r, err := OpenReader(testFile)
344+
if err != nil {
345+
t.Errorf("TestEncoder_Relationship() error = %v", err)
346+
}
347+
r.Decode(&model)
348+
defer r.Close()
349+
w, err := CreateWriter(outputFile)
350+
if err != nil {
351+
t.Errorf("TestEncoder_Relationship() error = %v", err)
352+
}
353+
w.Encode(&model)
354+
defer w.Close()
355+
356+
var modelUpdated Model
357+
r, err = OpenReader(outputFile)
358+
if err != nil {
359+
t.Errorf("TestEncoder_Relationship() error = %v", err)
360+
}
361+
r.Decode(&modelUpdated)
362+
defer r.Close()
363+
364+
if diff := deep.Equal(modelUpdated.Relationships, tt.response.relationships); diff != nil {
365+
t.Errorf("TestEncoder_Relationship() = %v", diff)
366+
}
367+
})
368+
369+
})
370+
}
371+
372+
}

testdata/super_boogoku_tiny.3mf

752 KB
Binary file not shown.

utils/utils.go

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package utils
2+
3+
import (
4+
"io/ioutil"
5+
"os"
6+
"path"
7+
"path/filepath"
8+
"runtime"
9+
)
10+
11+
func GetRootProjectDir() (string, error) {
12+
_, filename, _, _ := runtime.Caller(0)
13+
dir := filepath.Join(path.Dir(filename), "..")
14+
err := os.Chdir(dir)
15+
return dir, err
16+
}
17+
18+
func WithRootProjectDir(callback func(string)) {
19+
dir, err := GetRootProjectDir()
20+
if err != nil {
21+
panic(err)
22+
}
23+
callback(dir)
24+
}
25+
26+
func WithProjectDirAndTestTempDirRemoveAtEnd(fileFolder string, callback func(testFileDir string, testTempDir string)) {
27+
dir, err := GetRootProjectDir()
28+
if err != nil {
29+
panic(err)
30+
}
31+
testTempDir, err := ioutil.TempDir("", "testcase")
32+
if err != nil {
33+
panic(err)
34+
}
35+
defer os.RemoveAll(testTempDir)
36+
defer os.Remove(testTempDir)
37+
38+
callback(filepath.Join(dir, fileFolder), testTempDir)
39+
}

0 commit comments

Comments
 (0)