From 9dc4659319a81e9221a46a3966de921bcff3310e Mon Sep 17 00:00:00 2001 From: Aleksandr Razumov Date: Fri, 20 Nov 2020 02:16:25 +0300 Subject: [PATCH] all: don't use golang.org/x/xerrors Let's stick to stdlib for now. --- annotation.go | 18 +++++++++--------- definition.go | 21 ++++++++++----------- flag.go | 8 +++----- go.mod | 1 - go.sum | 2 -- parameter.go | 8 +++----- schema.go | 11 +++++------ type.go | 15 +++++++-------- 8 files changed, 37 insertions(+), 47 deletions(-) diff --git a/annotation.go b/annotation.go index b066c3b..e7a6822 100644 --- a/annotation.go +++ b/annotation.go @@ -1,10 +1,10 @@ package tl import ( + "errors" + "fmt" "strings" "unicode" - - "golang.org/x/xerrors" ) // Common values for Annotation.Name. @@ -59,14 +59,14 @@ 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. @@ -74,11 +74,11 @@ func parseAnnotation(line string) ([]Annotation, error) { 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:] @@ -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, @@ -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, diff --git a/definition.go b/definition.go index 3eb471a..b7257ad 100644 --- a/definition.go +++ b/definition.go @@ -1,13 +1,12 @@ package tl import ( + "errors" "fmt" "hash/crc32" "strconv" "strings" "unicode" - - "golang.org/x/xerrors" ) // Definition represents "Type Language" definition. @@ -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` @@ -63,10 +62,10 @@ 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. @@ -74,14 +73,14 @@ func (d *Definition) Parse(line string) error { 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 { @@ -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) } } } @@ -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: @@ -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 } diff --git a/flag.go b/flag.go index e3007f3..1b2c773 100644 --- a/flag.go +++ b/flag.go @@ -4,8 +4,6 @@ import ( "fmt" "strconv" "strings" - - "golang.org/x/xerrors" ) // Flag describes conditional parameter. @@ -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 diff --git a/go.mod b/go.mod index 0a8dc07..6be68f0 100644 --- a/go.mod +++ b/go.mod @@ -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 ) diff --git a/go.sum b/go.sum index dc2c716..a3c5116 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/parameter.go b/parameter.go index 04f8b00..bf31dd4 100644 --- a/parameter.go +++ b/parameter.go @@ -3,8 +3,6 @@ package tl import ( "fmt" "strings" - - "golang.org/x/xerrors" ) // Parameter with Name and Type. @@ -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] @@ -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 } diff --git a/schema.go b/schema.go index 3371925..63fd66a 100644 --- a/schema.go +++ b/schema.go @@ -2,10 +2,9 @@ package tl import ( "bufio" + "fmt" "io" "strings" - - "golang.org/x/xerrors" ) // SchemaDefinition is annotated Definition with Category. @@ -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. @@ -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. @@ -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) } } @@ -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. diff --git a/type.go b/type.go index b4f6da1..80ce87c 100644 --- a/type.go +++ b/type.go @@ -1,10 +1,9 @@ package tl import ( + "errors" "fmt" "strings" - - "golang.org/x/xerrors" ) // Type of a Definition or a Parameter. @@ -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 @@ -46,11 +45,11 @@ func (p *Type) Parse(s string) error { // Parse `type`. 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] } @@ -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) } }