Skip to content

Commit 85ae7fc

Browse files
authored
add Edition support (#147)
* add Edition support * rm VisitEdition to be backward compat * update hist * add git build * add build badge * build for each pull
1 parent 9f84c9d commit 85ae7fc

File tree

11 files changed

+165
-2
lines changed

11 files changed

+165
-2
lines changed

.github/workflows/go.yml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# This workflow will test a golang project
2+
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go
3+
4+
name: Go
5+
6+
on:
7+
push:
8+
branches: [ "master" ]
9+
pull_request:
10+
branches:
11+
- '*' # matches every branch that doesn't contain a '/'
12+
- '*/*' # matches every branch containing a single '/'
13+
- '**' # matches every branch
14+
- '!master' # excludes master
15+
16+
jobs:
17+
18+
build:
19+
runs-on: ubuntu-latest
20+
steps:
21+
- uses: actions/checkout@v4
22+
23+
- name: Set up Go
24+
uses: actions/setup-go@v4
25+
with:
26+
go-version: '1.23'
27+
28+
- name: Test
29+
run: go test -v -cover ./...

CHANGES.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## v1.14.0 (2024-12-18)
2+
3+
- parse edition element (PR #147, ISSUE #145)
4+
15
## v1.13.4 (2024-12-17)
26

37
- fixed handling identifiers known as numbers by scanner (PR #146)

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# proto
22

3+
[![Go](https://github.com/emicklei/proto/actions/workflows/go.yml/badge.svg)](https://github.com/emicklei/proto/actions/workflows/go.yml)
34
[![Go Report Card](https://goreportcard.com/badge/github.com/emicklei/proto)](https://goreportcard.com/report/github.com/emicklei/proto)
45
[![GoDoc](https://pkg.go.dev/badge/github.com/emicklei/proto)](https://pkg.go.dev/github.com/emicklei/proto)
56
[![codecov](https://codecov.io/gh/emicklei/proto/branch/master/graph/badge.svg)](https://codecov.io/gh/emicklei/proto)

edition.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Copyright (c) 2024 Ernest Micklei
2+
//
3+
// MIT License
4+
//
5+
// Permission is hereby granted, free of charge, to any person obtaining
6+
// a copy of this software and associated documentation files (the
7+
// "Software"), to deal in the Software without restriction, including
8+
// without limitation the rights to use, copy, modify, merge, publish,
9+
// distribute, sublicense, and/or sell copies of the Software, and to
10+
// permit persons to whom the Software is furnished to do so, subject to
11+
// the following conditions:
12+
//
13+
// The above copyright notice and this permission notice shall be
14+
// included in all copies or substantial portions of the Software.
15+
//
16+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17+
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19+
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20+
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21+
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22+
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23+
24+
package proto
25+
26+
import (
27+
"text/scanner"
28+
)
29+
30+
type Edition struct {
31+
Position scanner.Position
32+
Comment *Comment
33+
Value string
34+
InlineComment *Comment
35+
Parent Visitee
36+
}
37+
38+
func (e *Edition) parse(p *Parser) error {
39+
if _, tok, lit := p.next(); tok != tEQUALS {
40+
return p.unexpected(lit, "edition =", e)
41+
}
42+
_, _, lit := p.next()
43+
if !isString(lit) {
44+
return p.unexpected(lit, "edition string constant", e)
45+
}
46+
e.Value, _ = unQuote(lit)
47+
return nil
48+
}
49+
50+
// Accept dispatches the call to the visitor.
51+
func (e *Edition) Accept(v Visitor) {
52+
// v.VisitEdition(e) in v2
53+
}
54+
55+
// Doc is part of Documented
56+
func (e *Edition) Doc() *Comment {
57+
return e.Comment
58+
}
59+
60+
// inlineComment is part of commentInliner.
61+
func (e *Edition) inlineComment(c *Comment) {
62+
e.InlineComment = c
63+
}
64+
65+
func (e *Edition) parent(v Visitee) { e.Parent = v }

edition_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright (c) 2024 Ernest Micklei
2+
//
3+
// MIT License
4+
//
5+
// Permission is hereby granted, free of charge, to any person obtaining
6+
// a copy of this software and associated documentation files (the
7+
// "Software"), to deal in the Software without restriction, including
8+
// without limitation the rights to use, copy, modify, merge, publish,
9+
// distribute, sublicense, and/or sell copies of the Software, and to
10+
// permit persons to whom the Software is furnished to do so, subject to
11+
// the following conditions:
12+
//
13+
// The above copyright notice and this permission notice shall be
14+
// included in all copies or substantial portions of the Software.
15+
//
16+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17+
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19+
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20+
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21+
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22+
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23+
24+
package proto
25+
26+
import "testing"
27+
28+
func TestEdition(t *testing.T) {
29+
proto := `edition = "1967";`
30+
p := newParserOn(proto)
31+
pr, err := p.Parse()
32+
if err != nil {
33+
t.Fatal(err)
34+
}
35+
e := pr.elements()[0].(*Edition)
36+
if got, want := e.Value, "1967"; got != want {
37+
t.Errorf("got [%v] want [%v]", got, want)
38+
}
39+
}

noop_visitor.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323

2424
package proto
2525

26+
var _ Visitor = NoopVisitor{}
27+
2628
// 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.
2729
// It implements the Visitor interface.
2830
type NoopVisitor struct{}
@@ -36,6 +38,9 @@ func (n NoopVisitor) VisitService(v *Service) {}
3638
// VisitSyntax is part of Visitor interface
3739
func (n NoopVisitor) VisitSyntax(s *Syntax) {}
3840

41+
// VisitSyntax is part of Visitor interface
42+
func (n NoopVisitor) VisitEdition(e *Edition) {}
43+
3944
// VisitPackage is part of Visitor interface
4045
func (n NoopVisitor) VisitPackage(p *Package) {}
4146

parent_accessor.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,7 @@ func (p *parentAccessor) VisitGroup(g *Group) {
8585
func (p *parentAccessor) VisitExtensions(e *Extensions) {
8686
p.parent = e.Parent
8787
}
88+
func (p *parentAccessor) VisitEdition(e *Edition) {
89+
p.parent = e.Parent
90+
}
8891
func (p *parentAccessor) VisitProto(*Proto) {}

parent_test.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Copyright (c) 2018 Ernest Micklei
22
//
3-
// MIT License
3+
// # MIT License
44
//
55
// Permission is hereby granted, free of charge, to any person obtaining
66
// a copy of this software and associated documentation files (the
@@ -122,3 +122,8 @@ func (pc *parentChecker) VisitGroup(g *Group) {
122122
func (pc *parentChecker) VisitExtensions(e *Extensions) {
123123
pc.check("Extensions", "", e.Parent)
124124
}
125+
126+
// edition (proto3+)
127+
func (pc *parentChecker) VisitEdition(e *Edition) {
128+
pc.check("Edition", "", e.Parent)
129+
}

proto.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,14 @@ func (proto *Proto) parse(p *Parser) error {
8181
return err
8282
}
8383
proto.addElement(s)
84+
case tEDITION == tok:
85+
s := new(Edition)
86+
s.Position = pos
87+
s.Comment, proto.Elements = takeLastCommentIfEndsOnLine(proto.Elements, pos.Line-1)
88+
if err := s.parse(p); err != nil {
89+
return err
90+
}
91+
proto.addElement(s)
8492
case tIMPORT == tok:
8593
im := new(Import)
8694
im.Position = pos

token.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ const (
6060

6161
// Keywords
6262
keywordsStart
63+
tEDITION
6364
tSYNTAX
6465
tSERVICE
6566
tRPC
@@ -192,6 +193,8 @@ func asToken(literal string) token {
192193
// words
193194
case "syntax":
194195
return tSYNTAX
196+
case "edition":
197+
return tEDITION
195198
case "service":
196199
return tSERVICE
197200
case "rpc":

visitor.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ package proto
2525

2626
// Visitor is for dispatching Proto elements.
2727
type Visitor interface {
28-
//VisitProto(p *Proto)
2928
VisitMessage(m *Message)
3029
VisitService(v *Service)
3130
VisitSyntax(s *Syntax)
@@ -44,6 +43,8 @@ type Visitor interface {
4443
// proto2
4544
VisitGroup(g *Group)
4645
VisitExtensions(e *Extensions)
46+
// edition (proto3+), v2
47+
// VisitEdition(e *Edition)
4748
}
4849

4950
// Visitee is implemented by all Proto elements.

0 commit comments

Comments
 (0)