Skip to content

Commit

Permalink
Merge pull request #28 from HPInc/mesh-attrs
Browse files Browse the repository at this point in the history
Improve extension mechanism for authorizing new specs
  • Loading branch information
qmuntal authored Oct 14, 2021
2 parents 15713ab + 207a8cb commit 41d6751
Show file tree
Hide file tree
Showing 51 changed files with 1,420 additions and 1,333 deletions.
32 changes: 16 additions & 16 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,28 @@ jobs:
test:
strategy:
matrix:
go-version: [1.13.x, 1.14.x, 1.15.x]
go-version: [1.15.x, 1.16.x, 1.17.x]
platform: [ubuntu-latest, macos-latest, windows-latest]
runs-on: ${{ matrix.platform }}
steps:
- name: Install Go
uses: actions/setup-go@v2
with:
go-version: ${{ matrix.go-version }}
- name: Checkout code
uses: actions/checkout@v2
- name: Test
run: go test -race -covermode atomic -coverprofile profile.cov ./...
- name: Send coverage
uses: shogo82148/actions-goveralls@v1
with:
path-to-profile: profile.cov
flag-name: Go-${{ matrix.go-version }}
parallel: true
- name: Install Go
uses: actions/setup-go@v2
with:
go-version: ${{ matrix.go-version }}
- name: Checkout code
uses: actions/checkout@v2
- name: Test
run: go test -race -covermode atomic -coverprofile profile.cov ./...
- name: Send coverage
uses: shogo82148/actions-goveralls@v1
with:
path-to-profile: profile.cov
flag-name: Go-${{ matrix.go-version }}
parallel: true
finish:
needs: test
runs-on: ubuntu-latest
steps:
- uses: shogo82148/actions-goveralls@v1
with:
parallel-finished: true
parallel-finished: true
16 changes: 13 additions & 3 deletions beamlattice/beamlattice.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ package beamlattice

import (
"errors"

"github.com/hpinc/go3mf"
"github.com/hpinc/go3mf/spec"
)

// Namespace is the canonical name of this extension.
Expand All @@ -26,7 +28,7 @@ var (
)

func init() {
go3mf.Register(Namespace, Spec{})
spec.Register(Namespace, Spec{})
}

type Spec struct{}
Expand Down Expand Up @@ -90,12 +92,20 @@ type BeamLattice struct {
ClipMode ClipMode
ClippingMeshID uint32
RepresentationMeshID uint32
Beams []Beam
BeamSets []BeamSet
Beams Beams
BeamSets BeamSets
MinLength, Radius float32
CapMode CapMode
}

type Beams struct {
Beam []Beam
}

type BeamSets struct {
BeamSet []BeamSet
}

