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

This commit is contained in:
Mike Schwörer 2024-03-10 12:49:31 +01:00
parent ed53f297bd
commit 409d6e108d
Signed by: Mikescher
GPG Key ID: D3C7172E0A70F8CF
10 changed files with 107 additions and 37 deletions

View File

@ -2,6 +2,8 @@
- cronext - cronext
- rfctime.DateOnly
- rfctime.HMSTimeOnly - rfctime.HMSTimeOnly
- rfctime.NanoTimeOnly - rfctime.NanoTimeOnly
- remove sqlx dependency from sq (unmaintained, and mostly superseeded by our own stuff?)
- Move DBLogger and DBPreprocessor to sq

View File

@ -26,6 +26,10 @@ type CSIDDef struct {
Prefix string Prefix string
} }
type CSIDGenOptions struct {
DebugOutput *bool
}
var rexCSIDPackage = rext.W(regexp.MustCompile(`^package\s+(?P<name>[A-Za-z0-9_]+)\s*$`)) 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})].*$`)) 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 //go:embed csid-generate.template
var templateCSIDGenerateText string 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) files, err := os.ReadDir(sourceDir)
if err != nil { if err != nil {
@ -81,13 +87,18 @@ func GenerateCharsetIDSpecs(sourceDir string, destFile string) error {
pkgname := "" pkgname := ""
for _, f := range files { for _, f := range files {
fmt.Printf("========= %s =========\n\n", f.Name()) if debugOutput {
fileIDs, pn, err := processCSIDFile(sourceDir, path.Join(sourceDir, f.Name())) fmt.Printf("========= %s =========\n\n", f.Name())
}
fileIDs, pn, err := processCSIDFile(sourceDir, path.Join(sourceDir, f.Name()), debugOutput)
if err != nil { if err != nil {
return err return err
} }
fmt.Printf("\n") if debugOutput {
fmt.Printf("\n")
}
allIDs = append(allIDs, fileIDs...) allIDs = append(allIDs, fileIDs...)
@ -113,7 +124,7 @@ func GenerateCharsetIDSpecs(sourceDir string, destFile string) error {
return nil 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) file, err := os.Open(fn)
if err != nil { if err != nil {
return nil, "", err return nil, "", err
@ -155,7 +166,11 @@ func processCSIDFile(basedir string, fn string) ([]CSIDDef, string, error) {
Name: match.GroupByName("name").Value(), Name: match.GroupByName("name").Value(),
Prefix: match.GroupByName("prefix").Value(), Prefix: match.GroupByName("prefix").Value(),
} }
fmt.Printf("Found ID definition { '%s' }\n", def.Name)
if debugOutput {
fmt.Printf("Found ID definition { '%s' }\n", def.Name)
}
ids = append(ids, def) ids = append(ids, def)
} }
} }

View File

@ -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() _, err = cmdext.Runner("tar").Arg("-xvzf").Arg(tmpFile).Arg("-C").Arg(tmpDir).FailOnExitCode().FailOnTimeout().Timeout(time.Minute).Run()
tst.AssertNoErr(t, err) 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) 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) tst.AssertNoErr(t, err)
fmt.Println() fmt.Println()

View File

@ -37,6 +37,11 @@ type EnumDef struct {
Values []EnumDefVal 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 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).*$`)) 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 //go:embed enum-generate.template
var templateEnumGenerateText string var templateEnumGenerateText string
func GenerateEnumSpecs(sourceDir string, destFile string) error { func GenerateEnumSpecs(sourceDir string, destFile string, opt EnumGenOptions) error {
oldChecksum := "N/A" oldChecksum := "N/A"
if _, err := os.Stat(destFile); !os.IsNotExist(err) { 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 { if err != nil {
return err return err
} }
@ -78,7 +83,7 @@ func GenerateEnumSpecs(sourceDir string, destFile string) error {
return nil 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) files, err := os.ReadDir(sourceDir)
if err != nil { if err != nil {
@ -113,13 +118,18 @@ func _generateEnumSpecs(sourceDir string, destFile string, oldChecksum string, g
pkgname := "" pkgname := ""
for _, f := range files { for _, f := range files {
fmt.Printf("========= %s =========\n\n", f.Name()) if debugOutput {
fileEnums, pn, err := processEnumFile(sourceDir, path.Join(sourceDir, f.Name())) fmt.Printf("========= %s =========\n\n", f.Name())
}
fileEnums, pn, err := processEnumFile(sourceDir, path.Join(sourceDir, f.Name()), debugOutput)
if err != nil { if err != nil {
return "", "", false, err return "", "", false, err
} }
fmt.Printf("\n") if debugOutput {
fmt.Printf("\n")
}
allEnums = append(allEnums, fileEnums...) allEnums = append(allEnums, fileEnums...)
@ -146,7 +156,7 @@ func _generateEnumSpecs(sourceDir string, destFile string, oldChecksum string, g
return string(fdata), newChecksum, true, nil 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) file, err := os.Open(fn)
if err != nil { if err != nil {
return nil, "", err return nil, "", err
@ -190,7 +200,10 @@ func processEnumFile(basedir string, fn string) ([]EnumDef, string, error) {
Values: make([]EnumDefVal, 0), Values: make([]EnumDefVal, 0),
} }
enums = append(enums, def) enums = append(enums, def)
fmt.Printf("Found enum definition { '%s' -> '%s' }\n", def.EnumTypeName, def.Type)
if debugOutput {
fmt.Printf("Found enum definition { '%s' -> '%s' }\n", def.EnumTypeName, def.Type)
}
} }
if match, ok := rexEnumValueDef.MatchFirst(line); ok { if match, ok := rexEnumValueDef.MatchFirst(line); ok {
@ -230,16 +243,21 @@ func processEnumFile(basedir string, fn string) ([]EnumDef, string, error) {
if v.EnumTypeName == typename { if v.EnumTypeName == typename {
enums[i].Values = append(enums[i].Values, def) enums[i].Values = append(enums[i].Values, def)
found = true found = true
if def.Description != nil {
fmt.Printf("Found enum value [%s] for '%s' ('%s')\n", def.Value, def.VarName, *def.Description) if debugOutput {
} else { if def.Description != nil {
fmt.Printf("Found enum value [%s] for '%s'\n", def.Value, def.VarName) 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 break
} }
} }
if !found { if !found {
fmt.Printf("Found non-enum value [%s] for '%s' ( looks like enum value, but no matching @enum:type )\n", def.Value, def.VarName) 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)
}
} }
} }
} }

View File

@ -7,6 +7,8 @@ import "gogs.mikescher.com/BlackForestBytes/goext/enums"
const ChecksumEnumGenerator = "{{.Checksum}}" // GoExtVersion: {{.GoextVersion}} const ChecksumEnumGenerator = "{{.Checksum}}" // GoExtVersion: {{.GoextVersion}}
{{ $pkgname := .PkgName }}
{{range .Enums}} {{range .Enums}}
{{ $hasStr := ( . | hasStr ) }} {{ $hasStr := ( . | hasStr ) }}
@ -97,6 +99,14 @@ func (e {{.EnumTypeName}}) VarName() string {
return "" return ""
} }
func (e {{.EnumTypeName}}) TypeName() string {
return "{{$typename}}"
}
func (e {{.EnumTypeName}}) PackageName() string {
return "{{$pkgname }}"
}
func (e {{.EnumTypeName}}) Meta() enums.EnumMetaValue { 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}} {{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}}
} }
@ -134,4 +144,12 @@ func {{.EnumTypeName}}ValuesDescriptionMeta() []enums.EnumDescriptionMetaValue {
} }
{{end}} {{end}}
{{end}} {{end}}
// ================================ ================= ================================
func AllPackageEnums() []enums.Enum {
return []enums.Enum{ {{range .Enums}}
{{.EnumTypeName}}, {{end}}
}
}

