Skip to content

Commit

Permalink
linting
Browse files Browse the repository at this point in the history
  • Loading branch information
dlepex committed Mar 22, 2018
1 parent dc75351 commit e9e2361
Show file tree
Hide file tree
Showing 14 changed files with 159 additions and 152 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ _testmain.go
*.out
*.log
testdata/**/*_ti.go
.vscode/
3 changes: 3 additions & 0 deletions .gometalinter.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"Enable":["deadcode", "varcheck", "golint", "errcheck"]
}
28 changes: 15 additions & 13 deletions dsl.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,32 @@ import (
)

type (
// DSL - dsl-struct description
DSL struct {
Imports
Items []*DSLItem
PkgName string
}

PkgTypePair struct {
PkgName string
Type string
}

// DSLItem corresponds to the field of dsl-struct
DSLItem struct {
InstName string
GenericTypes []PkgTypePair
TypeArgs map[string]string
}
// PkgTypePair tuple of type and its package
PkgTypePair struct {
PkgName string
Type string
}
)

const DefaultStructName = "_typeinst"
const defaultStructName = "_typeinst"

// ParseDSL parses and rertrieves dsl-struct
func ParseDSL(filename, structName string) (dsl *DSL, err error) {
defer bpan.RecoverTo(&err)
if structName == "" {
structName = DefaultStructName
structName = defaultStructName
}
fset := token.NewFileSet()
f, err := parser.ParseFile(fset, filename, nil, parser.ParseComments)
Expand Down Expand Up @@ -77,8 +79,8 @@ func ParseDSL(filename, structName string) (dsl *DSL, err error) {
it.TypeArgs[typeVar] = stringer.ToString(field.Type)
}

for pkgname, _ := range typeVarsPkgs {
dsl.Imports.Add(pkgname, imports.requireNamed(pkgname))
for pkgname := range typeVarsPkgs {
bpan.Check(dsl.Imports.Add(pkgname, imports.requireNamed(pkgname)))
}

qtset := NewStrSet()
Expand All @@ -90,7 +92,7 @@ func ParseDSL(filename, structName string) (dsl *DSL, err error) {
if pair.PkgName == "" {
bpan.Panicf(estr("generic type cannot be local, it must be imported from another package"))
}
qt := pair.QualifiedType()
qt := pair.qualifiedType()
if qtset.Has(qt) {
bpan.Panicf(estr("merging repeated generic type: %v"), qt)
}
Expand Down Expand Up @@ -160,7 +162,7 @@ func (s *astStringer) ToString(node interface{}) string {
s.Config.Mode = printer.RawFormat
}
s.buf.Reset()
s.Fprint(s.buf, s.fset, node)
_ = s.Fprint(s.buf, s.fset, node)
return s.buf.String()
}

Expand Down Expand Up @@ -192,7 +194,7 @@ func parseGenericTypeExpr(t ast.Expr) PkgTypePair {
return PkgTypePair{}
}

func (p PkgTypePair) QualifiedType() string {
func (p PkgTypePair) qualifiedType() string {
if p.PkgName == "" {
return p.Type
}
Expand Down
1 change: 1 addition & 0 deletions generic/set/set.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// nolint
package set

type E = interface{}
Expand Down
2 changes: 1 addition & 1 deletion gentypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

//go:generate typeinst
type _typeinst struct {
type _typeinst struct { // nolint
StrSet func(E string) set.Set
AstIdentSet func(E *ast.Ident) set.Set
}
1 change: 1 addition & 0 deletions gentypes_ti.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// This file was generated by typeinst. Do not edit, use `go generate` instead.
// nolint
package main

import (
Expand Down
35 changes: 18 additions & 17 deletions imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ import (
"strings"
)

// Import limitations in "generic" packages:
// - The imported packages must have the same local names in each file of the generic package
// - Can not import package as "."
// Imports is name-path bimap
type Imports struct {
p2n map[string]string // path -> name, one side of bimap
n2p map[string]string // the other side
Expand All @@ -23,26 +21,29 @@ func (im *Imports) init() {
}
}

// IsEmpty -
func (im *Imports) IsEmpty() bool { return len(im.p2n) == 0 }

// AddSpec -
func (im *Imports) AddSpec(spec *ast.ImportSpec) error {
return im.Add(ImportSpecName(spec), spec.Path.Value)
return im.Add(importSpecName(spec), spec.Path.Value)
}

func ImportSpecName(spec *ast.ImportSpec) string {
func importSpecName(spec *ast.ImportSpec) string {
if spec.Name != nil {
return spec.Name.Name
} else {
a := strings.FieldsFunc(spec.Path.Value, func(r rune) bool {
return r == '"' || r == '/'
})
if len(a) == 0 {
log.Fatal("Nameless import spec")
}
return a[len(a)-1]
}
a := strings.FieldsFunc(spec.Path.Value, func(r rune) bool {
return r == '"' || r == '/'
})
if len(a) == 0 {
log.Fatal("Nameless import spec")
}
return a[len(a)-1]

}

// Add n - import name, p - import path
func (im *Imports) Add(n, p string) error {
if n == "." {
return fmt.Errorf("dot-import is not allowed: %s", p)
Expand All @@ -54,9 +55,8 @@ func (im *Imports) Add(n, p string) error {
if oldn, ok := im.p2n[p]; ok {
if oldn != n {
return fmt.Errorf("import package %s under different names: %s, %s ", p, n, oldn)
} else {
return nil
}
return nil
}
if oldp, ok := im.n2p[n]; ok {
if oldp != p {
Expand Down Expand Up @@ -91,12 +91,12 @@ func (im *Imports) Merge(other Imports) map[string]string {
}
}
for _, pair := range add {
im.Add(pair[0], pair[1])
_ = im.Add(pair[0], pair[1])
}
return rename
}

func (im *Imports) Decl() *ast.GenDecl {
func (im *Imports) decl() *ast.GenDecl {
specs := make([]ast.Spec, 0, len(im.n2p))
for n, p := range im.n2p {
spec := &ast.ImportSpec{
Expand All @@ -122,6 +122,7 @@ func (im *Imports) Decl() *ast.GenDecl {
}
}

//Named returns import path by name
func (im *Imports) Named(n string) string {
return im.n2p[n]
}
Expand Down
38 changes: 19 additions & 19 deletions mangle.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,61 +6,61 @@ import (
"unicode/utf8"
)

// MangleCtorName mangles constructor function name.
// orig - original ctor name, gen - generic type name, inst - instantiated type name
func MangleCtorName(orig, gen, inst string) string {
n, isUpper := StrUpcase(orig)
inst, _ = StrUpcase(inst)
gen, _ = StrUpcase(gen)
n, isUpper := strUpcase(orig)
inst, _ = strUpcase(inst)
gen, _ = strUpcase(gen)
g := strings.Index(n, gen)

if g < 0 {
n = orig + inst
} else {
n = orig[0:g] + inst + n[g+len(gen):]
}
return StrEnsureCase(n, isUpper)
return strEnsureCase(n, isUpper)
}

// "non-root generic types"
// MangleDepTypeName mangles non-root generic type.
// orig - original dependant type name, gen - "parent" type name, inst - instantiated "parent" type name
func MangleDepTypeName(orig, gen, inst string) string {
n, isUpper := StrUpcase(orig)
gen, _ = StrUpcase(gen)
inst, _ = StrUpcase(inst)
n, isUpper := strUpcase(orig)
gen, _ = strUpcase(gen)
inst, _ = strUpcase(inst)
g := strings.Index(n, gen)
if g < 0 {
n = inst + n
} else {
n = orig[0:g] + inst + n[g+len(gen):]
}
return StrEnsureCase(n, isUpper)
return strEnsureCase(n, isUpper)
}

func StrUpcase(s string) (string, bool) {
return StrReplaceFirst(s, unicode.IsUpper, unicode.ToUpper)
func strUpcase(s string) (string, bool) {
return strReplaceFirst(s, unicode.IsUpper, unicode.ToUpper)
}
func StrLocase(s string) (string, bool) {
return StrReplaceFirst(s, unicode.IsLower, unicode.ToLower)
func strLocase(s string) (string, bool) {
return strReplaceFirst(s, unicode.IsLower, unicode.ToLower)

}

func StrEnsureCase(s string, isUpper bool) (result string) {
func strEnsureCase(s string, isUpper bool) (result string) {
if isUpper {
result, _ = StrUpcase(s)
result, _ = strUpcase(s)
} else {
result, _ = StrLocase(s)
result, _ = strLocase(s)
}
return
}

func StrReplaceFirst(s string, isMapped func(rune) bool, doMap func(rune) rune) (string, bool) {
func strReplaceFirst(s string, isMapped func(rune) bool, doMap func(rune) rune) (string, bool) {
if s == "" {
return "", true
}
r, p := utf8.DecodeRuneInString(s)
if isMapped(r) {
return s, true
} else {
return string(doMap(r)) + s[p:], false
}
return string(doMap(r)) + s[p:], false
}
19 changes: 10 additions & 9 deletions misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ func genSymbol(prefix string) string {
return fmt.Sprintf("%s_%d", prefix, atomic.AddInt64(&symCounter, 1))
}

func DictStr(d map[string]string) (keystr, str string) {
func dictStr(d map[string]string) (keystr, str string) {
if len(d) == 0 {
return "", ""
}
keys := make([]string, 0, len(d))
for k, _ := range d {
for k := range d {
keys = append(keys, k)
}
sort.Strings(keys)
Expand All @@ -43,32 +43,33 @@ func DictStr(d map[string]string) (keystr, str string) {
return bk.String(), b.String()
}

// Immutable
// TypeArgs is cached typevar bindings map, *TypeArgs is used as map key.
type TypeArgs struct {
Binds map[string]string // typevar -> replacement
Key string // unique string for targsCache
Shape string // unique string of Binds map keys
}

var targsCache map[string]*TypeArgs = make(map[string]*TypeArgs)
var targsCache = make(map[string]*TypeArgs)
var bindsCacheLock sync.Mutex

// TypeArgsOf returns cached result
func TypeArgsOf(m map[string]string) *TypeArgs {
if len(m) == 0 {
return nil
}
bindsCacheLock.Lock()
defer bindsCacheLock.Unlock()
shape, key := DictStr(m)
shape, key := dictStr(m)
if b, ok := targsCache[key]; ok {
return b
} else {
b := &TypeArgs{m, key, shape}
targsCache[key] = b
return b
}
b := &TypeArgs{m, key, shape}
targsCache[key] = b
return b
}

// Len returns number of bindings
func (b *TypeArgs) Len() int {
if b == nil {
return 0
Expand Down
8 changes: 5 additions & 3 deletions pan/bounded.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Le terme panique est une référence au dieu Pan en mythologie grecque
// Package pan - le terme panique est une référence au dieu Pan en mythologie grecque
package pan

import (
Expand All @@ -18,12 +18,12 @@ type errWrap struct {
b *Bounded
}

// must be called to initialize private var of your package
// NewBounded must be called to initialize private var of your package
func NewBounded() *Bounded {
return &Bounded{callerPkg()}
}

// Public API of your package must use RecoverTo to translate "bounded panic" into error.
// RecoverTo - public API of your package must use RecoverTo to translate "bounded panic" into error.
func (b *Bounded) RecoverTo(errPtr *error) {
r := recover()
if r == nil {
Expand All @@ -45,12 +45,14 @@ func (b *Bounded) Check(e error) {
}
}

// Checkf asserts condition, if failed panics
func (b *Bounded) Checkf(cond bool, format string, a ...interface{}) {
if !cond {
panic(&errWrap{fmt.Errorf(format, a...), b})
}
}

// Panicf -
func (b *Bounded) Panicf(format string, a ...interface{}) {
panic(&errWrap{fmt.Errorf(format, a...), b})
}
Expand Down
Loading

0 comments on commit e9e2361

Please sign in to comment.