Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: pacedotdev/oto
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v0.9.1
Choose a base ref
...
head repository: pacedotdev/oto
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v0.10.0
Choose a head ref
  • 3 commits
  • 3 files changed
  • 2 contributors

Commits on Aug 13, 2020

  1. ignored dist folder

    matryer committed Aug 13, 2020
    Copy the full SHA
    480c690 View commit details
  2. exported sentinal error

    matryer committed Aug 13, 2020
    Copy the full SHA
    8dc7cef View commit details

Commits on Aug 19, 2020

  1. Copy the full SHA
    472f4fd View commit details
Showing with 15 additions and 9 deletions.
  1. +1 −0 .gitignore
  2. +13 −9 parser/parser.go
  3. +1 −0 parser/parser_test.go
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -14,3 +14,4 @@
# binary
oto
.DS_Store
dist
22 changes: 13 additions & 9 deletions parser/parser.go
Original file line number Diff line number Diff line change
@@ -17,7 +17,8 @@ import (
"golang.org/x/tools/go/packages"
)

var errNotFound = errors.New("not found")
// ErrNotFound is returned when an Object is not found.
var ErrNotFound = errors.New("not found")

// Definition describes an Oto definition.
type Definition struct {
@@ -32,7 +33,7 @@ type Definition struct {
Imports map[string]string `json:"imports"`
}

// Object looks up an object by name. Returns errNotFound error
// Object looks up an object by name. Returns ErrNotFound error
// if it cannot find it.
func (d *Definition) Object(name string) (*Object, error) {
for i := range d.Objects {
@@ -41,7 +42,7 @@ func (d *Definition) Object(name string) (*Object, error) {
return obj, nil
}
}
return nil, errNotFound
return nil, ErrNotFound
}

// Service describes a service, akin to an interface in Go.
@@ -214,7 +215,7 @@ func (p *Parser) parseService(pkg *packages.Package, obj types.Object, interface
s.Name = obj.Name()
s.Comment = p.commentForType(s.Name)
var err error
s.Metadata, s.Comment, err = extractCommentMetadata(s.Comment)
s.Metadata, s.Comment, err = p.extractCommentMetadata(s.Comment)
if err != nil {
return s, p.wrapErr(errors.New("extract comment metadata"), pkg, obj.Pos())
}
@@ -239,7 +240,7 @@ func (p *Parser) parseMethod(pkg *packages.Package, serviceName string, methodTy
m.NameLowerCamel = camelizeDown(m.Name)
m.Comment = p.commentForMethod(serviceName, m.Name)
var err error
m.Metadata, m.Comment, err = extractCommentMetadata(m.Comment)
m.Metadata, m.Comment, err = p.extractCommentMetadata(m.Comment)
if err != nil {
return m, p.wrapErr(errors.New("extract comment metadata"), pkg, methodType.Pos())
}
@@ -270,7 +271,7 @@ func (p *Parser) parseObject(pkg *packages.Package, o types.Object, v *types.Str
obj.Name = o.Name()
obj.Comment = p.commentForType(obj.Name)
var err error
obj.Metadata, obj.Comment, err = extractCommentMetadata(obj.Comment)
obj.Metadata, obj.Comment, err = p.extractCommentMetadata(obj.Comment)
if err != nil {
return p.wrapErr(errors.New("extract comment metadata"), pkg, o.Pos())
}
@@ -328,7 +329,7 @@ func (p *Parser) parseField(pkg *packages.Package, objectName string, v *types.V
return f, p.wrapErr(errors.New(f.Name+" must be exported"), pkg, v.Pos())
}
var err error
f.Metadata, f.Comment, err = extractCommentMetadata(f.Comment)
f.Metadata, f.Comment, err = p.extractCommentMetadata(f.Comment)
if err != nil {
return f, p.wrapErr(errors.New("extract comment metadata"), pkg, v.Pos())
}
@@ -521,7 +522,7 @@ var metadataCommentRegex = regexp.MustCompile(`^.*:.*`)
// It returns a map of metadata, and the
// remaining comment string.
// Metadata fields should succeed the comment string.
func extractCommentMetadata(comment string) (map[string]interface{}, string, error) {
func (p *Parser) extractCommentMetadata(comment string) (map[string]interface{}, string, error) {
var lines []string
var metadata = make(map[string]interface{})
s := bufio.NewScanner(strings.NewReader(comment))
@@ -539,7 +540,10 @@ func extractCommentMetadata(comment string) (map[string]interface{}, string, err
value := strings.TrimSpace(splitLine[1])
var val interface{}
if err := json.Unmarshal([]byte(value), &val); err != nil {
return nil, "", err
if p.Verbose {
fmt.Printf("(skipping) failed to marshal JSON value (%s): %s", err, value)
}
continue
}
metadata[key] = val
continue
1 change: 1 addition & 0 deletions parser/parser_test.go
Original file line number Diff line number Diff line change
@@ -173,6 +173,7 @@ func TestExtractCommentMetadata(t *testing.T) {
example: "With an example"
required: true
monkey: 24
Kind is one of: monthly, weekly, tags-monthly, tags-weekly
`)
is.NoErr(err)
is.Equal(comment, "This is a comment")