diff options
author | bnewbold <bnewbold@robocracy.org> | 2012-04-18 23:56:43 -0400 |
---|---|---|
committer | bnewbold <bnewbold@robocracy.org> | 2012-04-18 23:56:55 -0400 |
commit | 5c860476dad6d007bd3eb6cec105be20cef7b31a (patch) | |
tree | 90bf33dea0f511547ee8660602574313c484686e | |
parent | 3b03a7a42ca8d71f8a6c68133cadb6faf358f712 (diff) | |
download | bommom-5c860476dad6d007bd3eb6cec105be20cef7b31a.tar.gz bommom-5c860476dad6d007bd3eb6cec105be20cef7b31a.zip |
container for marshaling
-rw-r--r-- | bommom.go | 7 | ||||
-rw-r--r-- | core.go | 4 | ||||
-rw-r--r-- | formats.go | 67 |
3 files changed, 39 insertions, 39 deletions
@@ -258,9 +258,14 @@ func convertCmd() { } if inFormat == "csv" && bs == nil { // TODO: from inname? if ShortName? - bs = &BomMeta{Name: "untitled", Owner: anonUser.name} + bs = &BomMeta{Name: "untitled", + Owner: anonUser.name} + b.Version = "unversioned" } + b.Created = time.Now() + b.Progeny = "File import from " + inFname + " (" + inFormat + ")" + if err := bs.Validate(); err != nil { log.Fatal("loaded bommeta not valid: " + err.Error()) } @@ -41,7 +41,7 @@ type BomMeta struct { Owner string `json:"owner_name"` Description string `json:"description"` HeadVersion string `json:"head_version"` - Homepage *Url `json:"homepage_url"` + Homepage Url `json:"homepage_url"` IsPublicView bool `json:"is_publicview",omitempty` IsPublicEdit bool `json:"is_publicedit",omitempty` } @@ -81,7 +81,7 @@ func (b *Bom) Validate() error { if !isShortName(b.Version) { return Error("version not a ShortName: \"" + b.Version + "\"") } - if &b.Created == nil { + if b.Created.IsZero() { return Error("created timestamp not defined") } return nil @@ -12,14 +12,28 @@ import ( "strings" ) +// This compound container struct is useful for serializing to XML and JSON +type BomContainer struct { + BomMetadata *BomMeta `json:"metadata"` + Bom *Bom `json:"bom"` +} + // --------------------- text (CLI only ) ----------------------- func DumpBomAsText(bs *BomMeta, b *Bom, out io.Writer) { fmt.Fprintln(out) - fmt.Fprintf(out, "%s (version %s, created %s)\n", bs.Name, b.Version, b.Created) - fmt.Fprintf(out, "Creator: %s\n", bs.Owner) + fmt.Fprintf(out, "Name:\t\t%s\n", bs.Name) + fmt.Fprintf(out, "Version:\t%s\n", b.Version) + fmt.Fprintf(out, "Creator:\t%s\n", bs.Owner) + fmt.Fprintf(out, "Timestamp:\t%s\n", b.Created) + if bs.Homepage != "" { + fmt.Fprintf(out, "Homepage:\t%s\n", bs.Homepage) + } + if b.Progeny != "" { + fmt.Fprintf(out, "Source:\t\t%s\n", b.Progeny) + } if bs.Description != "" { - fmt.Fprintf(out, "Description: %s\n", bs.Description) + fmt.Fprintf(out, "Description:\t%s\n", bs.Description) } fmt.Println() // "by line item" @@ -83,65 +97,46 @@ func LoadBomFromCSV(out io.Writer) (*Bom, error) { func DumpBomAsJSON(bs *BomMeta, b *Bom, out io.Writer) { - obj := map[string]interface{}{ - "bom_meta": bs, - "bom": b, - } + container := &BomContainer{BomMetadata: bs, Bom: b} enc := json.NewEncoder(out) - if err := enc.Encode(&obj); err != nil { + if err := enc.Encode(&container); err != nil { log.Fatal(err) } } func LoadBomFromJSON(input io.Reader) (*BomMeta, *Bom, error) { - bs := &BomMeta{} - b := &Bom{} - - obj := map[string]interface{}{ - "bom_meta": &bs, - "bom": &b, - } - - fmt.Println(obj) + container := &BomContainer{} enc := json.NewDecoder(input) - if err := enc.Decode(&obj); err != nil { + if err := enc.Decode(&container); err != nil { log.Fatal(err) } - if &bs == nil || &b == nil { - log.Fatal("didn't load successfully") - } - fmt.Println(bs) - fmt.Println(b) - return bs, b, nil + return container.BomMetadata, container.Bom, nil } // --------------------- XML ----------------------- func DumpBomAsXML(bs *BomMeta, b *Bom, out io.Writer) { + container := &BomContainer{BomMetadata: bs, Bom: b} enc := xml.NewEncoder(out) - if err := enc.Encode(bs); err != nil { - log.Fatal(err) - } - if err := enc.Encode(b); err != nil { + + // generic XML header + io.WriteString(out, xml.Header) + + if err := enc.Encode(container); err != nil { log.Fatal(err) } } func LoadBomFromXML(input io.Reader) (*BomMeta, *Bom, error) { - bs := BomMeta{} - b := Bom{} - + container := &BomContainer{} enc := xml.NewDecoder(input) - if err := enc.Decode(&bs); err != nil { - log.Fatal(err) - } - if err := enc.Decode(&b); err != nil { + if err := enc.Decode(&container); err != nil { log.Fatal(err) } - return &bs, &b, nil + return container.BomMetadata, container.Bom, nil } |