transpiler/transpiler_utils.go
package transpiler
import (
"fmt"
"io/ioutil"
"strings"
"github.com/cheikhshift/samb"
)
// WrapEndpoint adds conditional Go statements
// around the specified h parameter. Parameter
// h is Go code. parameter r (samb.Route) specifies
// which condition is evaluated.
func WrapEndpoint(path string, r samb.Route, h string) string {
res := fmt.Sprintf(`
if basePath := "%s"; strings.Contains(r.URL.Path , basePath) `, path+r.Path)
if r.Strict {
res = fmt.Sprintf(`
if basePath := "%s"; r.URL.Path == basePath `, path+r.Path)
}
if r.Method != "" &&
r.Method != "*" {
res += fmt.Sprintf(`&& r.Method == "%s"`, strings.ToUpper(r.Method))
}
return fmt.Sprintf(endpointWrapper, res, h)
}
// GetHandler Generates the Go code executed specified to
// be executed by
// a samb route.
func GetHandler(p *samb.Project, r samb.Route, providers []string) (middleware, handler string) {
gocode := GetCustomCode(r)
var providerInitializer = GetProviderInits(p, providers, gocode + r.Handler)
middleware = providerInitializer + gocode
if r.Handler != "" {
handler = "\n\ntools.ShortenPath(basePath, r)\n" + r.Handler + "\nreturn"
}
return
}
// GetCustomCode returns the Go
// code to be ran, by the specified
// samb route.
func GetCustomCode(r samb.Route) string {
return strings.Join(r.Go.Do, "\n") + "\n"
}
// WriteRecoveryFuncs generates a file
// with the recovery function called
// on panic.
func WriteRecoveryFuncs(r []string) error {
for i := range r {
r[i] += "(w,r,n.(string))"
}
recovFile := fmt.Sprintf(recoveryWrapper, strings.Join(r, "\n"))
err := ioutil.WriteFile("./pkg/api/recover.go", []byte(recovFile), 0700)
return err
}