Skip to content

Commit

Permalink
refactor remaining client unmarshal switches
Browse files Browse the repository at this point in the history
  • Loading branch information
jgramoll committed Aug 2, 2019
1 parent 0b28faf commit d05d39a
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 14 deletions.
15 changes: 15 additions & 0 deletions client/cps_scm_flow_definition.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package client

import "encoding/xml"

func init() {
jobDefinitionUnmarshalFunc["org.jenkinsci.plugins.workflow.cps.CpsScmFlowDefinition"] = unmarshalCpsScmFlowDefinition
}

type CpsScmFlowDefinition struct {
Class string `xml:"class,attr"`
Id string `xml:"id,attr,omitempty"`
Expand All @@ -23,3 +29,12 @@ func (d *CpsScmFlowDefinition) GetId() string {
func (d *CpsScmFlowDefinition) SetId(id string) {
d.Id = id
}

func unmarshalCpsScmFlowDefinition(d *xml.Decoder, start xml.StartElement) (JobDefinition, error) {
definition := NewCpsScmFlowDefinition()
err := d.DecodeElement(definition, &start)
if err != nil {
return nil, err
}
return definition, nil
}
15 changes: 15 additions & 0 deletions client/job_build_discarder_property_strategy_log_rotator.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package client

import "encoding/xml"

func init() {
jobBuildDiscarderPropertyStrategyUnmarshalFunc["hudson.tasks.LogRotator"] = unmarshalJobBuildDiscarderPropertyStrategyLogRotator
}

type JobBuildDiscarderPropertyStrategyLogRotator struct {
Id string `xml:"id,attr,omitempty"`
Class string `xml:"class,attr"`
Expand Down Expand Up @@ -28,3 +34,12 @@ func (s *JobBuildDiscarderPropertyStrategyLogRotator) GetId() string {
func (s *JobBuildDiscarderPropertyStrategyLogRotator) SetId(id string) {
s.Id = id
}

func unmarshalJobBuildDiscarderPropertyStrategyLogRotator(d *xml.Decoder, start xml.StartElement) (JobBuildDiscarderPropertyStrategy, error) {
strategy := NewJobBuildDiscarderPropertyStrategyLogRotator()
err := d.DecodeElement(strategy, &start)
if err != nil {
return nil, err
}
return strategy, nil
}
18 changes: 11 additions & 7 deletions client/job_build_discarder_property_strategy_xml.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import (
"log"
)

type jobBuildDiscarderPropertyStrategyUnmarshaler func(*xml.Decoder, xml.StartElement) (JobBuildDiscarderPropertyStrategy, error)

var jobBuildDiscarderPropertyStrategyUnmarshalFunc = map[string]jobBuildDiscarderPropertyStrategyUnmarshaler{}

type JobBuildDiscarderPropertyStrategyXml struct {
Item JobBuildDiscarderPropertyStrategy `xml:",any"`
}
Expand All @@ -19,15 +23,15 @@ func (strategy *JobBuildDiscarderPropertyStrategyXml) MarshalXML(e *xml.Encoder,

func (strategy *JobBuildDiscarderPropertyStrategyXml) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
for _, attr := range start.Attr {
// TODO use map
switch attr.Name.Local {
case "class":
switch attr.Value {
default:
case "hudson.tasks.LogRotator":
newStrategy := NewJobBuildDiscarderPropertyStrategyLogRotator()
strategy.Item = newStrategy
return d.DecodeElement(newStrategy, &start)
if unmarshalXML, ok := jobBuildDiscarderPropertyStrategyUnmarshalFunc[attr.Value]; ok {
newStrategyItem, err := unmarshalXML(d, start)
if err != nil {
return err
}
strategy.Item = newStrategyItem
return nil
}
}
}
Expand Down
18 changes: 11 additions & 7 deletions client/job_definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import (
"log"
)

type jobDefinitionUnmarshaler func(*xml.Decoder, xml.StartElement) (JobDefinition, error)

var jobDefinitionUnmarshalFunc = map[string]jobDefinitionUnmarshaler{}

type JobDefinitionXml struct {
Item JobDefinition
}
Expand All @@ -20,15 +24,15 @@ func (jobDefinition *JobDefinitionXml) MarshalXML(e *xml.Encoder, start xml.Star

func (jobDefinition *JobDefinitionXml) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
for _, attr := range start.Attr {
// TODO use map
switch attr.Name.Local {
case "class":
switch attr.Value {
default:
case "org.jenkinsci.plugins.workflow.cps.CpsScmFlowDefinition":
newDef := NewCpsScmFlowDefinition()
jobDefinition.Item = newDef
return d.DecodeElement(newDef, &start)
if unmarshalXML, ok := jobDefinitionUnmarshalFunc[attr.Value]; ok {
newDefItem, err := unmarshalXML(d, start)
if err != nil {
return err
}
jobDefinition.Item = newDefItem
return nil
}
}
}
Expand Down

0 comments on commit d05d39a

Please sign in to comment.