Skip to content

Commit

Permalink
Reorder items in generated source files.
Browse files Browse the repository at this point in the history
Ideally they would always have been sorted as such, but this seems more
critical in migrating from one service definition to another where the ordering might
change.
  • Loading branch information
huin committed Mar 13, 2022
1 parent 730ab65 commit ca81a64
Show file tree
Hide file tree
Showing 8 changed files with 7,156 additions and 7,116 deletions.
8 changes: 4 additions & 4 deletions cmd/goupnpdcpgen/codetemplate.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,16 @@ import (
var _ time.Time
// Device URNs:
const ({{range .DeviceTypes}}
const ({{range .OrderedDeviceTypes}}
{{.Const}} = "{{.URN}}"{{end}}
)
// Service URNs:
const ({{range .ServiceTypes}}
const ({{range .OrderedServiceTypes}}
{{.Const}} = "{{.URN}}"{{end}}
)
{{range .Services}}
{{range .OrderedServices}}
{{$srv := .}}
{{$srvIdent := printf "%s%s" .Name .Version}}
Expand Down Expand Up @@ -102,7 +102,7 @@ func new{{$srvIdent}}ClientsFromGenericClients(genericClients []goupnp.ServiceCl
return clients
}
{{range .SCPD.Actions}}{{/* loops over *SCPDWithURN values */}}
{{range .SCPD.OrderedActions}}{{/* loops over *SCPDWithURN values */}}
{{$winargs := $srv.WrapArguments .InputArguments}}
{{$woutargs := $srv.WrapArguments .OutputArguments}}
Expand Down
29 changes: 29 additions & 0 deletions cmd/goupnpdcpgen/dcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"io"
"os"
"sort"
"strings"

"github.com/huin/goupnp"
Expand Down Expand Up @@ -85,13 +86,41 @@ func (dcp *DCP) writeCode(outFile string, useGofmt bool) error {
return err
}
}

if err = packageTmpl.Execute(output, dcp); err != nil {
output.Close()
return err
}
return output.Close()
}

func (dcp *DCP) OrderedServices() []SCPDWithURN {
services := append([]SCPDWithURN{}, dcp.Services...)
sort.SliceStable(services, func(i, j int) bool {
return services[i].URNParts.URN < services[j].URNParts.URN
})
return services
}

func orderedURNParts(urnMap map[string]*URNParts) []*URNParts {
urns := make([]*URNParts, 0, len(urnMap))
for _, urn := range urnMap {
urns = append(urns, urn)
}
sort.SliceStable(urns, func(i, j int) bool {
return urns[i].URN < urns[j].URN
})
return urns
}

func (dcp *DCP) OrderedDeviceTypes() []*URNParts {
return orderedURNParts(dcp.DeviceTypes)
}

func (dcp *DCP) OrderedServiceTypes() []*URNParts {
return orderedURNParts(dcp.ServiceTypes)
}

func (dcp *DCP) processSCPDFile(file *zip.File) error {
scpd := new(scpd.SCPD)
if err := unmarshalXmlFile(file, scpd); err != nil {
Expand Down
Loading

0 comments on commit ca81a64

Please sign in to comment.