Skip to content

Commit

Permalink
Merge ba9ed71 into 60bd1e5
Browse files Browse the repository at this point in the history
  • Loading branch information
roblillack committed Mar 23, 2021
2 parents 60bd1e5 + ba9ed71 commit 1950a40
Show file tree
Hide file tree
Showing 24 changed files with 378 additions and 274 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ env:
- GO111MODULE=on

go:
- 1.12
- 1.13
- 1.14
- 1.15
Expand Down
12 changes: 8 additions & 4 deletions cmd/mars-gen/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"bytes"
"fmt"
"go/format"
"os"
Expand All @@ -9,8 +10,6 @@ import (
"time"

"github.com/codegangsta/cli"

"github.com/roblillack/mars"
)

func fatalf(layout string, args ...interface{}) {
Expand Down Expand Up @@ -109,7 +108,12 @@ func reverseRoutes(ctx *cli.Context) {
}

func generateSources(tpl, filename string, templateArgs map[string]interface{}) {
sourceCode := mars.ExecuteTemplate(template.Must(template.New("").Parse(tpl)), templateArgs)
var b bytes.Buffer

tmpl := template.Must(template.New("").Parse(tpl))
if err := tmpl.Execute(&b, templateArgs); err != nil {
fatalf("Unable to create source file: %v", err)
}

if err := os.MkdirAll(path.Dir(filename), 0755); err != nil {
fatalf("Unable to create dir: %v", err)
Expand All @@ -122,7 +126,7 @@ func generateSources(tpl, filename string, templateArgs map[string]interface{})
}
defer file.Close()

formatted, err := format.Source([]byte(sourceCode))
formatted, err := format.Source(b.Bytes())
if err != nil {
fatalf("Failed to format file: %v", err)
}
Expand Down
25 changes: 20 additions & 5 deletions cmd/mars-gen/reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ type SourceInfo struct {
// controllerSpecs lists type info for all structs found under
// app/controllers/... that embed (directly or indirectly) mars.Controller
controllerSpecs []*TypeInfo
// testSuites list the types that constitute the set of application tests.
testSuites []*TypeInfo
}

// TypeInfo summarizes information about a struct type in the app source code.
Expand Down Expand Up @@ -445,6 +443,15 @@ func getStructTypeDecl(decl ast.Decl, fset *token.FileSet) (spec *ast.TypeSpec,
return
}

func containsString(list []string, target string) bool {
for _, el := range list {
if el == target {
return true
}
}
return false
}

// TypesThatEmbed returns all types that (directly or indirectly) embed the
// target type, which must be a fully qualified type name,
// e.g. "github.com/roblillack/mars.Controller"
Expand All @@ -462,8 +469,7 @@ func (s *SourceInfo) TypesThatEmbed(targetType string) (filtered []*TypeInfo) {
// Look through all known structs.
for _, spec := range s.StructSpecs {
// If this one has been processed or is already in nodeQueue, then skip it.
if mars.ContainsString(processed, spec.String()) ||
mars.ContainsString(nodeQueue, spec.String()) {
if containsString(processed, spec.String()) || containsString(nodeQueue, spec.String()) {
continue
}

Expand Down Expand Up @@ -502,10 +508,19 @@ type TypeExpr struct {
Valid bool
}

func firstNonEmpty(strs ...string) string {
for _, str := range strs {
if len(str) > 0 {
return str
}
}
return ""
}

// TypeName returns the fully-qualified type name for this expression.
// The caller may optionally specify a package name to override the default.
func (e TypeExpr) TypeName(pkgOverride string) string {
pkgName := mars.FirstNonEmpty(pkgOverride, e.PkgName)
pkgName := firstNonEmpty(pkgOverride, e.PkgName)
if pkgName == "" {
return e.Expr
}
Expand Down
20 changes: 20 additions & 0 deletions cookie.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package mars

import (
"net/url"
"regexp"
)

var (
cookieKeyValueParser = regexp.MustCompile("\x00([^:]*):([^\x00]*)\x00")
)

// parseKeyValueCookie takes the raw (escaped) cookie value and parses out key values.
func parseKeyValueCookie(val string, cb func(key, val string)) {
val, _ = url.QueryUnescape(val)
if matches := cookieKeyValueParser.FindAllStringSubmatch(val, -1); matches != nil {
for _, match := range matches {
cb(match[1], match[2])
}
}
}
6 changes: 6 additions & 0 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ type Error struct {
Link string // A configurable link to wrap the error source in
}

var _ error = &Error{}

// An object to hold the per-source-line details.
type sourceLine struct {
Source string
Expand All @@ -27,6 +29,10 @@ type sourceLine struct {
// Construct a plaintext version of the error, taking account that fields are optionally set.
// Returns e.g. Compilation Error (in views/header.html:51): expected right delim in end; got "}"
func (e *Error) Error() string {
if e == nil {
return "<nil>"
}

loc := ""
if e.Path != "" {
line := ""
Expand Down
2 changes: 1 addition & 1 deletion filterconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func FilterAction(methodRef interface{}) FilterConfigurator {
}

controllerType := methodType.In(0)
method := FindMethod(controllerType, methodValue)
method := findMethod(controllerType, methodValue)
if method == nil {
panic("Action not found on controller " + controllerType.Name())
}
Expand Down
2 changes: 1 addition & 1 deletion flash.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func restoreFlash(req *http.Request) Flash {
Out: make(map[string]string),
}
if cookie, err := req.Cookie(CookiePrefix + "_FLASH"); err == nil {
ParseKeyValueCookie(cookie.Value, func(key, val string) {
parseKeyValueCookie(cookie.Value, func(key, val string) {
flash.Data[key] = val
})
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/roblillack/mars

go 1.12
go 1.13

require (
github.com/agtorre/gocolorize v1.0.0
Expand Down
Loading

0 comments on commit 1950a40

Please sign in to comment.