Skip to content

Commit

Permalink
Check if content is empty to return empty []byte
Browse files Browse the repository at this point in the history
If the content is a empty file, then return []byte(resource) instead the error decoding from base64 an empty string.
  • Loading branch information
xhit committed Aug 5, 2020
1 parent 6f9faa2 commit 191dfbf
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
11 changes: 8 additions & 3 deletions embed.go
Expand Up @@ -17,14 +17,19 @@ func embedTemplate() string {
"\n" +
"package {{.Package}}\n" +
"\n" +
"\t{{ if not .EmptyContent }} " +
"import \"encoding/base64\"\n" +
"{{ end }}\n" +
"\n" +
"// {{.Name}}Resource is a generated function returning the Resource as a []byte.\n" +
"func {{.Name}}Resource() ([]byte, error) {\n" +
"\tvar resource = \"{{.Content}}\"\n" +
"\n" +
"\treturn base64.StdEncoding.DecodeString(resource)\n" +
"}\n" +
""
"\treturn {{ if .EmptyContent }} " +
"[]byte(resource), nil" +
"{{ else }}" +
"base64.StdEncoding.DecodeString(resource)" +
"{{ end }}\n" +
"}\n" + ""
return tmpl
}
5 changes: 3 additions & 2 deletions embed.tpl
Expand Up @@ -6,11 +6,12 @@

package {{.Package}}

import "encoding/base64"
{{ if not .EmptyContent }} import "encoding/base64"
{{ end }}

// {{.Name}}Resource is a generated function returning the Resource as a []byte.
func {{.Name}}Resource() ([]byte, error) {
var resource = "{{.Content}}"
return base64.StdEncoding.DecodeString(resource)
return {{ if .EmptyContent }} []byte(resource), nil{{ else }}base64.StdEncoding.DecodeString(resource){{ end }}
}
11 changes: 8 additions & 3 deletions mule.go
Expand Up @@ -185,9 +185,10 @@ func main() {
}

data := struct {
Package string
Name string
Content string
Package string
Name string
Content string
EmptyContent bool
}{
Package: pckg,
Name: strings.Split(functionname, ".")[0],
Expand All @@ -198,6 +199,10 @@ func main() {
os.Exit(1)
}

if len(data.Content) == 0 {
data.EmptyContent = true
}

outfile, err := os.Create(outfilename)
if err != nil {
fmt.Printf("Error creating target file '%s'\n%v\n", outfilename, err)
Expand Down

0 comments on commit 191dfbf

Please sign in to comment.