-
Notifications
You must be signed in to change notification settings - Fork 168
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor protoparse, fixes multiple issues #316
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,18 @@ | ||
package internal | ||
|
||
import ( | ||
"math" | ||
"unicode" | ||
"unicode/utf8" | ||
) | ||
|
||
const ( | ||
// MaxTag is the maximum allowed tag number for a field. | ||
MaxTag = 536870911 // 2^29 - 1 | ||
// MaxNormalTag is the maximum allowed tag number for a field in a normal message. | ||
MaxNormalTag = 536870911 // 2^29 - 1 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: I might switch this from MaxNormalTag/MaxTag to MaxTag/MaxTagMessageSetWireFormat since the message set max tag is the edge case, but more a matter of preference There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wanted a name like Maybe There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yea that works! Or keep as is, either way :-) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In addition to adding |
||
|
||
// MaxTag is the maximum allowed tag number. Only messages with messageset wire | ||
// encoding can have tags this high. Normal messages use MaxNormalTag as the limit. | ||
MaxTag = math.MaxInt32 - 1 | ||
|
||
// SpecialReservedStart is the first tag in a range that is reserved and not | ||
// allowed for use in message definitions. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -373,33 +373,65 @@ func (n *compoundStringNode) value() interface{} { | |
return n.val | ||
} | ||
|
||
type intLiteral interface { | ||
asInt32(min, max int32) (int32, bool) | ||
value() interface{} | ||
} | ||
|
||
type intLiteralNode struct { | ||
basicNode | ||
val uint64 | ||
} | ||
|
||
var _ intLiteral = (*intLiteralNode)(nil) | ||
|
||
func (n *intLiteralNode) value() interface{} { | ||
return n.val | ||
} | ||
|
||
func (n *intLiteralNode) asInt32(min, max int32) (int32, bool) { | ||
if (min >= 0 && n.val < uint64(min)) || n.val > uint64(max) { | ||
return 0, false | ||
} | ||
return int32(n.val), true | ||
} | ||
|
||
type compoundUintNode struct { | ||
basicCompositeNode | ||
val uint64 | ||
} | ||
|
||
var _ intLiteral = (*compoundUintNode)(nil) | ||
|
||
func (n *compoundUintNode) value() interface{} { | ||
return n.val | ||
} | ||
|
||
func (n *compoundUintNode) asInt32(min, max int32) (int32, bool) { | ||
if (min >= 0 && n.val < uint64(min)) || n.val > uint64(max) { | ||
return 0, false | ||
} | ||
return int32(n.val), true | ||
} | ||
|
||
type compoundIntNode struct { | ||
basicCompositeNode | ||
val int64 | ||
} | ||
|
||
var _ intLiteral = (*compoundIntNode)(nil) | ||
|
||
func (n *compoundIntNode) value() interface{} { | ||
return n.val | ||
} | ||
|
||
func (n *compoundIntNode) asInt32(min, max int32) (int32, bool) { | ||
if n.val < int64(min) || n.val > int64(max) { | ||
return 0, false | ||
} | ||
return int32(n.val), true | ||
} | ||
|
||
type floatLiteralNode struct { | ||
basicNode | ||
val float64 | ||
|
@@ -729,17 +761,46 @@ type extensionRangeNode struct { | |
type rangeNode struct { | ||
basicCompositeNode | ||
stNode, enNode node | ||
st, en int32 | ||
enMax bool | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I'm not sure why I used There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. renamed |
||
} | ||
|
||
func (n *rangeNode) rangeStart() node { | ||
return n.stNode | ||
} | ||
|
||
func (n *rangeNode) rangeEnd() node { | ||
if n.enNode == nil { | ||
return n.stNode | ||
} | ||
return n.enNode | ||
} | ||
|
||
func (n *rangeNode) startValue() interface{} { | ||
return n.stNode.(intLiteral).value() | ||
} | ||
|
||
func (n *rangeNode) startValueAsInt32(min, max int32) (int32, bool) { | ||
return n.stNode.(intLiteral).asInt32(min, max) | ||
} | ||
|
||
func (n *rangeNode) endValue() interface{} { | ||
l, ok := n.enNode.(intLiteral) | ||
if !ok { | ||
return nil | ||
} | ||
return l.value() | ||
} | ||
|
||
func (n *rangeNode) endValueAsInt32(min, max int32) (int32, bool) { | ||
if n.enMax { | ||
return max, true | ||
} | ||
if n.enNode == nil { | ||
return n.startValueAsInt32(min, max) | ||
} | ||
return n.enNode.(intLiteral).asInt32(min, max) | ||
} | ||
|
||
type reservedNode struct { | ||
basicCompositeNode | ||
ranges []*rangeNode | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you want to enforce the
isMessageSet
case as well here? I'm sure you've thought of this and there's likely a reason you're not, but just checking.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The code already validates that tags are never >
internal.MaxTag
when fields are created. This is a deferred check just for the tighter range imposed on non-messageset fields, which can't be performed until we know the field's context (e.g. is it part of a messageset or not).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah ok
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I changed this to just use
internal.GetMaxTag
so it's simpler to follow along