Skip to content

Commit

Permalink
Merge pull request #8 from percolate/add_array_type
Browse files Browse the repository at this point in the history
Add Array Type Support
  • Loading branch information
kevinbirch authored Nov 28, 2017
2 parents 8ecf0e0 + b27c544 commit 93c6aee
Show file tree
Hide file tree
Showing 677 changed files with 185,989 additions and 38 deletions.
8 changes: 7 additions & 1 deletion Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,7 @@
[[constraint]]
name = "github.com/stretchr/testify"
version = "1.1.4"

[[constraint]]
branch = "master"
name = "golang.org/x/tools"
2 changes: 1 addition & 1 deletion VERSION.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.9.1
0.9.2
1 change: 1 addition & 0 deletions golden_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ var (
"Namedvaluer",
"Multireturner",
"Variadic",
"Array",
"Pointer",
"Interfacer",
"Structer",
Expand Down
188 changes: 154 additions & 34 deletions model.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,6 @@ func (r *ImportSet) Add(value *Import) {
}
}

func (r *ImportSet) Remove(ri *Import) {
for index, i := range r.imports {
if i.Name == ri.Name && i.Path == ri.Path {
r.imports = append(r.imports[:index], r.imports[index+1:]...)
return
}
}
}

func (r *ImportSet) Contains(ri *Import) bool {
for _, i := range r.imports {
if i.Name == ri.Name && i.Path == ri.Path {
Expand All @@ -53,10 +44,6 @@ func (r *ImportSet) Contains(ri *Import) bool {
return false
}

func (r *ImportSet) GetAll() []*Import {
return r.imports
}

func (r *ImportSet) GetRequired() []*Import {
result := make([]*Import, 0, len(r.imports))
for _, imp := range r.imports {
Expand Down Expand Up @@ -152,6 +139,24 @@ func unwrap(node ast.Expr, imports *ImportSet) (t Type, err error) {
t = &Ellipsis{
subType: subType,
}
case *ast.ArrayType:
var subType Type
subType, err = unwrap(nodeType.Elt, imports)
if err != nil {
return
}
a := &Array{
subType: subType,
}
if nodeType.Len != nil {
if lit, ok := nodeType.Len.(*ast.BasicLit); ok {
a.scale = lit.Value
} else {
err = fmt.Errorf("internal error: unsupported array len type node: %#v", nodeType.Len)
return
}
}
t = a
case *ast.ChanType:
switch nodeType.Dir {
case ast.SEND:
Expand Down Expand Up @@ -376,107 +381,222 @@ type Type interface {
FieldFormat() string
}

type Array struct {
subType Type
scale string
parameterFormat string
fieldFormat string
}

func (t *Array) ParameterFormat() string {
if t.parameterFormat != "" {
return t.parameterFormat
}

t.parameterFormat = fmt.Sprintf("[%s]%s", t.scale, t.subType.ParameterFormat())

return t.parameterFormat
}

func (t *Array) ReferenceFormat() string {
return t.subType.ReferenceFormat()
}

func (t *Array) FieldFormat() string {
if t.fieldFormat != "" {
return t.fieldFormat
}

t.fieldFormat = fmt.Sprintf("[%s]%s", t.scale, t.subType.FieldFormat())

return t.fieldFormat
}

type Ellipsis struct {
subType Type
subType Type
parameterFormat string
fieldFormat string
}

func (t *Ellipsis) ParameterFormat() string {
return fmt.Sprintf("...%s", t.subType.ParameterFormat())
if t.parameterFormat != "" {
return t.parameterFormat
}

t.parameterFormat = fmt.Sprintf("...%s", t.subType.ParameterFormat())

return t.parameterFormat
}

func (t *Ellipsis) ReferenceFormat() string {
return "..."
}

func (t *Ellipsis) FieldFormat() string {
return fmt.Sprintf("[]%s", t.subType.FieldFormat())
if t.fieldFormat != "" {
return t.fieldFormat
}

t.fieldFormat = fmt.Sprintf("[]%s", t.subType.FieldFormat())

return t.fieldFormat
}

type Channel struct {
subType Type
subType Type
parameterFormat string
fieldFormat string
}

func (t *Channel) ParameterFormat() string {
return fmt.Sprintf("chan %s", t.subType.ParameterFormat())
if t.parameterFormat != "" {
return t.parameterFormat
}

t.parameterFormat = fmt.Sprintf("chan %s", t.subType.ParameterFormat())

return t.parameterFormat
}

func (t *Channel) ReferenceFormat() string {
return t.subType.ReferenceFormat()
}

func (t *Channel) FieldFormat() string {
return fmt.Sprintf("chan %s", t.subType.FieldFormat())
if t.fieldFormat != "" {
return t.fieldFormat
}

t.fieldFormat = fmt.Sprintf("chan %s", t.subType.FieldFormat())

return t.fieldFormat
}

type ReceiveChannel struct {
subType Type
subType Type
parameterFormat string
fieldFormat string
}

func (t *ReceiveChannel) ParameterFormat() string {
return fmt.Sprintf("<-chan %s", t.subType.ParameterFormat())
if t.parameterFormat != "" {
return t.parameterFormat
}

t.parameterFormat = fmt.Sprintf("<-chan %s", t.subType.ParameterFormat())

return t.parameterFormat
}

func (t *ReceiveChannel) ReferenceFormat() string {
return t.subType.ReferenceFormat()
}

func (t *ReceiveChannel) FieldFormat() string {
return fmt.Sprintf("<-chan %s", t.subType.FieldFormat())
if t.fieldFormat != "" {
return t.fieldFormat
}

t.fieldFormat = fmt.Sprintf("<-chan %s", t.subType.FieldFormat())

return t.fieldFormat
}

type SendChannel struct {
subType Type
subType Type
parameterFormat string
fieldFormat string
}

func (t *SendChannel) ParameterFormat() string {
return fmt.Sprintf("chan<- %s", t.subType.ParameterFormat())
if t.parameterFormat != "" {
return t.parameterFormat
}

t.parameterFormat = fmt.Sprintf("chan<- %s", t.subType.ParameterFormat())

return t.parameterFormat
}

func (t *SendChannel) ReferenceFormat() string {
return t.subType.ReferenceFormat()
}

func (t *SendChannel) FieldFormat() string {
return fmt.Sprintf("chan<- %s", t.subType.FieldFormat())
if t.fieldFormat != "" {
return t.fieldFormat
}

t.fieldFormat = fmt.Sprintf("chan<- %s", t.subType.FieldFormat())

return t.fieldFormat
}

type Pointer struct {
subType Type
subType Type
parameterFormat string
fieldFormat string
}

func (t *Pointer) ParameterFormat() string {
return fmt.Sprintf("*%s", t.subType.ParameterFormat())
if t.parameterFormat != "" {
return t.parameterFormat
}

t.parameterFormat = fmt.Sprintf("*%s", t.subType.ParameterFormat())

return t.parameterFormat
}

func (t *Pointer) ReferenceFormat() string {
return t.subType.ReferenceFormat()
}

func (t *Pointer) FieldFormat() string {
return fmt.Sprintf("*%s", t.subType.FieldFormat())
if t.fieldFormat != "" {
return t.fieldFormat
}

t.fieldFormat = fmt.Sprintf("*%s", t.subType.FieldFormat())

return t.fieldFormat
}

type BasicType struct {
Name string
Qualifier string
Name string
Qualifier string
parameterFormat string
fieldFormat string
}

func (t *BasicType) ParameterFormat() string {
if t.parameterFormat != "" {
return t.parameterFormat
}

if t.Qualifier != "" {
return fmt.Sprintf("%s.%s", t.Qualifier, t.Name)
t.parameterFormat = fmt.Sprintf("%s.%s", t.Qualifier, t.Name)
} else {
t.parameterFormat = t.Name
}

return t.Name
return t.parameterFormat
}

func (t *BasicType) ReferenceFormat() string {
return ""
}

func (t *BasicType) FieldFormat() string {
if t.fieldFormat != "" {
return t.fieldFormat
}

if t.Qualifier != "" {
return fmt.Sprintf("%s.%s", t.Qualifier, t.Name)
t.fieldFormat = fmt.Sprintf("%s.%s", t.Qualifier, t.Name)
} else {
t.fieldFormat = t.Name
}

return t.Name
return t.fieldFormat
}
5 changes: 3 additions & 2 deletions template.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package main
import (
"bytes"
"fmt"
"go/format"
"text/template"

"golang.org/x/tools/imports"
)

const charlatanTemplate = `// generated by "{{.CommandLine}}". DO NOT EDIT.
Expand Down Expand Up @@ -253,7 +254,7 @@ func (t *Template) Execute() ([]byte, error) {
return nil, err
}

src, err := format.Source(buf.Bytes())
src, err := imports.Process("", buf.Bytes(), nil)
if err != nil {
// Should not happen except when developing this code.
// The user can compile the output to see the error.
Expand Down
Loading

0 comments on commit 93c6aee

Please sign in to comment.