Skip to content

Commit

Permalink
add Edition support (#147)
Browse files Browse the repository at this point in the history
* add Edition support

* rm VisitEdition to be backward compat

* update hist

* add git build

* add build badge

* build for each pull
  • Loading branch information
emicklei authored Dec 18, 2024
1 parent 9f84c9d commit 85ae7fc
Show file tree
Hide file tree
Showing 11 changed files with 165 additions and 2 deletions.
29 changes: 29 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# This workflow will test a golang project
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go

name: Go

on:
push:
branches: [ "master" ]
pull_request:
branches:
- '*' # matches every branch that doesn't contain a '/'
- '*/*' # matches every branch containing a single '/'
- '**' # matches every branch
- '!master' # excludes master

jobs:

build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: '1.23'

- name: Test
run: go test -v -cover ./...
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## v1.14.0 (2024-12-18)

- parse edition element (PR #147, ISSUE #145)

## v1.13.4 (2024-12-17)

- fixed handling identifiers known as numbers by scanner (PR #146)
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# proto

[![Go](https://github.com/emicklei/proto/actions/workflows/go.yml/badge.svg)](https://github.com/emicklei/proto/actions/workflows/go.yml)
[![Go Report Card](https://goreportcard.com/badge/github.com/emicklei/proto)](https://goreportcard.com/report/github.com/emicklei/proto)
[![GoDoc](https://pkg.go.dev/badge/github.com/emicklei/proto)](https://pkg.go.dev/github.com/emicklei/proto)
[![codecov](https://codecov.io/gh/emicklei/proto/branch/master/graph/badge.svg)](https://codecov.io/gh/emicklei/proto)
Expand Down
65 changes: 65 additions & 0 deletions edition.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright (c) 2024 Ernest Micklei
//
// MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

package proto

import (
"text/scanner"
)

type Edition struct {
Position scanner.Position
Comment *Comment
Value string
InlineComment *Comment
Parent Visitee
}

func (e *Edition) parse(p *Parser) error {
if _, tok, lit := p.next(); tok != tEQUALS {
return p.unexpected(lit, "edition =", e)
}
_, _, lit := p.next()
if !isString(lit) {
return p.unexpected(lit, "edition string constant", e)
}
e.Value, _ = unQuote(lit)
return nil
}

// Accept dispatches the call to the visitor.
func (e *Edition) Accept(v Visitor) {
// v.VisitEdition(e) in v2
}

// Doc is part of Documented
func (e *Edition) Doc() *Comment {
return e.Comment
}

// inlineComment is part of commentInliner.
func (e *Edition) inlineComment(c *Comment) {
e.InlineComment = c
}

func (e *Edition) parent(v Visitee) { e.Parent = v }
39 changes: 39 additions & 0 deletions edition_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright (c) 2024 Ernest Micklei
//
// MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

package proto

import "testing"

func TestEdition(t *testing.T) {
proto := `edition = "1967";`
p := newParserOn(proto)
pr, err := p.Parse()
if err != nil {
t.Fatal(err)
}
e := pr.elements()[0].(*Edition)
if got, want := e.Value, "1967"; got != want {
t.Errorf("got [%v] want [%v]", got, want)
}
}
5 changes: 5 additions & 0 deletions noop_visitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

package proto

var _ Visitor = NoopVisitor{}

// NoopVisitor is a no-operation visitor that can be used when creating your own visitor that is interested in only one or a few types.
// It implements the Visitor interface.
type NoopVisitor struct{}
Expand All @@ -36,6 +38,9 @@ func (n NoopVisitor) VisitService(v *Service) {}
// VisitSyntax is part of Visitor interface
func (n NoopVisitor) VisitSyntax(s *Syntax) {}

// VisitSyntax is part of Visitor interface
func (n NoopVisitor) VisitEdition(e *Edition) {}

// VisitPackage is part of Visitor interface
func (n NoopVisitor) VisitPackage(p *Package) {}

Expand Down
3 changes: 3 additions & 0 deletions parent_accessor.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,7 @@ func (p *parentAccessor) VisitGroup(g *Group) {
func (p *parentAccessor) VisitExtensions(e *Extensions) {
p.parent = e.Parent
}
func (p *parentAccessor) VisitEdition(e *Edition) {
p.parent = e.Parent
}
func (p *parentAccessor) VisitProto(*Proto) {}
7 changes: 6 additions & 1 deletion parent_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright (c) 2018 Ernest Micklei
//
// MIT License
// # MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
Expand Down Expand Up @@ -122,3 +122,8 @@ func (pc *parentChecker) VisitGroup(g *Group) {
func (pc *parentChecker) VisitExtensions(e *Extensions) {
pc.check("Extensions", "", e.Parent)
}

// edition (proto3+)
func (pc *parentChecker) VisitEdition(e *Edition) {
pc.check("Edition", "", e.Parent)
}
8 changes: 8 additions & 0 deletions proto.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,14 @@ func (proto *Proto) parse(p *Parser) error {
return err
}
proto.addElement(s)
case tEDITION == tok:
s := new(Edition)
s.Position = pos
s.Comment, proto.Elements = takeLastCommentIfEndsOnLine(proto.Elements, pos.Line-1)
if err := s.parse(p); err != nil {
return err
}
proto.addElement(s)
case tIMPORT == tok:
im := new(Import)
im.Position = pos
Expand Down
3 changes: 3 additions & 0 deletions token.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ const (

// Keywords
keywordsStart
tEDITION
tSYNTAX
tSERVICE
tRPC
Expand Down Expand Up @@ -192,6 +193,8 @@ func asToken(literal string) token {
// words
case "syntax":
return tSYNTAX
case "edition":
return tEDITION
case "service":
return tSERVICE
case "rpc":
Expand Down
3 changes: 2 additions & 1 deletion visitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ package proto

// Visitor is for dispatching Proto elements.
type Visitor interface {
//VisitProto(p *Proto)
VisitMessage(m *Message)
VisitService(v *Service)
VisitSyntax(s *Syntax)
Expand All @@ -44,6 +43,8 @@ type Visitor interface {
// proto2
VisitGroup(g *Group)
VisitExtensions(e *Extensions)
// edition (proto3+), v2
// VisitEdition(e *Edition)
}

// Visitee is implemented by all Proto elements.
Expand Down

0 comments on commit 85ae7fc

Please sign in to comment.