From a9fcc3229a399762512ee4397798cd64b1553c05 Mon Sep 17 00:00:00 2001 From: Gabriel Levine Date: Thu, 14 Jan 2016 10:36:38 -0500 Subject: [PATCH] Enable golint for influxql package. --- CHANGELOG.md | 3 +- influxql/ast.go | 18 +++++++----- influxql/parser.go | 10 +++---- influxql/result.go | 5 +++- influxql/scanner.go | 6 ++-- influxql/scanner_test.go | 16 +++++----- influxql/token.go | 63 ++++++++++++++++++++-------------------- 7 files changed, 66 insertions(+), 55 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 49af4216d00..80a4c967d87 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,7 @@ - [#5541](https://github.com/influxdata/influxdb/pull/5541): Client: Support for adding custom TLS Config for HTTP client. - [#4299](https://github.com/influxdata/influxdb/pull/4299): Client: Reject uint64 Client.Point.Field values. Thanks @arussellsaw -- [#5550](https://github.com/influxdata/influxdb/pull/5550): Enabled golint for tsdb/engine/wal. +- [#5550](https://github.com/influxdata/influxdb/pull/5550): Enabled golint for tsdb/engine/wal. @gabelev - [#5419](https://github.com/influxdata/influxdb/pull/5419): Graphite: Support matching tags multiple times Thanks @m4ce - [#5598](https://github.com/influxdata/influxdb/pull/5598): Client: Add Ping to v2 client @PSUdaemon - [#4125](https://github.com/influxdata/influxdb/pull/4125): Admin UI: Fetch and display server version on connect. Thanks @alexiri! @@ -12,6 +12,7 @@ - [#5562](https://github.com/influxdata/influxdb/pull/5562): Graphite: Support matching fields multiple times (@chrusty) - [#5666](https://github.com/influxdata/influxdb/pull/5666): Manage dependencies with gdm - [#5512](https://github.com/influxdata/influxdb/pull/5512): HTTP: Add config option to enable HTTP JSON write path which is now disabled by default. +- [#5336](https://github.com/influxdata/influxdb/pull/5366): Enabled golint for influxql. @gabelev ### Bugfixes diff --git a/influxql/ast.go b/influxql/ast.go index 6cca54403eb..c07c07475d5 100644 --- a/influxql/ast.go +++ b/influxql/ast.go @@ -749,6 +749,7 @@ func (s *AlterRetentionPolicyStatement) RequiredPrivileges() ExecutionPrivileges return ExecutionPrivileges{{Admin: true, Name: "", Privilege: AllPrivileges}} } +// FillOption represents different options for aggregate windows. type FillOption int const ( @@ -1406,6 +1407,7 @@ func (s *SelectStatement) validateAggregates(tr targetRequirement) error { return nil } +// HasDistinct checks if a select statement contains DISTINCT func (s *SelectStatement) HasDistinct() bool { // determine if we have a call named distinct for _, f := range s.Fields { @@ -1443,6 +1445,7 @@ func (s *SelectStatement) validateDistinct() error { return nil } +// HasCountDistinct checks if a select statement contains COUNT and DISTINCT func (s *SelectStatement) HasCountDistinct() bool { for _, f := range s.Fields { if c, ok := f.Expr.(*Call); ok { @@ -1539,7 +1542,7 @@ func (s *SelectStatement) validateDerivative() error { return nil } -// GroupByIterval extracts the time interval, if specified. +// GroupByInterval extracts the time interval, if specified. func (s *SelectStatement) GroupByInterval() (time.Duration, error) { // return if we've already pulled it out if s.groupByInterval != 0 { @@ -2262,7 +2265,7 @@ func (s *ShowRetentionPoliciesStatement) RequiredPrivileges() ExecutionPrivilege return ExecutionPrivileges{{Admin: false, Name: "", Privilege: ReadPrivilege}} } -// ShowStats statement displays statistics for a given module. +// ShowStatsStatement displays statistics for a given module. type ShowStatsStatement struct { // Module Module string @@ -2658,9 +2661,9 @@ func (f *Field) String() string { } // Sort Interface for Fields -func (f Fields) Len() int { return len(f) } -func (f Fields) Less(i, j int) bool { return f[i].Name() < f[j].Name() } -func (f Fields) Swap(i, j int) { f[i], f[j] = f[j], f[i] } +func (a Fields) Len() int { return len(a) } +func (a Fields) Less(i, j int) bool { return a[i].Name() < a[j].Name() } +func (a Fields) Swap(i, j int) { a[i], a[j] = a[j], a[i] } // Dimensions represents a list of dimensions. type Dimensions []*Dimension @@ -3094,7 +3097,7 @@ func TimeRange(expr Expr) (min, max time.Time) { return } -// TimeRange returns the minimum and maximum times, as epoch nano, specified by +// TimeRangeAsEpochNano returns the minimum and maximum times, as epoch nano, specified by // and expression. If there is no lower bound, the start of the epoch is returned // for minimum. If there is no higher bound, now is returned for maximum. func TimeRangeAsEpochNano(expr Expr) (min, max int64) { @@ -3721,11 +3724,12 @@ type Valuer interface { Value(key string) (interface{}, bool) } -// nowValuer returns only the value for "now()". +// NowValuer returns only the value for "now()". type NowValuer struct { Now time.Time } +// Value is a method that returns the value and existence flag for a given key. func (v *NowValuer) Value(key string) (interface{}, bool) { if key == "now()" { return v.Now, true diff --git a/influxql/parser.go b/influxql/parser.go index 101f670250d..81e99f7cd2b 100644 --- a/influxql/parser.go +++ b/influxql/parser.go @@ -519,7 +519,7 @@ func (p *Parser) parseUInt64() (uint64, error) { // This function assumes the DURATION token has already been consumed. func (p *Parser) parseDuration() (time.Duration, error) { tok, pos, lit := p.scanIgnoreWhitespace() - if tok != DURATION_VAL && tok != INF { + if tok != DURATIONVAL && tok != INF { return 0, newParseError(tokstr(tok, lit), []string{"duration"}, pos) } @@ -1509,7 +1509,7 @@ func (p *Parser) parseCreateDatabaseStatement() (*CreateDatabaseStatement, error stmt.RetentionPolicyDuration = rpDuration // Look for "REPLICATION" - var rpReplication int = 1 // default is 1 + var rpReplication = 1 // default is 1 if err := p.parseTokens([]Token{REPLICATION}); err != nil { p.unscan() } else { @@ -2331,7 +2331,7 @@ func (p *Parser) parseUnaryExpr() (Expr, error) { return &NumberLiteral{Val: v}, nil case TRUE, FALSE: return &BooleanLiteral{Val: (tok == TRUE)}, nil - case DURATION_VAL: + case DURATIONVAL: v, _ := ParseDuration(lit) return &DurationLiteral{Val: v}, nil case MUL: @@ -2422,7 +2422,7 @@ func (p *Parser) parseResample() (time.Duration, time.Duration, error) { var interval time.Duration if p.parseTokenMaybe(EVERY) { tok, pos, lit := p.scanIgnoreWhitespace() - if tok != DURATION_VAL { + if tok != DURATIONVAL { return 0, 0, newParseError(tokstr(tok, lit), []string{"duration"}, pos) } @@ -2436,7 +2436,7 @@ func (p *Parser) parseResample() (time.Duration, time.Duration, error) { var maxDuration time.Duration if p.parseTokenMaybe(FOR) { tok, pos, lit := p.scanIgnoreWhitespace() - if tok != DURATION_VAL { + if tok != DURATIONVAL { return 0, 0, newParseError(tokstr(tok, lit), []string{"duration"}, pos) } diff --git a/influxql/result.go b/influxql/result.go index 9c52a51b683..ccf151ee9f0 100644 --- a/influxql/result.go +++ b/influxql/result.go @@ -22,8 +22,8 @@ func (t *TagSet) AddFilter(key string, filter Expr) { t.Filters = append(t.Filters, filter) } -// Rows represents a list of rows that can be sorted consistently by name/tag. // Result represents a resultset returned from a single statement. +// Rows represents a list of rows that can be sorted consistently by name/tag. type Result struct { // StatementID is just the statement's position in the query. It's used // to combine statement results if they're being buffered in memory. @@ -67,6 +67,8 @@ func (r *Result) UnmarshalJSON(b []byte) error { return nil } +// GetProcessor is a Method that returns processor type and index +// based on the type of expression. func GetProcessor(expr Expr, startIndex int) (Processor, int) { switch expr := expr.(type) { case *VarRef: @@ -91,6 +93,7 @@ func GetProcessor(expr Expr, startIndex int) (Processor, int) { panic("unreachable") } +// Processor is a prcessor type returned by GetProcessor type Processor func(values []interface{}) interface{} func newEchoProcessor(index int) Processor { diff --git a/influxql/scanner.go b/influxql/scanner.go index d071c85717c..aa562e224f2 100644 --- a/influxql/scanner.go +++ b/influxql/scanner.go @@ -176,6 +176,7 @@ func (s *Scanner) scanString() (tok Token, pos Pos, lit string) { return STRING, pos, lit } +// ScanRegex consumes a token to find escapes func (s *Scanner) ScanRegex() (tok Token, pos Pos, lit string) { _, pos = s.r.curr() @@ -255,7 +256,7 @@ func (s *Scanner) scanNumber() (tok Token, pos Pos, lit string) { // If the next rune is a duration unit (u,µ,ms,s) then return a duration token if ch0, _ := s.r.read(); ch0 == 'u' || ch0 == 'µ' || ch0 == 's' || ch0 == 'h' || ch0 == 'd' || ch0 == 'w' { _, _ = buf.WriteRune(ch0) - return DURATION_VAL, pos, buf.String() + return DURATIONVAL, pos, buf.String() } else if ch0 == 'm' { _, _ = buf.WriteRune(ch0) if ch1, _ := s.r.read(); ch1 == 's' { @@ -263,7 +264,7 @@ func (s *Scanner) scanNumber() (tok Token, pos Pos, lit string) { } else { s.r.unread() } - return DURATION_VAL, pos, buf.String() + return DURATIONVAL, pos, buf.String() } s.r.unread() } @@ -444,6 +445,7 @@ func (r *reader) curr() (ch rune, pos Pos) { // eof is a marker code point to signify that the reader can't read any more. const eof = rune(0) +// ScanDelimited reads a delimited set of runes func ScanDelimited(r io.RuneScanner, start, end rune, escapes map[rune]rune, escapesPassThru bool) ([]byte, error) { // Scan start delimiter. if ch, _, err := r.ReadRune(); err != nil { diff --git a/influxql/scanner_test.go b/influxql/scanner_test.go index d859d20e650..f437928e35a 100644 --- a/influxql/scanner_test.go +++ b/influxql/scanner_test.go @@ -95,14 +95,14 @@ func TestScanner_Scan(t *testing.T) { {s: `10.3s`, tok: influxql.NUMBER, lit: `10.3`}, // Durations - {s: `10u`, tok: influxql.DURATION_VAL, lit: `10u`}, - {s: `10µ`, tok: influxql.DURATION_VAL, lit: `10µ`}, - {s: `10ms`, tok: influxql.DURATION_VAL, lit: `10ms`}, - {s: `-1s`, tok: influxql.DURATION_VAL, lit: `-1s`}, - {s: `10m`, tok: influxql.DURATION_VAL, lit: `10m`}, - {s: `10h`, tok: influxql.DURATION_VAL, lit: `10h`}, - {s: `10d`, tok: influxql.DURATION_VAL, lit: `10d`}, - {s: `10w`, tok: influxql.DURATION_VAL, lit: `10w`}, + {s: `10u`, tok: influxql.DURATIONVAL, lit: `10u`}, + {s: `10µ`, tok: influxql.DURATIONVAL, lit: `10µ`}, + {s: `10ms`, tok: influxql.DURATIONVAL, lit: `10ms`}, + {s: `-1s`, tok: influxql.DURATIONVAL, lit: `-1s`}, + {s: `10m`, tok: influxql.DURATIONVAL, lit: `10m`}, + {s: `10h`, tok: influxql.DURATIONVAL, lit: `10h`}, + {s: `10d`, tok: influxql.DURATIONVAL, lit: `10d`}, + {s: `10w`, tok: influxql.DURATIONVAL, lit: `10w`}, {s: `10x`, tok: influxql.NUMBER, lit: `10`}, // non-duration unit // Keywords diff --git a/influxql/token.go b/influxql/token.go index fa66ec089f3..f26a565690b 100644 --- a/influxql/token.go +++ b/influxql/token.go @@ -7,28 +7,29 @@ import ( // Token is a lexical token of the InfluxQL language. type Token int +// These are a comprehensive list of InfluxQL language tokens. const ( - // Special tokens + // ILLEGAL Token, EOF, WS are Special InfluxQL tokens. ILLEGAL Token = iota EOF WS - literal_beg - // Literals - IDENT // main - NUMBER // 12345.67 - DURATION_VAL // 13h - STRING // "abc" - BADSTRING // "abc - BADESCAPE // \q - TRUE // true - FALSE // false - REGEX // Regular expressions - BADREGEX // `.* - literal_end + literalBeg + // IDENT and the following are InfluxQL literal tokens. + IDENT // main + NUMBER // 12345.67 + DURATIONVAL // 13h + STRING // "abc" + BADSTRING // "abc + BADESCAPE // \q + TRUE // true + FALSE // false + REGEX // Regular expressions + BADREGEX // `.* + literalEnd - operator_beg - // Operators + operatorBeg + // ADD and the following are InfluxQL Operators ADD // + SUB // - MUL // * @@ -45,7 +46,7 @@ const ( LTE // <= GT // > GTE // >= - operator_end + operatorEnd LPAREN // ( RPAREN // ) @@ -54,8 +55,8 @@ const ( SEMICOLON // ; DOT // . - keyword_beg - // Keywords + keywordBeg + // ALL and the following are InfluxQL Keywords ALL ALTER ANY @@ -137,7 +138,7 @@ const ( WHERE WITH WRITE - keyword_end + keywordEnd ) var tokens = [...]string{ @@ -145,15 +146,15 @@ var tokens = [...]string{ EOF: "EOF", WS: "WS", - IDENT: "IDENT", - NUMBER: "NUMBER", - DURATION_VAL: "DURATION_VAL", - STRING: "STRING", - BADSTRING: "BADSTRING", - BADESCAPE: "BADESCAPE", - TRUE: "TRUE", - FALSE: "FALSE", - REGEX: "REGEX", + IDENT: "IDENT", + NUMBER: "NUMBER", + DURATIONVAL: "DURATIONVAL", + STRING: "STRING", + BADSTRING: "BADSTRING", + BADESCAPE: "BADESCAPE", + TRUE: "TRUE", + FALSE: "FALSE", + REGEX: "REGEX", ADD: "+", SUB: "-", @@ -266,7 +267,7 @@ var keywords map[string]Token func init() { keywords = make(map[string]Token) - for tok := keyword_beg + 1; tok < keyword_end; tok++ { + for tok := keywordBeg + 1; tok < keywordEnd; tok++ { keywords[strings.ToLower(tokens[tok])] = tok } for _, tok := range []Token{AND, OR} { @@ -302,7 +303,7 @@ func (tok Token) Precedence() int { } // isOperator returns true for operator tokens. -func (tok Token) isOperator() bool { return tok > operator_beg && tok < operator_end } +func (tok Token) isOperator() bool { return tok > operatorBeg && tok < operatorEnd } // tokstr returns a literal if provided, otherwise returns the token string. func tokstr(tok Token, lit string) string {