Refactor to allow for per-DCPS metadata in code generator.

This commit is contained in:
John Beisley 2014-06-07 20:29:11 +01:00
parent df61e019e6
commit cb64bf9e9e

View File

@ -64,9 +64,9 @@ func TaskSpecgen(t *tasking.T) {
} }
defer specArchive.Close() defer specArchive.Close()
dcpsCol := newDcpsCollection(map[string]string{ dcpsCol := newDcpsCollection(map[string]DCPSMetadata{
"Internet Gateway_1": "internetgateway1", "Internet Gateway_1": {"internetgateway1"},
"Internet Gateway_2": "internetgateway2", "Internet Gateway_2": {"internetgateway2"},
}) })
for _, f := range globFiles("standardizeddcps/*/*.zip", specArchive.Reader) { for _, f := range globFiles("standardizeddcps/*/*.zip", specArchive.Reader) {
dirName := strings.TrimPrefix(f.Name, "standardizeddcps/") dirName := strings.TrimPrefix(f.Name, "standardizeddcps/")
@ -91,46 +91,50 @@ func TaskSpecgen(t *tasking.T) {
for _, dcp := range dcpsCol.dcpsByAlias { for _, dcp := range dcpsCol.dcpsByAlias {
if err := dcp.writePackage(outDir, useGofmt); err != nil { if err := dcp.writePackage(outDir, useGofmt); err != nil {
log.Printf("Error writing package %q: %v", dcp.Name, err) log.Printf("Error writing package %q: %v", dcp.Metadata.Name, err)
} }
} }
} }
type DCPSMetadata struct {
Name string
}
type dcpsCollection struct { type dcpsCollection struct {
dcpsAliasByDir map[string]string dcpsMetadataByDir map[string]DCPSMetadata
dcpsByAlias map[string]*DCP dcpsByAlias map[string]*DCP
} }
func newDcpsCollection(dcpsAliasByDir map[string]string) *dcpsCollection { func newDcpsCollection(dcpsMetadataByDir map[string]DCPSMetadata) *dcpsCollection {
c := &dcpsCollection{ c := &dcpsCollection{
dcpsAliasByDir: dcpsAliasByDir, dcpsMetadataByDir: dcpsMetadataByDir,
dcpsByAlias: make(map[string]*DCP), dcpsByAlias: make(map[string]*DCP),
} }
for _, alias := range dcpsAliasByDir { for _, metadata := range dcpsMetadataByDir {
c.dcpsByAlias[alias] = newDCP(alias) c.dcpsByAlias[metadata.Name] = newDCP(metadata)
} }
return c return c
} }
func (c dcpsCollection) dcpsForDir(dirName string) *DCP { func (c *dcpsCollection) dcpsForDir(dirName string) *DCP {
alias, ok := c.dcpsAliasByDir[dirName] metadata, ok := c.dcpsMetadataByDir[dirName]
if !ok { if !ok {
return nil return nil
} }
return c.dcpsByAlias[alias] return c.dcpsByAlias[metadata.Name]
} }
// DCP collects together information about a UPnP Device Control Protocol. // DCP collects together information about a UPnP Device Control Protocol.
type DCP struct { type DCP struct {
Name string Metadata DCPSMetadata
DeviceTypes map[string]*URNParts DeviceTypes map[string]*URNParts
ServiceTypes map[string]*URNParts ServiceTypes map[string]*URNParts
Services []SCPDWithURN Services []SCPDWithURN
} }
func newDCP(name string) *DCP { func newDCP(metadata DCPSMetadata) *DCP {
return &DCP{ return &DCP{
Name: name, Metadata: metadata,
DeviceTypes: make(map[string]*URNParts), DeviceTypes: make(map[string]*URNParts),
ServiceTypes: make(map[string]*URNParts), ServiceTypes: make(map[string]*URNParts),
} }
@ -178,12 +182,12 @@ func (dcp *DCP) processDeviceFile(file *zip.File) {
} }
func (dcp *DCP) writePackage(outDir string, useGofmt bool) error { func (dcp *DCP) writePackage(outDir string, useGofmt bool) error {
packageDirname := filepath.Join(outDir, dcp.Name) packageDirname := filepath.Join(outDir, dcp.Metadata.Name)
err := os.MkdirAll(packageDirname, os.ModePerm) err := os.MkdirAll(packageDirname, os.ModePerm)
if err != nil && !os.IsExist(err) { if err != nil && !os.IsExist(err) {
return err return err
} }
packageFilename := filepath.Join(packageDirname, dcp.Name+".go") packageFilename := filepath.Join(packageDirname, dcp.Metadata.Name+".go")
packageFile, err := os.Create(packageFilename) packageFile, err := os.Create(packageFilename)
if err != nil { if err != nil {
return err return err
@ -415,9 +419,9 @@ func urnPartsFromSCPDFilename(filename string) (*URNParts, error) {
}, nil }, nil
} }
var packageTmpl = template.Must(template.New("package").Parse(`package {{.Name}} var packageTmpl = template.Must(template.New("package").Parse(`{{$name := .Metadata.Name}}
// Generated file - do not edit by hand. See README.md package {{$name}}
import ( import (
"time" "time"