Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check if content is empty to return empty []byte #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions embed.go
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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