Skip to content

Commit

Permalink
all: don't use golang.org/x/xerrors
Browse files Browse the repository at this point in the history
Let's stick to stdlib for now.
  • Loading branch information
ernado committed Nov 19, 2020
1 parent 6c7e1f3 commit 9dc4659
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 47 deletions.
18 changes: 9 additions & 9 deletions annotation.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package tl

import (
"errors"
"fmt"
"strings"
"unicode"

"golang.org/x/xerrors"
)

// Common values for Annotation.Name.
Expand Down Expand Up @@ -59,26 +59,26 @@ func singleLineAnnotations(a []Annotation) string {
// parseAnnotation parses one or multiple annotations on the line.
func parseAnnotation(line string) ([]Annotation, error) {
if !strings.HasPrefix(line, "//") {
return nil, xerrors.New("annotation should be comment")
return nil, errors.New("annotation should be comment")
}
line = strings.TrimSpace(strings.TrimLeft(line, "/"))
if line == "" {
return nil, xerrors.New("blank comment")
return nil, errors.New("blank comment")
}
if !strings.HasPrefix(line, "@") {
return nil, xerrors.New("invalid annotation start")
return nil, errors.New("invalid annotation start")
}

// Probably this can be simplified.
var annotations []Annotation
for line != "" {
nameEnd := strings.Index(line, " ")
if nameEnd <= 1 {
return nil, xerrors.New("failed to find name end")
return nil, errors.New("failed to find name end")
}
name := line[1:nameEnd]
if !isValidName(name) {
return nil, xerrors.New("invalid annotation name")
return nil, errors.New("invalid annotation name")
}

line = line[nameEnd:]
Expand All @@ -87,7 +87,7 @@ func parseAnnotation(line string) ([]Annotation, error) {
// No more annotations.
value := strings.TrimSpace(line)
if !isValidAnnotationValue(value) {
return nil, xerrors.Errorf("invalid annotation value %q", value)
return nil, fmt.Errorf("invalid annotation value %q", value)
}
annotations = append(annotations, Annotation{
Name: name,
Expand All @@ -99,7 +99,7 @@ func parseAnnotation(line string) ([]Annotation, error) {
// There will be more.
value := strings.TrimSpace(line[:nextAnnotationPos])
if !isValidAnnotationValue(value) {
return nil, xerrors.Errorf("invalid annotation value %q", value)
return nil, fmt.Errorf("invalid annotation value %q", value)
}
annotations = append(annotations, Annotation{
Name: name,
Expand Down
21 changes: 10 additions & 11 deletions definition.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package tl

import (
"errors"
"fmt"
"hash/crc32"
"strconv"
"strings"
"unicode"

"golang.org/x/xerrors"
)

// Definition represents "Type Language" definition.
Expand Down Expand Up @@ -52,7 +51,7 @@ func (d *Definition) Parse(line string) error {
line = strings.TrimRight(line, ";")
parts := strings.Split(line, "=")
if len(parts) != 2 {
return xerrors.New("unexpected definition elements")
return errors.New("unexpected definition elements")
}
// Splitting definition line into left and right parts.
// Example: `foo#123 code:int name:string = Message`
Expand All @@ -63,25 +62,25 @@ func (d *Definition) Parse(line string) error {
leftParts = strings.Split(left, " ")
)
if left == "" || right == "" {
return xerrors.New("definition part is blank")
return errors.New("definition part is blank")
}
if err := d.Type.Parse(right); err != nil {
return xerrors.Errorf("failed to parse type: %w", err)
return fmt.Errorf("failed to parse type: %w", err)
}
{
// Parsing definition name and id.
first := leftParts[0]
nameParts := strings.SplitN(first, tokID, 2)
d.Name = nameParts[0]
if d.Name == "" {
return xerrors.New("blank name")
return errors.New("blank name")
}
if len(nameParts) > 1 {
// Parsing definition id as hex to uint32.
idHex := nameParts[1]
id, err := strconv.ParseUint(idHex, 16, 32)
if err != nil {
return xerrors.Errorf("%s is invalid id: %w", idHex, err)
return fmt.Errorf("%s is invalid id: %w", idHex, err)
}
d.ID = uint32(id)
} else {
Expand All @@ -95,7 +94,7 @@ func (d *Definition) Parse(line string) error {
}
for _, ns := range d.Namespace {
if !isValidName(ns) {
return xerrors.Errorf("invalid namespace part %q", ns)
return fmt.Errorf("invalid namespace part %q", ns)
}
}
}
Expand All @@ -109,7 +108,7 @@ func (d *Definition) Parse(line string) error {
}
var param Parameter
if err := param.Parse(f); err != nil {
return xerrors.Errorf("failed to parse param: %w", err)
return fmt.Errorf("failed to parse param: %w", err)
}
// Handling generics.
// Example:
Expand All @@ -125,13 +124,13 @@ func (d *Definition) Parse(line string) error {
// E.g. `t#1 {Y:Type} x:!X = X;` is invalid, because X was not defined.
if param.Type.GenericRef {
if _, ok := genericParams[param.Type.Name]; !ok {
return xerrors.Errorf("undefined generic parameter type %s", param.Type.Name)
return fmt.Errorf("undefined generic parameter type %s", param.Type.Name)
}
}
d.Params = append(d.Params, param)
}
if !isValidName(d.Name) {
return xerrors.Errorf("invalid name %q", d.Name)
return fmt.Errorf("invalid name %q", d.Name)
}
return nil
}
Expand Down
8 changes: 3 additions & 5 deletions flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"fmt"
"strconv"
"strings"

"golang.org/x/xerrors"
)

// Flag describes conditional parameter.
Expand All @@ -23,15 +21,15 @@ func (f Flag) String() string {
func (f *Flag) Parse(s string) error {
pos := strings.Index(s, ".")
if pos < 1 {
return xerrors.New("bad flag")
return fmt.Errorf("bad flag %q", s)
}
f.Name = s[:pos]
if !isValidName(f.Name) {
return xerrors.Errorf("name %q is invalid", f.Name)
return fmt.Errorf("name %q is invalid", f.Name)
}
idx, err := strconv.Atoi(s[pos+1:])
if err != nil {
return xerrors.New("bad index")
return fmt.Errorf("bad index: %w", err)
}
f.Index = idx
return nil
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,4 @@ go 1.15
require (
github.com/sebdah/goldie/v2 v2.5.3
github.com/stretchr/testify v1.6.1
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1
)
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
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=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
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=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
Expand Down
8 changes: 3 additions & 5 deletions parameter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package tl
import (
"fmt"
"strings"

"golang.org/x/xerrors"
)

// Parameter with Name and Type.
Expand Down Expand Up @@ -58,7 +56,7 @@ func (p Parameter) String() string {
func (p *Parameter) Parse(s string) error {
if strings.HasPrefix(s, "{") {
if !strings.HasSuffix(s, ":Type}") {
return xerrors.Errorf("unexpected generic %s", s)
return fmt.Errorf("unexpected generic %s", s)
}
p.typeDefinition = true
p.Name = strings.SplitN(s[1:], ":", 2)[0]
Expand All @@ -80,12 +78,12 @@ func (p *Parameter) Parse(s string) error {
if pos := strings.Index(s, "?"); pos >= 0 {
p.Flag = &Flag{}
if err := p.Flag.Parse(s[:pos]); err != nil {
return xerrors.Errorf("failed to parse flag: %w", err)
return fmt.Errorf("failed to parse flag: %w", err)
}
s = s[pos+1:]
}
if err := p.Type.Parse(s); err != nil {
return xerrors.Errorf("failed to parse type: %w", err)
return fmt.Errorf("failed to parse type: %w", err)
}
return nil
}
Expand Down
11 changes: 5 additions & 6 deletions schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ package tl

import (
"bufio"
"fmt"
"io"
"strings"

"golang.org/x/xerrors"
)

// SchemaDefinition is annotated Definition with Category.
Expand Down Expand Up @@ -120,7 +119,7 @@ func Parse(reader io.Reader) (*Schema, error) {
// Found annotation.
ann, err := parseAnnotation(s)
if err != nil {
return nil, xerrors.Errorf("failed to parse line %d: %w", line, err)
return nil, fmt.Errorf("failed to parse line %d: %w", line, err)
}
if strings.HasPrefix(s, "//@"+AnnotationClass) {
// Handling class annotation as special case.
Expand Down Expand Up @@ -150,7 +149,7 @@ func Parse(reader io.Reader) (*Schema, error) {
// Type definition started.
def.Category = category
if err := def.Definition.Parse(s); err != nil {
return nil, xerrors.Errorf("failed to parse line %d: definition: %w", line, err)
return nil, fmt.Errorf("failed to parse line %d: definition: %w", line, err)
}

// Validating annotations.
Expand All @@ -171,7 +170,7 @@ func Parse(reader io.Reader) (*Schema, error) {
if _, ok := paramExist[searchFor]; !ok {
// Probably such errors can be just skipped, but seems like it
// is OK to consider this as hard failure.
return nil, xerrors.Errorf("failed to parse line %d: "+
return nil, fmt.Errorf("failed to parse line %d: "+
"can't find param for annotation %q", line, ann.Name)
}
}
Expand All @@ -180,7 +179,7 @@ func Parse(reader io.Reader) (*Schema, error) {
def = SchemaDefinition{} // reset definition
}
if err := scanner.Err(); err != nil {
return nil, xerrors.Errorf("failed to scan: %w", err)
return nil, fmt.Errorf("failed to scan: %w", err)
}

// Remaining type.
Expand Down
15 changes: 7 additions & 8 deletions type.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package tl

import (
"errors"
"fmt"
"strings"

"golang.org/x/xerrors"
)

// Type of a Definition or a Parameter.
Expand Down Expand Up @@ -36,7 +35,7 @@ func (p Type) String() string {

func (p *Type) Parse(s string) error {
if strings.HasPrefix(s, ".") {
return xerrors.New("type can't start with dot")
return errors.New("type can't start with dot")
}
if strings.HasPrefix(s, "!") {
p.GenericRef = true
Expand All @@ -46,11 +45,11 @@ func (p *Type) Parse(s string) error {
// Parse `type<generic_arg>`.
if pos := strings.Index(s, "<"); pos >= 0 {
if !strings.HasSuffix(s, ">") {
return xerrors.New("invalid generic")
return errors.New("invalid generic")
}
p.GenericArg = &Type{}
if err := p.GenericArg.Parse(s[pos+1 : len(s)-1]); err != nil {
return xerrors.Errorf("failed to parse generic: %w", err)
return fmt.Errorf("failed to parse generic: %w", err)
}
s = s[:pos]
}
Expand All @@ -64,14 +63,14 @@ func (p *Type) Parse(s string) error {
p.Namespace = ns[:len(ns)-1]
}
if p.Name == "" {
return xerrors.New("blank name")
return errors.New("blank name")
}
if !isValidName(p.Name) {
return xerrors.Errorf("invalid name %q", p.Name)
return fmt.Errorf("invalid name %q", p.Name)
}
for _, ns := range p.Namespace {
if !isValidName(ns) {
return xerrors.Errorf("invalid namespace part %q", ns)
return fmt.Errorf("invalid namespace part %q", ns)
}
}

Expand Down

0 comments on commit 9dc4659

Please sign in to comment.