Skip to content

Commit

Permalink
refactoring for xml output
Browse files Browse the repository at this point in the history
  • Loading branch information
hhatto committed Mar 2, 2019
1 parent 95a5d74 commit 8f522b2
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 24 deletions.
28 changes: 4 additions & 24 deletions cmd/gocloc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@ import (

// OutputTypeDefault is cloc's text output format for --output-type option
const OutputTypeDefault string = "default"

// OutputTypeClocXML is Cloc's XML output format for --output-type option
const OutputTypeClocXML string = "cloc-xml"

// OutputTypeSloccount is Sloccount output format for --output-type option
const OutputTypeSloccount string = "sloccount"

// OutputTypeJSON is JSON output format for --output-type option
const OutputTypeJSON string = "json"

Expand Down Expand Up @@ -175,30 +178,7 @@ func main() {

switch opts.OutputType {
case OutputTypeClocXML:
var langs []gocloc.ClocLanguage
for _, language := range sortedLanguages {
c := gocloc.ClocLanguage{
Name: language.Name,
FilesCount: int32(len(language.Files)),
Code: language.Code,
Comments: language.Comments,
Blanks: language.Blanks,
}
langs = append(langs, c)
}
t := gocloc.XMLTotalLanguages{
Code: total.Code,
Comment: total.Comments,
Blank: total.Blanks,
SumFiles: total.Total,
}
f := &gocloc.XMLResultLanguages{
Languages: langs,
Total: t,
}
xmlResult := gocloc.XMLResult{
XMLLanguages: f,
}
xmlResult := gocloc.NewXMLResultFromCloc(total, sortedLanguages, gocloc.XMLResultWithLangs)
xmlResult.Encode()
default:
for _, language := range sortedLanguages {
Expand Down
35 changes: 35 additions & 0 deletions xml.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ import (
"fmt"
)

type XMLResultType int8

const (
XMLResultWithLangs XMLResultType = iota
XMLResultWithFiles
)

type XMLTotalLanguages struct {
SumFiles int32 `xml:"sum_files,attr"`
Code int32 `xml:"code,attr"`
Expand Down Expand Up @@ -38,3 +45,31 @@ func (x *XMLResult) Encode() {
fmt.Println(string(output))
}
}

func NewXMLResultFromCloc(total *Language, sortedLanguages Languages, option XMLResultType) *XMLResult {
var langs []ClocLanguage
for _, language := range sortedLanguages {
c := ClocLanguage{
Name: language.Name,
FilesCount: int32(len(language.Files)),
Code: language.Code,
Comments: language.Comments,
Blanks: language.Blanks,
}
langs = append(langs, c)
}
t := XMLTotalLanguages{
Code: total.Code,
Comment: total.Comments,
Blank: total.Blanks,
SumFiles: total.Total,
}
f := &XMLResultLanguages{
Languages: langs,
Total: t,
}

return &XMLResult{
XMLLanguages: f,
}
}

0 comments on commit 8f522b2

Please sign in to comment.