Skip to content

Commit

Permalink
node correctly manages zeros array
Browse files Browse the repository at this point in the history
  • Loading branch information
qmuntal committed Mar 25, 2019
1 parent 7f33887 commit eed3107
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
23 changes: 14 additions & 9 deletions struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,19 +156,15 @@ type Node struct {
Weights []float64 `json:"weights,omitempty"` // The weights of the instantiated Morph Target.
}

// NewNode returns a default Node.
func NewNode() *Node {
return &Node{
// UnmarshalJSON unmarshal the node with the correct default values.
func (n *Node) UnmarshalJSON(data []byte) error {
type alias Node
def := Node{
Matrix: [16]float64{1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1},
Rotation: [4]float64{0, 0, 0, 1},
Scale: [3]float64{1, 1, 1},
}
}

// UnmarshalJSON unmarshal the node with the correct default values.
func (n *Node) UnmarshalJSON(data []byte) error {
type alias Node
tmp := alias(*NewNode())
tmp := alias(def)
err := json.Unmarshal(data, &tmp)
if err == nil {
*n = Node(tmp)
Expand All @@ -183,13 +179,22 @@ func (n *Node) MarshalJSON() ([]byte, error) {
if err == nil {
if n.Matrix == [16]float64{1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1} {
out = removeProperty([]byte(`"matrix":[1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1]`), out)
} else if n.Matrix == [16]float64{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} {
out = removeProperty([]byte(`"matrix":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]`), out)
}

if n.Rotation == [4]float64{0, 0, 0, 1} {
out = removeProperty([]byte(`"rotation":[0,0,0,1]`), out)
} else if n.Rotation == [4]float64{0, 0, 0, 0} {
out = removeProperty([]byte(`"rotation":[0,0,0,0]`), out)
}

if n.Scale == [3]float64{1, 1, 1} {
out = removeProperty([]byte(`"scale":[1,1,1]`), out)
} else if n.Scale == [3]float64{0, 0, 0} {
out = removeProperty([]byte(`"scale":[0,0,0]`), out)
}

if n.Translation == [3]float64{0, 0, 0} {
out = removeProperty([]byte(`"translation":[0,0,0]`), out)
}
Expand Down
7 changes: 6 additions & 1 deletion struct_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,14 +258,19 @@ func TestNode_MarshalJSON(t *testing.T) {
Rotation: [4]float64{0, 0, 0, 1},
Scale: [3]float64{1, 1, 1},
}, []byte("{}"), false},
{"default2", &Node{
Matrix: [16]float64{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
Rotation: [4]float64{0, 0, 0, 0},
Scale: [3]float64{0, 0, 0},
}, []byte("{}"), false},
{"empty", &Node{
Matrix: [16]float64{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
Rotation: [4]float64{0, 0, 0, 0},
Scale: [3]float64{0, 0, 0},
Camera: Index(0),
Skin: Index(0),
Mesh: Index(0),
}, []byte(`{"camera":0,"skin":0,"matrix":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],"mesh":0,"rotation":[0,0,0,0],"scale":[0,0,0]}`), false},
}, []byte(`{"camera":0,"skin":0,"mesh":0}`), false},
{"nodefault", &Node{
Matrix: [16]float64{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
Rotation: [4]float64{1, 0, 0, 0},
Expand Down

0 comments on commit eed3107

Please sign in to comment.