1
0

reflection work so far...

This commit is contained in:
brent saner
2025-01-25 16:11:19 -05:00
parent bf887ce948
commit 1471dc29ed
31 changed files with 2240 additions and 150 deletions

View File

@@ -0,0 +1,32 @@
package main
import (
_ `embed`
`text/template`
`github.com/oriser/regroup`
)
const (
pfx string = "Param"
matchType string = "tlsUriParam"
)
var (
//go:embed "tpl/consts_param_map.go.tpl"
constmapTplBytes []byte
tpl = template.Must(template.New("consts").Parse(string(constmapTplBytes)))
)
var (
// If we restructure, these paths will need to be changed.
// constsPath string = filepath.Join("..", "..", "consts.go")
// outPath string = filepath.Join("..", "..", "consts_param_map.go")
constsPath string = "consts.go"
outPath string = "consts_param_map.go"
)
var (
// The most complex part about this pattern is it has to quote the backticks as their own string addition.
stripQuotesPtrn *regroup.ReGroup = regroup.MustCompile(`^(` + "`" + `(?P<m1>.+)` + "`" + `|"(?P<m2>.+)")$`)
)

View File

@@ -0,0 +1,94 @@
package main
import (
`errors`
`go/ast`
`go/token`
`log`
`strings`
`github.com/oriser/regroup`
)
/*
getParamSpec takes an ast.Decl d and returns a slice of
ParamConst found in it.
If no ParamConst are found, foundParams will be nil.
*/
func getValueSpec(d ast.Decl) (foundParams []*ParamConst) {
var ok bool
var idx int
var gd *ast.GenDecl
var spec ast.Spec
var vs *ast.ValueSpec
var vsId *ast.Ident
var nm *ast.Ident
var bl *ast.BasicLit
if gd, ok = d.(*ast.GenDecl); !ok || gd.Tok != token.CONST {
return
}
for _, spec = range gd.Specs {
if vs, ok = spec.(*ast.ValueSpec); !ok {
continue
}
if vs.Type != nil {
if vsId, ok = vs.Type.(*ast.Ident); !ok || vsId.Name != matchType {
continue
}
}
for idx, nm = range vs.Names {
if !strings.HasPrefix(nm.Name, pfx) {
continue
}
if bl, ok = vs.Values[idx].(*ast.BasicLit); !ok {
continue
}
foundParams = append(
foundParams,
&ParamConst{
ConstName: nm.Name,
// UriParamName: bl.Value,
UriParamName: stripQuotes(bl.Value),
},
)
}
}
return
}
/*
stripQuotes removes Golang-AST defining quotes.
This probably doesn't work for multiline, but should be fine for our purposes.
*/
func stripQuotes(inStr string) (outStr string) {
var err error
var matches map[string]string
var nomchErr *regroup.NoMatchFoundError = new(regroup.NoMatchFoundError)
outStr = inStr
if matches, err = stripQuotesPtrn.Groups(inStr); err != nil {
if errors.As(err, &nomchErr) {
err = nil
return
} else {
log.Panicln(err)
}
}
for _, v := range matches {
if v != "" {
outStr = v
return
}
}
return
}

57
internal/constmap/main.go Normal file
View File

@@ -0,0 +1,57 @@
package main
import (
`bytes`
`fmt`
`go/ast`
`go/parser`
`go/token`
`log`
`os`
`r00t2.io/sysutils/paths`
)
/*
DO NOT RUN THIS ANYWHERE BUT FROM WHERE <r00t2.io/cryptparse>/consts.go IS LOCATED.
*/
// I *cannot believe* a library does not exist that will do this for me.
func main() {
var err error
var tfs *token.FileSet
var af *ast.File
var foundParams []*ParamConst
var paramConsts []*ParamConst
var buf *bytes.Buffer = new(bytes.Buffer)
if err = paths.RealPath(&constsPath); err != nil {
return
}
if err = paths.RealPath(&outPath); err != nil {
return
}
tfs = token.NewFileSet()
if af, err = parser.ParseFile(tfs, constsPath, nil, parser.AllErrors|parser.ParseComments); err != nil {
log.Panicln(err)
}
for _, d := range af.Decls {
if foundParams = getValueSpec(d); foundParams == nil {
continue
}
paramConsts = append(paramConsts, foundParams...)
}
if err = tpl.Execute(buf, paramConsts); err != nil {
log.Panicln(err)
}
if err = os.WriteFile(outPath, buf.Bytes(), 0644); err != nil {
log.Panicln(err)
}
fmt.Printf("++ Generated %s ++\n", outPath)
}

View File

@@ -0,0 +1,24 @@
{{- /*gotype: r00t2.io/cryptparse/internal/constmap.ParamConsts*/ -}}
package cryptparse
/*
THIS FILE IS AUTOMATICALLY GENERATED.
DO NOT EDIT.
SEE internal/constmap/ FOR DETAILS.
*/
var (
// tlsUriParamStrMap contains a map of the constant string *name* of a tlsUriParam as mapped to its *value* (at time of generation).
tlsUriParamStrMap map[string]string = map[string]string{
{{- range $p := . }}
{{ printf "%#v" $p.ConstName }}: {{ printf "%#v" $p.UriParamName }},
{{- end }}
}
// tlsUriStrParamMap contains a map of the *value* (at time of generation) of tlsUriParam constants to the constant string *name*.
tlsUriStrParamMap map[string]string = map[string]string{
{{- range $p := . }}
{{ printf "%#v" $p.UriParamName }}: {{ printf "%#v" $p.ConstName }},
{{- end }}
}
)

View File

@@ -0,0 +1,8 @@
package main
type ParamConsts []*ParamConst
type ParamConst struct {
ConstName string
UriParamName string
}