Skip to content

Commit

Permalink
Merge pull request #8 from gotd/bare-vector
Browse files Browse the repository at this point in the history
Parse percent as bare type
  • Loading branch information
ernado authored Mar 16, 2021
2 parents 1eab139 + d0fec00 commit f89f814
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 11 deletions.
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sebdah/goldie/v2 v2.5.3 h1:9ES/mNN+HNUbNWpVAlrzuZ7jE+Nrczbj8uFRjM7624Y=
github.com/sebdah/goldie/v2 v2.5.3/go.mod h1:oZ9fp0+se1eapSRjfYbsV/0Hqhbuu3bJVvKI/NNtssI=
github.com/sergi/go-diff v1.0.0 h1:Kpca3qRNrduNnOQeazBd0ysaKrUJiIuISHxogkT9RPQ=
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
28 changes: 20 additions & 8 deletions type.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,33 +11,45 @@ type Type struct {
Namespace []string `json:"namespace,omitempty"` // namespace components of the type
Name string `json:"name,omitempty"` // the name of the type
Bare bool `json:"bare,omitempty"` // whether this type is bare or boxed
Percent bool `json:"-"` // whether this type has percent in name (like %Message)
GenericRef bool `json:"generic_ref,omitempty"` // whether the type name refers to a generic definition
GenericArg *Type `json:"generic_arg,omitempty"` // generic arguments of the type
}

func (p Type) String() string {
var b strings.Builder
if p.Percent {
b.WriteByte('%')
}
if p.GenericRef {
b.WriteRune('!')
b.WriteByte('!')
}
for _, ns := range p.Namespace {
b.WriteString(ns)
b.WriteRune('.')
b.WriteByte('.')
}
b.WriteString(p.Name)
if p.GenericArg != nil {
b.WriteRune('<')
b.WriteByte('<')
b.WriteString(p.GenericArg.String())
b.WriteRune('>')
b.WriteByte('>')
}
return b.String()
}

func (p *Type) Parse(s string) error {
if strings.HasPrefix(s, ".") {
return errors.New("type can't start with dot")
if len(s) < 1 {
return errors.New("got empty string")
}
if strings.HasPrefix(s, "!") {

switch s[0] {
case '.':
return errors.New("type can't start with dot")
case '%':
p.Bare = true
p.Percent = true
s = s[1:]
case '!':
p.GenericRef = true
s = s[1:]
}
Expand Down Expand Up @@ -75,7 +87,7 @@ func (p *Type) Parse(s string) error {
}

// Bare types starts from lowercase.
if len(p.Name) > 0 {
if len(p.Name) > 0 && !p.Percent {
p.Bare = p.Name[0:1] == strings.ToLower(p.Name[0:1])
}
return nil
Expand Down
17 changes: 14 additions & 3 deletions type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,19 @@ func TestParameterType(t *testing.T) {
},
String: "basic.Vec<generic.T>",
},
{
Type: Type{
Name: "vector",
GenericArg: &Type{
Name: "T",
Bare: true,
Percent: true,
Namespace: []string{"generic"},
},
Bare: true,
},
String: "vector<%generic.T>",
},
{
Type: Type{
Name: "X",
Expand All @@ -49,9 +62,7 @@ func TestParameterType(t *testing.T) {
} {
t.Run(tt.String, func(t *testing.T) {
t.Run("String", func(t *testing.T) {
if v := tt.Type.String(); v != tt.String {
t.Errorf("(%s).String = %s", tt.String, v)
}
require.Equal(t, tt.String, tt.Type.String())
})
t.Run("Parse", func(t *testing.T) {
var result Type
Expand Down

0 comments on commit f89f814

Please sign in to comment.