github.com/Azareal/Gosora@v0.0.0-20210729070923-553e66b59003/cmd/common_hook_gen/hookgen.go (about) 1 package hookgen 2 3 import ( 4 "bytes" 5 "log" 6 "os" 7 "text/template" 8 ) 9 10 type HookVars struct { 11 Imports []string 12 Hooks []Hook 13 } 14 15 type Hook struct { 16 Name string 17 Params string 18 Params2 string 19 Ret string 20 Type string 21 Any bool 22 MultiHook bool 23 Skip bool 24 DefaultRet string 25 Pure string 26 } 27 28 func AddHooks(add func(name, params, ret, htype string, multiHook, skip bool, defaultRet, pure string)) { 29 vhookskip := func(name, params string) { 30 add(name, params, "(bool,RouteError)", "VhookSkippable_", false, true, "false,nil", "") 31 } 32 vhookskip("simple_forum_check_pre_perms", "w http.ResponseWriter,r *http.Request,u *User,fid *int,h *HeaderLite") 33 vhookskip("forum_check_pre_perms", "w http.ResponseWriter,r *http.Request,u *User,fid *int,h *Header") 34 vhookskip("router_after_filters", "w http.ResponseWriter,r *http.Request,prefix string") 35 vhookskip("router_pre_route", "w http.ResponseWriter,r *http.Request,u *User,prefix string") 36 vhookskip("route_forum_list_start", "w http.ResponseWriter,r *http.Request,u *User,h *Header") 37 vhookskip("route_topic_list_start", "w http.ResponseWriter,r *http.Request,u *User,h *Header") 38 vhookskip("route_attach_start", "w http.ResponseWriter,r *http.Request,u *User,fname string") 39 vhookskip("route_attach_post_get", "w http.ResponseWriter,r *http.Request,u *User,a *Attachment") 40 41 vhooknoret := func(name, params string) { 42 add(name, params, "", "Vhooks", false, false, "false,nil", "") 43 } 44 vhooknoret("router_end", "w http.ResponseWriter,r *http.Request,u *User,prefix string,extraData string") 45 vhooknoret("topic_reply_row_assign", "r *ReplyUser") 46 vhooknoret("counters_perf_tick_row", "low int64,high int64,avg int64") 47 //forums_frow_assign 48 //Hook(name string, data interface{}) interface{} 49 /*hook := func(name, params, ret, pure string) { 50 add(name,params,ret,"Hooks",true,false,ret,pure) 51 }*/ 52 53 hooknoret := func(name, params string) { 54 add(name, params, "", "HooksNoRet", true, false, "", "") 55 } 56 hooknoret("forums_frow_assign", "f *Forum") 57 58 hookskip := func(name, params string) { 59 add(name, params, "(skip bool)", "HooksSkip", true, true, "", "") 60 } 61 //hookskip("forums_frow_assign","f *Forum") 62 hookskip("topic_create_frow_assign", "f *Forum") 63 64 hookss := func(name string) { 65 add(name, "d string", "string", "Sshooks", true, false, "", "d") 66 } 67 hookss("topic_ogdesc_assign") 68 } 69 70 func Write(hookVars HookVars) { 71 fileData := `// Code generated by Gosora's Hook Generator. DO NOT EDIT. 72 /* This file was automatically generated by the software. Please don't edit it as your changes may be overwritten at any moment. */ 73 package common 74 import ({{range .Imports}} 75 "{{.}}"{{end}} 76 ) 77 {{range .Hooks}} 78 func H_{{.Name}}_hook(t *HookTable,{{.Params}}) {{.Ret}} { {{if .Any}} 79 {{if .MultiHook}}for _, hook := range t.{{.Type}}["{{.Name}}"] { 80 {{if .Skip}}if skip = hook({{.Params2}}); skip { 81 break 82 }{{else}}{{if .Pure}}{{.Pure}} = {{else if .Ret}}return {{end}}hook({{.Params2}}){{end}} 83 }{{else}}hook := t.{{.Type}}["{{.Name}}"] 84 if hook != nil { 85 {{if .Ret}}return {{end}}hook({{.Params2}}) 86 } {{end}}{{end}}{{if .Pure}} 87 return {{.Pure}}{{else if .Ret}} 88 return {{.DefaultRet}}{{end}} 89 }{{end}} 90 ` 91 tmpl := template.Must(template.New("hooks").Parse(fileData)) 92 var b bytes.Buffer 93 if e := tmpl.Execute(&b, hookVars); e != nil { 94 log.Fatal(e) 95 } 96 97 err := writeFile("./common/gen_extend.go", b.String()) 98 if err != nil { 99 log.Fatal(err) 100 } 101 } 102 103 func writeFile(name, body string) error { 104 f, e := os.Create(name) 105 if e != nil { 106 return e 107 } 108 if _, e = f.WriteString(body); e != nil { 109 return e 110 } 111 if e = f.Sync(); e != nil { 112 return e 113 } 114 return f.Close() 115 }