func GetBeamLattice(mesh *go3mf.Mesh) *BeamLattice {
for _, a := range mesh.Any {
if a, ok := a.(*BeamLattice); ok {
Expand Down
4 changes: 4 additions & 0 deletions beamlattice/beamlattice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ import (
)

var _ spec.Marshaler = new(BeamLattice)
var _ spec.ChildElementDecoder = new(beamLatticeDecoder)
var _ spec.ChildElementDecoder = new(beamsDecoder)
var _ spec.ChildElementDecoder = new(beamSetsDecoder)
var _ spec.ChildElementDecoder = new(beamSetDecoder)

func TestCapMode_String(t *testing.T) {
tests := []struct {
Expand Down
107 changes: 47 additions & 60 deletions beamlattice/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,32 @@ import (
"encoding/xml"
"strconv"

"github.com/hpinc/go3mf"
specerr "github.com/hpinc/go3mf/errors"
"github.com/hpinc/go3mf/spec"
)

func (Spec) DecodeAttribute(interface{}, spec.Attr) error {
func (Spec) NewAttrGroup(xml.Name) spec.AttrGroup {
return nil
}

func (Spec) CreateElementDecoder(parent interface{}, name string) spec.ElementDecoder {
if name == attrBeamLattice {
return &beamLatticeDecoder{mesh: parent.(*go3mf.Mesh)}
func (Spec) NewElementDecoder(name xml.Name) spec.GetterElementDecoder {
if name.Space == Namespace && name.Local == attrBeamLattice {
return new(beamLatticeDecoder)
}
return nil
}

type beamLatticeDecoder struct {
baseDecoder
mesh *go3mf.Mesh
beamLattice BeamLattice
}

func (d *beamLatticeDecoder) Start(attrs []spec.Attr) error {
func (d *beamLatticeDecoder) Element() interface{} {
return &d.beamLattice
}

func (d *beamLatticeDecoder) Start(attrs []spec.XMLAttr) error {
var errs error
beamLattice := new(BeamLattice)
d.mesh.Any = append(d.mesh.Any, beamLattice)
for _, a := range attrs {
if a.Name.Space != "" {
continue
Expand All @@ -42,16 +43,16 @@ func (d *beamLatticeDecoder) Start(attrs []spec.Attr) error {
if err != nil {
errs = specerr.Append(errs, specerr.NewParseAttrError(a.Name.Local, true))
}
beamLattice.Radius = float32(val)
d.beamLattice.Radius = float32(val)
case attrMinLength, attrPrecision: // lib3mf legacy
val, err := strconv.ParseFloat(string(a.Value), 32)
if err != nil {
errs = specerr.Append(errs, specerr.NewParseAttrError(a.Name.Local, true))
}
beamLattice.MinLength = float32(val)
d.beamLattice.MinLength = float32(val)
case attrClippingMode, attrClipping: // lib3mf legacy
var ok bool
beamLattice.ClipMode, ok = newClipMode(string(a.Value))
d.beamLattice.ClipMode, ok = newClipMode(string(a.Value))
if !ok {
errs = specerr.Append(errs, specerr.NewParseAttrError(a.Name.Local, false))
}
Expand All @@ -60,72 +61,67 @@ func (d *beamLatticeDecoder) Start(attrs []spec.Attr) error {
if err != nil {
errs = specerr.Append(errs, specerr.NewParseAttrError(a.Name.Local, false))
}
beamLattice.ClippingMeshID = uint32(val)
d.beamLattice.ClippingMeshID = uint32(val)
case attrRepresentationMesh:
val, err := strconv.ParseUint(string(a.Value), 10, 32)
if err != nil {
errs = specerr.Append(errs, specerr.NewParseAttrError(a.Name.Local, false))
}
beamLattice.RepresentationMeshID = uint32(val)
d.beamLattice.RepresentationMeshID = uint32(val)
case attrCap:
var ok bool
beamLattice.CapMode, ok = newCapMode(string(a.Value))
d.beamLattice.CapMode, ok = newCapMode(string(a.Value))
if !ok {
errs = specerr.Append(errs, specerr.NewParseAttrError(a.Name.Local, false))
}
}
}
if errs != nil {
return specerr.Wrap(errs, GetBeamLattice(d.mesh))
}
return nil
}

func (d *beamLatticeDecoder) Wrap(err error) error {
return specerr.Wrap(err, GetBeamLattice(d.mesh))
return errs
}

func (d *beamLatticeDecoder) Child(name xml.Name) (child spec.ElementDecoder) {
func (d *beamLatticeDecoder) Child(name xml.Name) (i int, child spec.ElementDecoder) {
if name.Space == Namespace {
if name.Local == attrBeams {
child = &beamsDecoder{mesh: d.mesh}
child = &beamsDecoder{beamLattice: &d.beamLattice}
i = -1
} else if name.Local == attrBeamSets {
child = &beamSetsDecoder{mesh: d.mesh}
child = &beamSetsDecoder{beamLattice: &d.beamLattice}
i = -1
}
}
return
}

type beamsDecoder struct {
baseDecoder
mesh *go3mf.Mesh
beamLattice *BeamLattice
beamDecoder beamDecoder
}

func (d *beamsDecoder) Start(_ []spec.Attr) error {
d.beamDecoder.mesh = d.mesh
func (d *beamsDecoder) Start(_ []spec.XMLAttr) error {
d.beamDecoder.beamLattice = d.beamLattice
return nil
}

func (d *beamsDecoder) Child(name xml.Name) (child spec.ElementDecoder) {
func (d *beamsDecoder) Child(name xml.Name) (i int, child spec.ElementDecoder) {
if name.Space == Namespace && name.Local == attrBeam {
child = &d.beamDecoder
i = len(d.beamDecoder.beamLattice.Beams.Beam)
}
return
}

type beamDecoder struct {
baseDecoder
mesh *go3mf.Mesh
beamLattice *BeamLattice
}

func (d *beamDecoder) Start(attrs []spec.Attr) error {
func (d *beamDecoder) Start(attrs []spec.XMLAttr) error {
var (
beam Beam
hasCap1, hasCap2 bool
errs error
)
beamLattice := GetBeamLattice(d.mesh)
for _, a := range attrs {
if a.Name.Space != "" {
continue
Expand Down Expand Up @@ -170,49 +166,46 @@ func (d *beamDecoder) Start(attrs []spec.Attr) error {
}
}
if beam.Radius[0] == 0 {
beam.Radius[0] = beamLattice.Radius
beam.Radius[0] = d.beamLattice.Radius
}
if beam.Radius[1] == 0 {
beam.Radius[1] = beam.Radius[0]
}
if !hasCap1 {
beam.CapMode[0] = beamLattice.CapMode
beam.CapMode[0] = d.beamLattice.CapMode
}
if !hasCap2 {
beam.CapMode[1] = beamLattice.CapMode
beam.CapMode[1] = d.beamLattice.CapMode
}
beamLattice.Beams = append(beamLattice.Beams, beam)
if errs != nil {
return specerr.WrapIndex(errs, beam, len(beamLattice.Beams)-1)
}
return nil
d.beamLattice.Beams.Beam = append(d.beamLattice.Beams.Beam, beam)
return errs
}

type beamSetsDecoder struct {
baseDecoder
mesh *go3mf.Mesh
beamLattice *BeamLattice
}

func (d *beamSetsDecoder) Child(name xml.Name) (child spec.ElementDecoder) {
func (d *beamSetsDecoder) Child(name xml.Name) (i int, child spec.ElementDecoder) {
if name.Space == Namespace && name.Local == attrBeamSet {
child = &beamSetDecoder{mesh: d.mesh}
child = &beamSetDecoder{beamLattice: d.beamLattice}
i = len(d.beamLattice.BeamSets.BeamSet)
}
return
}

type beamSetDecoder struct {
baseDecoder
mesh *go3mf.Mesh
beamLattice *BeamLattice
beamSet BeamSet
beamRefDecoder beamRefDecoder
}

func (d *beamSetDecoder) End() {
beamLattice := GetBeamLattice(d.mesh)
beamLattice.BeamSets = append(beamLattice.BeamSets, d.beamSet)
d.beamLattice.BeamSets.BeamSet = append(d.beamLattice.BeamSets.BeamSet, d.beamSet)
}

func (d *beamSetDecoder) Start(attrs []spec.Attr) error {
func (d *beamSetDecoder) Start(attrs []spec.XMLAttr) error {
d.beamRefDecoder.beamSet = &d.beamSet
for _, a := range attrs {
if a.Name.Space != "" {
Expand All @@ -228,13 +221,10 @@ func (d *beamSetDecoder) Start(attrs []spec.Attr) error {
return nil
}

func (d *beamSetDecoder) Wrap(err error) error {
return specerr.WrapIndex(err, &d.beamSet, len(GetBeamLattice(d.mesh).BeamSets))
}

func (d *beamSetDecoder) Child(name xml.Name) (child spec.ElementDecoder) {
func (d *beamSetDecoder) Child(name xml.Name) (i int, child spec.ElementDecoder) {
if name.Space == Namespace && name.Local == attrRef {
child = &d.beamRefDecoder
i = len(d.beamSet.Refs)
}
return
}
Expand All @@ -244,7 +234,7 @@ type beamRefDecoder struct {
beamSet *BeamSet
}

func (d *beamRefDecoder) Start(attrs []spec.Attr) error {
func (d *beamRefDecoder) Start(attrs []spec.XMLAttr) error {
var (
val uint64
errs error
Expand All @@ -260,14 +250,11 @@ func (d *beamRefDecoder) Start(attrs []spec.Attr) error {
}
}
d.beamSet.Refs = append(d.beamSet.Refs, uint32(val))
if errs != nil {
return specerr.WrapIndex(errs, uint32(0), len(d.beamSet.Refs)-1)
}
return nil
return errs
}

type baseDecoder struct {
}

func (d *baseDecoder) Start([]spec.Attr) error { return nil }
func (d *baseDecoder) End() {}
func (d *baseDecoder) Start([]spec.XMLAttr) error { return nil }
func (d *baseDecoder) End() {}
Loading

0 comments on commit 41d6751

Please sign in to comment.