v0.0.402 add PackageName() and TypeName() to enums_codegen
All checks were successful
Build Docker and Deploy / Run goext test-suite (push) Successful in 2m12s
All checks were successful
Build Docker and Deploy / Run goext test-suite (push) Successful in 2m12s
This commit is contained in:
parent
ed53f297bd
commit
409d6e108d
4
TODO.md
4
TODO.md
@ -2,6 +2,8 @@
|
||||
|
||||
- cronext
|
||||
|
||||
- rfctime.DateOnly
|
||||
- rfctime.HMSTimeOnly
|
||||
- rfctime.NanoTimeOnly
|
||||
|
||||
- remove sqlx dependency from sq (unmaintained, and mostly superseeded by our own stuff?)
|
||||
- Move DBLogger and DBPreprocessor to sq
|
@ -26,6 +26,10 @@ type CSIDDef struct {
|
||||
Prefix string
|
||||
}
|
||||
|
||||
type CSIDGenOptions struct {
|
||||
DebugOutput *bool
|
||||
}
|
||||
|
||||
var rexCSIDPackage = rext.W(regexp.MustCompile(`^package\s+(?P<name>[A-Za-z0-9_]+)\s*$`))
|
||||
|
||||
var rexCSIDDef = rext.W(regexp.MustCompile(`^\s*type\s+(?P<name>[A-Za-z0-9_]+)\s+string\s*//\s*(@csid:type)\s+\[(?P<prefix>[A-Z0-9]{3})].*$`))
|
||||
@ -35,7 +39,9 @@ var rexCSIDChecksumConst = rext.W(regexp.MustCompile(`const ChecksumCharsetIDGen
|
||||
//go:embed csid-generate.template
|
||||
var templateCSIDGenerateText string
|
||||
|
||||
func GenerateCharsetIDSpecs(sourceDir string, destFile string) error {
|
||||
func GenerateCharsetIDSpecs(sourceDir string, destFile string, opt CSIDGenOptions) error {
|
||||
|
||||
debugOutput := langext.Coalesce(opt.DebugOutput, false)
|
||||
|
||||
files, err := os.ReadDir(sourceDir)
|
||||
if err != nil {
|
||||
@ -81,13 +87,18 @@ func GenerateCharsetIDSpecs(sourceDir string, destFile string) error {
|
||||
pkgname := ""
|
||||
|
||||
for _, f := range files {
|
||||
if debugOutput {
|
||||
fmt.Printf("========= %s =========\n\n", f.Name())
|
||||
fileIDs, pn, err := processCSIDFile(sourceDir, path.Join(sourceDir, f.Name()))
|
||||
}
|
||||
|
||||
fileIDs, pn, err := processCSIDFile(sourceDir, path.Join(sourceDir, f.Name()), debugOutput)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if debugOutput {
|
||||
fmt.Printf("\n")
|
||||
}
|
||||
|
||||
allIDs = append(allIDs, fileIDs...)
|
||||
|
||||
@ -113,7 +124,7 @@ func GenerateCharsetIDSpecs(sourceDir string, destFile string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func processCSIDFile(basedir string, fn string) ([]CSIDDef, string, error) {
|
||||
func processCSIDFile(basedir string, fn string, debugOutput bool) ([]CSIDDef, string, error) {
|
||||
file, err := os.Open(fn)
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
@ -155,7 +166,11 @@ func processCSIDFile(basedir string, fn string) ([]CSIDDef, string, error) {
|
||||
Name: match.GroupByName("name").Value(),
|
||||
Prefix: match.GroupByName("prefix").Value(),
|
||||
}
|
||||
|
||||
if debugOutput {
|
||||
fmt.Printf("Found ID definition { '%s' }\n", def.Name)
|
||||
}
|
||||
|
||||
ids = append(ids, def)
|
||||
}
|
||||
}
|
||||
|
@ -34,10 +34,10 @@ func TestGenerateCSIDSpecs(t *testing.T) {
|
||||
_, err = cmdext.Runner("tar").Arg("-xvzf").Arg(tmpFile).Arg("-C").Arg(tmpDir).FailOnExitCode().FailOnTimeout().Timeout(time.Minute).Run()
|
||||
tst.AssertNoErr(t, err)
|
||||
|
||||
err = GenerateCharsetIDSpecs(tmpDir, tmpDir+"/csid_gen.go")
|
||||
err = GenerateCharsetIDSpecs(tmpDir, tmpDir+"/csid_gen.go", CSIDGenOptions{DebugOutput: langext.PTrue})
|
||||
tst.AssertNoErr(t, err)
|
||||
|
||||
err = GenerateCharsetIDSpecs(tmpDir, tmpDir+"/csid_gen.go")
|
||||
err = GenerateCharsetIDSpecs(tmpDir, tmpDir+"/csid_gen.go", CSIDGenOptions{DebugOutput: langext.PTrue})
|
||||
tst.AssertNoErr(t, err)
|
||||
|
||||
fmt.Println()
|
||||
|
@ -37,6 +37,11 @@ type EnumDef struct {
|
||||
Values []EnumDefVal
|
||||
}
|
||||
|
||||
type EnumGenOptions struct {
|
||||
DebugOutput *bool
|
||||
GoFormat *bool
|
||||
}
|
||||
|
||||
var rexEnumPackage = rext.W(regexp.MustCompile(`^package\s+(?P<name>[A-Za-z0-9_]+)\s*$`))
|
||||
|
||||
var rexEnumDef = rext.W(regexp.MustCompile(`^\s*type\s+(?P<name>[A-Za-z0-9_]+)\s+(?P<type>[A-Za-z0-9_]+)\s*//\s*(@enum:type).*$`))
|
||||
@ -48,7 +53,7 @@ var rexEnumChecksumConst = rext.W(regexp.MustCompile(`const ChecksumEnumGenerato
|
||||
//go:embed enum-generate.template
|
||||
var templateEnumGenerateText string
|
||||
|
||||
func GenerateEnumSpecs(sourceDir string, destFile string) error {
|
||||
func GenerateEnumSpecs(sourceDir string, destFile string, opt EnumGenOptions) error {
|
||||
|
||||
oldChecksum := "N/A"
|
||||
if _, err := os.Stat(destFile); !os.IsNotExist(err) {
|
||||
@ -61,7 +66,7 @@ func GenerateEnumSpecs(sourceDir string, destFile string) error {
|
||||
}
|
||||
}
|
||||
|
||||
gocode, _, changed, err := _generateEnumSpecs(sourceDir, destFile, oldChecksum, true)
|
||||
gocode, _, changed, err := _generateEnumSpecs(sourceDir, destFile, oldChecksum, langext.Coalesce(opt.GoFormat, true), langext.Coalesce(opt.DebugOutput, false))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -78,7 +83,7 @@ func GenerateEnumSpecs(sourceDir string, destFile string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func _generateEnumSpecs(sourceDir string, destFile string, oldChecksum string, gofmt bool) (string, string, bool, error) {
|
||||
func _generateEnumSpecs(sourceDir string, destFile string, oldChecksum string, gofmt bool, debugOutput bool) (string, string, bool, error) {
|
||||
|
||||
files, err := os.ReadDir(sourceDir)
|
||||
if err != nil {
|
||||
@ -113,13 +118,18 @@ func _generateEnumSpecs(sourceDir string, destFile string, oldChecksum string, g
|
||||
pkgname := ""
|
||||
|
||||
for _, f := range files {
|
||||
if debugOutput {
|
||||
fmt.Printf("========= %s =========\n\n", f.Name())
|
||||
fileEnums, pn, err := processEnumFile(sourceDir, path.Join(sourceDir, f.Name()))
|
||||
}
|
||||
|
||||
fileEnums, pn, err := processEnumFile(sourceDir, path.Join(sourceDir, f.Name()), debugOutput)
|
||||
if err != nil {
|
||||
return "", "", false, err
|
||||
}
|
||||
|
||||
if debugOutput {
|
||||
fmt.Printf("\n")
|
||||
}
|
||||
|
||||
allEnums = append(allEnums, fileEnums...)
|
||||
|
||||
@ -146,7 +156,7 @@ func _generateEnumSpecs(sourceDir string, destFile string, oldChecksum string, g
|
||||
return string(fdata), newChecksum, true, nil
|
||||
}
|
||||
|
||||
func processEnumFile(basedir string, fn string) ([]EnumDef, string, error) {
|
||||
func processEnumFile(basedir string, fn string, debugOutput bool) ([]EnumDef, string, error) {
|
||||
file, err := os.Open(fn)
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
@ -190,8 +200,11 @@ func processEnumFile(basedir string, fn string) ([]EnumDef, string, error) {
|
||||
Values: make([]EnumDefVal, 0),
|
||||
}
|
||||
enums = append(enums, def)
|
||||
|
||||
if debugOutput {
|
||||
fmt.Printf("Found enum definition { '%s' -> '%s' }\n", def.EnumTypeName, def.Type)
|
||||
}
|
||||
}
|
||||
|
||||
if match, ok := rexEnumValueDef.MatchFirst(line); ok {
|
||||
typename := match.GroupByName("type").Value()
|
||||
@ -230,19 +243,24 @@ func processEnumFile(basedir string, fn string) ([]EnumDef, string, error) {
|
||||
if v.EnumTypeName == typename {
|
||||
enums[i].Values = append(enums[i].Values, def)
|
||||
found = true
|
||||
|
||||
if debugOutput {
|
||||
if def.Description != nil {
|
||||
fmt.Printf("Found enum value [%s] for '%s' ('%s')\n", def.Value, def.VarName, *def.Description)
|
||||
} else {
|
||||
fmt.Printf("Found enum value [%s] for '%s'\n", def.Value, def.VarName)
|
||||
}
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
if debugOutput {
|
||||
fmt.Printf("Found non-enum value [%s] for '%s' ( looks like enum value, but no matching @enum:type )\n", def.Value, def.VarName)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return enums, pkgname, nil
|
||||
}
|
||||
|
@ -7,6 +7,8 @@ import "gogs.mikescher.com/BlackForestBytes/goext/enums"
|
||||
|
||||
const ChecksumEnumGenerator = "{{.Checksum}}" // GoExtVersion: {{.GoextVersion}}
|
||||
|
||||
{{ $pkgname := .PkgName }}
|
||||
|
||||
{{range .Enums}}
|
||||
|
||||
{{ $hasStr := ( . | hasStr ) }}
|
||||
@ -97,6 +99,14 @@ func (e {{.EnumTypeName}}) VarName() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func (e {{.EnumTypeName}}) TypeName() string {
|
||||
return "{{$typename}}"
|
||||
}
|
||||
|
||||
func (e {{.EnumTypeName}}) PackageName() string {
|
||||
return "{{$pkgname }}"
|
||||
}
|
||||
|
||||
func (e {{.EnumTypeName}}) Meta() enums.EnumMetaValue {
|
||||
{{if $hasDescr}} return enums.EnumMetaValue{VarName: e.VarName(), Value: e, Description: langext.Ptr(e.Description())} {{else}} return enums.EnumMetaValue{VarName: e.VarName(), Value: e, Description: nil} {{end}}
|
||||
}
|
||||
@ -135,3 +145,11 @@ func {{.EnumTypeName}}ValuesDescriptionMeta() []enums.EnumDescriptionMetaValue {
|
||||
{{end}}
|
||||
|
||||
{{end}}
|
||||
|
||||
// ================================ ================= ================================
|
||||
|
||||
func AllPackageEnums() []enums.Enum {
|
||||
return []enums.Enum{ {{range .Enums}}
|
||||
{{.EnumTypeName}}, {{end}}
|
||||
}
|
||||
}
|
@ -37,10 +37,10 @@ func TestGenerateEnumSpecs(t *testing.T) {
|
||||
_, err = cmdext.Runner("tar").Arg("-xvzf").Arg(tmpFile).Arg("-C").Arg(tmpDir).FailOnExitCode().FailOnTimeout().Timeout(time.Minute).Run()
|
||||
tst.AssertNoErr(t, err)
|
||||
|
||||
s1, cs1, _, err := _generateEnumSpecs(tmpDir, "", "N/A", true)
|
||||
s1, cs1, _, err := _generateEnumSpecs(tmpDir, "", "N/A", true, true)
|
||||
tst.AssertNoErr(t, err)
|
||||
|
||||
s2, cs2, _, err := _generateEnumSpecs(tmpDir, "", "N/A", true)
|
||||
s2, cs2, _, err := _generateEnumSpecs(tmpDir, "", "N/A", true, true)
|
||||
tst.AssertNoErr(t, err)
|
||||
|
||||
tst.AssertEqual(t, cs1, cs2)
|
||||
@ -76,7 +76,7 @@ func TestGenerateEnumSpecsData(t *testing.T) {
|
||||
_, err = cmdext.Runner("tar").Arg("-xvzf").Arg(tmpFile).Arg("-C").Arg(tmpDir).FailOnExitCode().FailOnTimeout().Timeout(time.Minute).Run()
|
||||
tst.AssertNoErr(t, err)
|
||||
|
||||
s1, _, _, err := _generateEnumSpecs(tmpDir, "", "", true)
|
||||
s1, _, _, err := _generateEnumSpecs(tmpDir, "", "", true, true)
|
||||
tst.AssertNoErr(t, err)
|
||||
|
||||
fmt.Println()
|
||||
|
@ -25,6 +25,10 @@ type IDDef struct {
|
||||
Name string
|
||||
}
|
||||
|
||||
type IDGenOptions struct {
|
||||
DebugOutput *bool
|
||||
}
|
||||
|
||||
var rexIDPackage = rext.W(regexp.MustCompile(`^package\s+(?P<name>[A-Za-z0-9_]+)\s*$`))
|
||||
|
||||
var rexIDDef = rext.W(regexp.MustCompile(`^\s*type\s+(?P<name>[A-Za-z0-9_]+)\s+string\s*//\s*(@id:type).*$`))
|
||||
@ -34,7 +38,9 @@ var rexIDChecksumConst = rext.W(regexp.MustCompile(`const ChecksumIDGenerator =
|
||||
//go:embed id-generate.template
|
||||
var templateIDGenerateText string
|
||||
|
||||
func GenerateIDSpecs(sourceDir string, destFile string) error {
|
||||
func GenerateIDSpecs(sourceDir string, destFile string, opt *IDGenOptions) error {
|
||||
|
||||
debugOutput := langext.Coalesce(opt.DebugOutput, false)
|
||||
|
||||
files, err := os.ReadDir(sourceDir)
|
||||
if err != nil {
|
||||
@ -80,13 +86,18 @@ func GenerateIDSpecs(sourceDir string, destFile string) error {
|
||||
pkgname := ""
|
||||
|
||||
for _, f := range files {
|
||||
if debugOutput {
|
||||
fmt.Printf("========= %s =========\n\n", f.Name())
|
||||
fileIDs, pn, err := processIDFile(sourceDir, path.Join(sourceDir, f.Name()))
|
||||
}
|
||||
|
||||
fileIDs, pn, err := processIDFile(sourceDir, path.Join(sourceDir, f.Name()), debugOutput)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if debugOutput {
|
||||
fmt.Printf("\n")
|
||||
}
|
||||
|
||||
allIDs = append(allIDs, fileIDs...)
|
||||
|
||||
@ -112,7 +123,7 @@ func GenerateIDSpecs(sourceDir string, destFile string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func processIDFile(basedir string, fn string) ([]IDDef, string, error) {
|
||||
func processIDFile(basedir string, fn string, debugOutput bool) ([]IDDef, string, error) {
|
||||
file, err := os.Open(fn)
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
@ -153,7 +164,11 @@ func processIDFile(basedir string, fn string) ([]IDDef, string, error) {
|
||||
FileRelative: rfp,
|
||||
Name: match.GroupByName("name").Value(),
|
||||
}
|
||||
|
||||
if debugOutput {
|
||||
fmt.Printf("Found ID definition { '%s' }\n", def.Name)
|
||||
}
|
||||
|
||||
ids = append(ids, def)
|
||||
}
|
||||
}
|
||||
|
@ -34,10 +34,10 @@ func TestGenerateIDSpecs(t *testing.T) {
|
||||
_, err = cmdext.Runner("tar").Arg("-xvzf").Arg(tmpFile).Arg("-C").Arg(tmpDir).FailOnExitCode().FailOnTimeout().Timeout(time.Minute).Run()
|
||||
tst.AssertNoErr(t, err)
|
||||
|
||||
err = GenerateIDSpecs(tmpDir, tmpDir+"/id_gen.go")
|
||||
err = GenerateIDSpecs(tmpDir, tmpDir+"/id_gen.go", &IDGenOptions{DebugOutput: langext.PTrue})
|
||||
tst.AssertNoErr(t, err)
|
||||
|
||||
err = GenerateIDSpecs(tmpDir, tmpDir+"/id_gen.go")
|
||||
err = GenerateIDSpecs(tmpDir, tmpDir+"/id_gen.go", &IDGenOptions{DebugOutput: langext.PTrue})
|
||||
tst.AssertNoErr(t, err)
|
||||
|
||||
fmt.Println()
|
||||
|
@ -5,6 +5,8 @@ type Enum interface {
|
||||
ValuesAny() []any
|
||||
ValuesMeta() []EnumMetaValue
|
||||
VarName() string
|
||||
TypeName() string
|
||||
PackageName() string
|
||||
}
|
||||
|
||||
type StringEnum interface {
|
||||
|
@ -1,5 +1,5 @@
|
||||
package goext
|
||||
|
||||
const GoextVersion = "0.0.401"
|
||||
const GoextVersion = "0.0.402"
|
||||
|
||||
const GoextVersionTimestamp = "2024-03-09T15:07:03+0100"
|
||||
const GoextVersionTimestamp = "2024-03-10T12:49:31+0100"
|
||||
|
Loading…
Reference in New Issue
Block a user