View File

@ -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() _, err = cmdext.Runner("tar").Arg("-xvzf").Arg(tmpFile).Arg("-C").Arg(tmpDir).FailOnExitCode().FailOnTimeout().Timeout(time.Minute).Run()
tst.AssertNoErr(t, err) 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) 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.AssertNoErr(t, err)
tst.AssertEqual(t, cs1, cs2) 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() _, err = cmdext.Runner("tar").Arg("-xvzf").Arg(tmpFile).Arg("-C").Arg(tmpDir).FailOnExitCode().FailOnTimeout().Timeout(time.Minute).Run()
tst.AssertNoErr(t, err) tst.AssertNoErr(t, err)
s1, _, _, err := _generateEnumSpecs(tmpDir, "", "", true) s1, _, _, err := _generateEnumSpecs(tmpDir, "", "", true, true)
tst.AssertNoErr(t, err) tst.AssertNoErr(t, err)
fmt.Println() fmt.Println()

View File

@ -25,6 +25,10 @@ type IDDef struct {
Name string Name string
} }
type IDGenOptions struct {
DebugOutput *bool
}
var rexIDPackage = rext.W(regexp.MustCompile(`^package\s+(?P<name>[A-Za-z0-9_]+)\s*$`)) 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).*$`)) 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 //go:embed id-generate.template
var templateIDGenerateText string 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) files, err := os.ReadDir(sourceDir)
if err != nil { if err != nil {
@ -80,13 +86,18 @@ func GenerateIDSpecs(sourceDir string, destFile string) error {
pkgname := "" pkgname := ""
for _, f := range files { for _, f := range files {
fmt.Printf("========= %s =========\n\n", f.Name()) if debugOutput {
fileIDs, pn, err := processIDFile(sourceDir, path.Join(sourceDir, f.Name())) fmt.Printf("========= %s =========\n\n", f.Name())
}
fileIDs, pn, err := processIDFile(sourceDir, path.Join(sourceDir, f.Name()), debugOutput)
if err != nil { if err != nil {
return err return err
} }
fmt.Printf("\n") if debugOutput {
fmt.Printf("\n")
}
allIDs = append(allIDs, fileIDs...) allIDs = append(allIDs, fileIDs...)
@ -112,7 +123,7 @@ func GenerateIDSpecs(sourceDir string, destFile string) error {
return nil 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) file, err := os.Open(fn)
if err != nil { if err != nil {
return nil, "", err return nil, "", err
@ -153,7 +164,11 @@ func processIDFile(basedir string, fn string) ([]IDDef, string, error) {
FileRelative: rfp, FileRelative: rfp,
Name: match.GroupByName("name").Value(), Name: match.GroupByName("name").Value(),
} }
fmt.Printf("Found ID definition { '%s' }\n", def.Name)
if debugOutput {
fmt.Printf("Found ID definition { '%s' }\n", def.Name)
}
ids = append(ids, def) ids = append(ids, def)
} }
} }

View File

@ -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() _, err = cmdext.Runner("tar").Arg("-xvzf").Arg(tmpFile).Arg("-C").Arg(tmpDir).FailOnExitCode().FailOnTimeout().Timeout(time.Minute).Run()
tst.AssertNoErr(t, err) 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) 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) tst.AssertNoErr(t, err)
fmt.Println() fmt.Println()

View File

@ -5,6 +5,8 @@ type Enum interface {
ValuesAny() []any ValuesAny() []any
ValuesMeta() []EnumMetaValue ValuesMeta() []EnumMetaValue
VarName() string VarName() string
TypeName() string
PackageName() string
} }
type StringEnum interface { type StringEnum interface {

View File

@ -1,5 +1,5 @@
package goext 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"