Skip to content

Commit

Permalink
Remove pkger from non-static template processing
Browse files Browse the repository at this point in the history
  • Loading branch information
1414C committed Apr 5, 2024
1 parent 24aa6c0 commit bde8743
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 73 deletions.
20 changes: 11 additions & 9 deletions gen/configgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package gen

import (
"database/sql"
"embed"
"fmt"
"log"
"os"
"strings"

// "text/template"

// needed
Expand Down Expand Up @@ -146,10 +148,10 @@ func (cfg Config) IsLastServiceActivationRec(name string) bool {

// GenerateAppConf generates the default application configuration
// source file appconf.go.
func (cfg *Config) GenerateAppConf(dstDir string) (fName string, err error) {
func (cfg *Config) GenerateAppConf(dstDir string, ef embed.FS) (fName string, err error) {

// get a *Template
at, err := prepareTemplate("/templates/appconf.gotmpl")
at, err := prepareTemplate("templates/appconf.gotmpl", ef)
if err != nil {
return "", err
}
Expand Down Expand Up @@ -189,10 +191,10 @@ func (cfg *Config) GenerateAppConf(dstDir string) (fName string, err error) {

// GenerateSampleConfig creates a sample .config.json file
// to hold the production application config.
func (cfg *Config) GenerateSampleConfig(dstDir string) error {
func (cfg *Config) GenerateSampleConfig(dstDir string, ef embed.FS) error {

// get a *Template
at, err := prepareTemplate("/templates/config.json.gotmpl")
at, err := prepareTemplate("templates/config.json.gotmpl", ef)
if err != nil {
return err
}
Expand Down Expand Up @@ -246,10 +248,10 @@ func (cfg *Config) GenerateSampleConfig(dstDir string) error {

// GenerateSampleDockerConfig creates a sample .config.json file
// to hold the production application config.
func (cfg *Config) GenerateSampleDockerConfig(dstDir string) error {
func (cfg *Config) GenerateSampleDockerConfig(dstDir string, ef embed.FS) error {

// get a *Template
at, err := prepareTemplate("/static/docker/docker_config.json.gotmpl")
at, err := prepareTemplate("static/docker/docker_config.json.gotmpl", ef)
if err != nil {
return err
}
Expand Down Expand Up @@ -288,7 +290,7 @@ func (cfg *Config) GenerateSampleDockerConfig(dstDir string) error {
log.Println("generated:", tfDir)

// get a *Template for a sample Dockerfile
at, err = prepareTemplate("/static/docker/Dockerfile.gotmpl")
at, err = prepareTemplate("static/docker/Dockerfile.gotmpl", ef)
if err != nil {
return err
}
Expand Down Expand Up @@ -322,7 +324,7 @@ func (cfg *Config) GenerateSampleDockerConfig(dstDir string) error {
// docker image. This script gets pulled into the docker image during
// the docker build process.
// generate a sample Dockerfile
at, err = prepareTemplate("/static/docker/docker-entrypoint.sh.gotmpl")
at, err = prepareTemplate("static/docker/docker-entrypoint.sh.gotmpl", ef)
if err != nil {
return err
}
Expand Down Expand Up @@ -354,7 +356,7 @@ func (cfg *Config) GenerateSampleDockerConfig(dstDir string) error {
log.Println("generated:", tfDir)

// generate a readme.md
at, err = prepareTemplate("/static/docker/docker_readme.md.gotmpl")
at, err = prepareTemplate("static/docker/docker_readme.md.gotmpl", ef)
if err != nil {
return err
}
Expand Down
105 changes: 57 additions & 48 deletions gen/generator.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gen

import (
"embed"
"fmt"
"log"
"os"
Expand Down Expand Up @@ -116,10 +117,10 @@ const (
// with the model.gotmpl text/template. Returns the fully-qualified file-name / error.
// pkger is used to bundle the .gotmpl files into the binary. Pkger implements the File interface, so the
// file handling is a little more pedantic than it would be with ioutil.
func (ent *Entity) CreateModelFile(tDir string) (fName string, err error) {
func (ent *Entity) CreateModelFile(tDir string, ef embed.FS) (fName string, err error) {

// new part
mt, err := prepareTemplate("/templates/model.gotmpl")
mt, err := prepareTemplate("templates/model.gotmpl", ef)
if err != nil {
return "", err
}
Expand Down Expand Up @@ -163,10 +164,10 @@ func (ent *Entity) CreateModelFile(tDir string) (fName string, err error) {
// using the user-defined model.json file in conjunction
// with the controller.gotmpl text/template. Returns the fully-qualified
// file-name / error.
func (ent *Entity) CreateControllerFile(tDir string) (fName string, err error) {
func (ent *Entity) CreateControllerFile(tDir string, ef embed.FS) (fName string, err error) {

// new part
ct, err := prepareTemplate("/templates/controller.gotmpl")
ct, err := prepareTemplate("templates/controller.gotmpl", ef)
if err != nil {
return "", err
}
Expand Down Expand Up @@ -213,10 +214,10 @@ func (ent *Entity) CreateControllerFile(tDir string) (fName string, err error) {
// facilitate the validation of the ToEntity field used in the
// foreign-key definition.
// Returns the fully-qualified file-name / error.
func (ent *Entity) CreateControllerRelationsFile(tDir string, entities []Entity) (fName string, err error) {
func (ent *Entity) CreateControllerRelationsFile(tDir string, entities []Entity, ef embed.FS) (fName string, err error) {

// new part
ctr, err := prepareTemplate("/templates/controller_relations.gotmpl")
ctr, err := prepareTemplate("templates/controller_relations.gotmpl", ef)
if err != nil {
return "", err
}
Expand Down Expand Up @@ -260,10 +261,10 @@ func (ent *Entity) CreateControllerRelationsFile(tDir string, entities []Entity)
// point implementation file for the Entity if the 'gen_controller' element
// is set to true in the user-defined model.json file.
// Returns the fully-qualified file-name / error.
func (ent *Entity) CreateControllerExtensionPointsFile(tDir string) (fName string, err error) {
func (ent *Entity) CreateControllerExtensionPointsFile(tDir string, ef embed.FS) (fName string, err error) {

// new part
ctx, err := prepareTemplate("/templates/controller_ext.gotmpl")
ctx, err := prepareTemplate("templates/controller_ext.gotmpl", ef)
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -317,10 +318,10 @@ func (ent *Entity) CreateControllerExtensionPointsFile(tDir string) (fName strin
// implementation file for the Entity if the 'gen_controller' element
// is set to true in the user-defined model.json file.
// Returns the fully-qualified file-name / error.
func (ent *Entity) CreateModelExtensionPointsFile(tDir string) (fName string, err error) {
func (ent *Entity) CreateModelExtensionPointsFile(tDir string, ef embed.FS) (fName string, err error) {

// new part
mtx, err := prepareTemplate("/templates/model_ext.gotmpl")
mtx, err := prepareTemplate("templates/model_ext.gotmpl", ef)
if err != nil {
return "", err
}
Expand Down Expand Up @@ -369,9 +370,9 @@ func (ent *Entity) CreateModelExtensionPointsFile(tDir string) (fName string, er
return tfDir, nil
}

//=============================================================================================
// =============================================================================================
// static generation functions
//=============================================================================================
// =============================================================================================
// GenerateStaticTemplates reads the ./static folder and uses Glob
// to execute each template in-turn. Returns the fully-qualified
// file-names or an error. Processes .gotmpl files into .go files.
Expand Down Expand Up @@ -452,9 +453,9 @@ func (s *Static) GenerateStaticTemplates() (fNames []string, err error) {
return fNames, nil
}

func (s *Static) GenerateGoMod() error {
func (s *Static) GenerateGoMod(ef embed.FS) error {
// func (s *Static) GenerateGoMod(tDir string) (fName string, err error) {
st, err := prepareTemplate("/static/modules/go.mod.gotmpl")
st, err := prepareTemplate("static/modules/go.mod.gotmpl", ef)
if err != nil {
return err
}
Expand Down Expand Up @@ -1159,24 +1160,25 @@ func (s *Static) GetConcatenatedEntities() string {
// in from the template. The intent is to create
// a body which can be edited by the developer in
// order to add more meaningful data.
// * string types will be assigned: "string_value"
// * float64 type will be assigned an incrementing
// float value starting at 9.91
// * int types will be assigned an incrementing int
// value starting at 10.
// * uint types will be assigned an incrementing uint
// value starting at 10.
// - string types will be assigned: "string_value"
// - float64 type will be assigned an incrementing
// float value starting at 9.91
// - int types will be assigned an incrementing int
// value starting at 10.
// - uint types will be assigned an incrementing uint
// value starting at 10.
//
// `{"name":"test_product",
// "height":55.5,
// "cost":66.6,
// "supplier":"Ace Hardware",
// "weight":88.8,
// "length":44.4,
// "width":33.3,
// "name":"TEST_PRODUCT",
// "description":"a nice test product",
// "uom":"EA"}`
//
// "height":55.5,
// "cost":66.6,
// "supplier":"Ace Hardware",
// "weight":88.8,
// "length":44.4,
// "width":33.3,
// "name":"TEST_PRODUCT",
// "description":"a nice test product",
// "uom":"EA"}`
func (ent *Entity) BuildTestPostJSON(isUpdate bool) string {

var result string
Expand Down Expand Up @@ -1347,9 +1349,9 @@ func getTestValue(isUpdate bool, dataType string) interface{} {
return nil
}

//=============================================================================================
// =============================================================================================
// local functions
//=============================================================================================
// =============================================================================================
func cleanString(s string) string {
s = strings.TrimPrefix(s, "\"")
s = strings.TrimSuffix(s, "\"")
Expand All @@ -1365,31 +1367,38 @@ func superCleanString(s string) string {

// prepareTemplate is used to read a template source from the pkger
// or local location, create a *Template and return it to the caller.
func prepareTemplate(templateName string) (*template.Template, error) {
func prepareTemplate(templateName string, ef embed.FS) (*template.Template, error) {
// stat for .gotmpl file size
fi, err := pkger.Stat(templateName)
if err != nil {
log.Printf("Stat: %v\n", err)
return nil, err
}
//fi, err := pkger.Stat(templateName)
//if err != nil {
// log.Printf("Stat: %v\n", err)
// return nil, err
//}

tf, err := pkger.Open(templateName)
tf, err := ef.ReadFile(templateName)
if err != nil {
log.Printf("Open: %v\n", err)
log.Printf("ReadFile: %v\n", err)
return nil, err
}
defer tf.Close()

// tf, err := ef.Open(templateName)
// if err != nil {
// log.Printf("Open: %v\n", err)
// return nil, err
// }
// defer tf.Close()

// read the template source from pkger
buf := make([]byte, fi.Size())
_, err = tf.Read(buf)
if err != nil {
log.Printf("Unable to read template %s\n", templateName)
return nil, err
}
// buf := make([]byte, fi.Size())
// _, err = tf.Read(buf)
// if err != nil {
// log.Printf("Unable to read template %s\n", templateName)
// return nil, err
// }

// create the template
t := template.Must(template.New("Entity model template").Parse(string(buf)))
// t := template.Must(template.New("Entity model template").Parse(string(buf)))
t := template.Must(template.New("Entity model template").Parse(string(tf)))
if t == nil {
log.Printf("Parse: %v\n", err)
return nil, err
Expand Down
8 changes: 4 additions & 4 deletions gen/readmodel.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package gen
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"reflect"
"strconv"
"strings"
Expand Down Expand Up @@ -31,7 +31,7 @@ func ReadModelFile(mf string) ([]Entity, error) {
var idMap map[string]json.RawMessage

// read the models.json file as []byte
raw, err := ioutil.ReadFile(mf)
raw, err := os.ReadFile(mf)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -427,7 +427,7 @@ func completeRelations(entities []Entity) error {
entities[i].Relations[j].RefKeyOptional = !f.Required
}
}
if bValid == false {
if !bValid {
return fmt.Errorf("RefKey %s is not a valid field-name in %s relationship %s", v.RefKey, entities[i].Header.Name, v.RelName)
}
}
Expand Down Expand Up @@ -458,7 +458,7 @@ func completeRelations(entities []Entity) error {
entities[i].Relations[j].ForeignPKOptional = !f.Required
}
}
if bValid == false {
if !bValid {
return fmt.Errorf("ForeignPK %s is not a valid field-name in %s relationship %s", v.ForeignPK, entities[i].Header.Name, v.RelName)
}
}
Expand Down
Loading

0 comments on commit bde8743

Please sign in to comment.