diff --git a/.golangci.yaml b/.golangci.yaml index cbae67d2..48ce1f63 100644 --- a/.golangci.yaml +++ b/.golangci.yaml @@ -14,20 +14,21 @@ linters: # Enable specific linter # https://golangci-lint.run/usage/linters/#enabled-by-default enable: -# - asasalint -# - asciicheck -# - bidichk -# - bodyclose -# - containedctx + - asasalint + - asciicheck + - bidichk + - bodyclose + - containedctx + - contextcheck # - contextcheck # - cyclop -# - deadcode -# - decorder + - deadcode + - decorder # - depguard -# - dogsled + - dogsled # - dupl # - dupword -# - durationcheck + - durationcheck # - errcheck # - errchkjson # - errname @@ -52,11 +53,11 @@ linters: # - godot # - godox # - goerr113 -# - gofmt + - gofmt # - gofumpt # - goheader # - goimports -# - golint + - golint # - gomnd # - gomoddirectives # - gomodguard @@ -85,7 +86,7 @@ linters: # - nilerr # - nilnil - nlreturn -# - noctx + - noctx # - nolintlint # - nonamedreturns # - nosnakecase @@ -112,10 +113,10 @@ linters: # - typecheck # - unconvert # - unparam -# - unused + - unused # - usestdlibvars -# - varcheck -# - varnamelen + - varcheck + - varnamelen # - wastedassign # - whitespace # - wrapcheck @@ -127,20 +128,20 @@ linters: # Disable specific linter # https://golangci-lint.run/usage/linters/#disabled-by-default disable: - - asasalint - - asciicheck - - bidichk - - bodyclose - - containedctx - - contextcheck +# - asasalint +# - asciicheck +# - bidichk +# - bodyclose +# - containedctx +# - contextcheck - cyclop - - deadcode - - decorder +# - deadcode +# - decorder - depguard - - dogsled +# - dogsled - dupl - dupword - - durationcheck +# - durationcheck - errcheck - errchkjson - errname @@ -165,11 +166,11 @@ linters: - godot - godox - goerr113 - - gofmt +# - gofmt - gofumpt - goheader - goimports - - golint +# - golint - gomnd - gomoddirectives - gomodguard @@ -198,7 +199,7 @@ linters: - nilerr - nilnil # - nlreturn - - noctx +# - noctx - nolintlint - nonamedreturns - nosnakecase @@ -225,10 +226,10 @@ linters: - typecheck - unconvert - unparam - - unused +# - unused - usestdlibvars - - varcheck - - varnamelen +# - varcheck +# - varnamelen - wastedassign - whitespace - wrapcheck diff --git a/antlr/GruleParserV3Listener.go b/antlr/GruleParserV3Listener.go index d0a0e9e9..332e35ff 100755 --- a/antlr/GruleParserV3Listener.go +++ b/antlr/GruleParserV3Listener.go @@ -88,74 +88,74 @@ type GruleV3ParserListener struct { } // VisitTerminal is called when a terminal node is visited. -func (s *GruleV3ParserListener) VisitTerminal(node antlr.TerminalNode) { - if s.StopParse { +func (thisListener *GruleV3ParserListener) VisitTerminal(node antlr.TerminalNode) { + if thisListener.StopParse { return } - s.PreviousNode = append(s.PreviousNode, node.GetText()) - if len(s.PreviousNode) > 5 { - s.PreviousNode = s.PreviousNode[1:] + thisListener.PreviousNode = append(thisListener.PreviousNode, node.GetText()) + if len(thisListener.PreviousNode) > 5 { + thisListener.PreviousNode = thisListener.PreviousNode[1:] } } // VisitErrorNode is called when an error node is visited. -func (s *GruleV3ParserListener) VisitErrorNode(node antlr.ErrorNode) { - LoggerV3.Errorf("GRL error, after '%v' and then unexpected '%s'", s.PreviousNode, node.GetText()) - s.StopParse = true +func (thisListener *GruleV3ParserListener) VisitErrorNode(node antlr.ErrorNode) { + LoggerV3.Errorf("GRL error, after '%v' and then unexpected '%thisListener'", thisListener.PreviousNode, node.GetText()) + thisListener.StopParse = true } // EnterEveryRule is called when any rule is entered. -func (s *GruleV3ParserListener) EnterEveryRule(ctx antlr.ParserRuleContext) {} +func (thisListener *GruleV3ParserListener) EnterEveryRule(ctx antlr.ParserRuleContext) {} // ExitEveryRule is called when any rule is exited. -func (s *GruleV3ParserListener) ExitEveryRule(ctx antlr.ParserRuleContext) {} +func (thisListener *GruleV3ParserListener) ExitEveryRule(ctx antlr.ParserRuleContext) {} // EnterGrl is called when production grl is entered. -func (s *GruleV3ParserListener) EnterGrl(ctx *grulev3.GrlContext) { - s.Grl = ast.NewGrl() - s.Stack.Push(s.Grl) +func (thisListener *GruleV3ParserListener) EnterGrl(ctx *grulev3.GrlContext) { + thisListener.Grl = ast.NewGrl() + thisListener.Stack.Push(thisListener.Grl) } // ExitGrl is called when production root is exited. The listener will instruct working memory re-index here. -func (s *GruleV3ParserListener) ExitGrl(ctx *grulev3.GrlContext) { - if s.StopParse { +func (thisListener *GruleV3ParserListener) ExitGrl(ctx *grulev3.GrlContext) { + if thisListener.StopParse { return } - if _, ok := s.Stack.Pop().(*ast.Grl); !ok { - s.StopParse = true + if _, ok := thisListener.Stack.Pop().(*ast.Grl); !ok { + thisListener.StopParse = true return } - for _, re := range s.Grl.RuleEntries { - err := s.KnowledgeBase.AddRuleEntry(re) + for _, re := range thisListener.Grl.RuleEntries { + err := thisListener.KnowledgeBase.AddRuleEntry(re) if err != nil { - s.ErrorCallback.AddError(err) + thisListener.ErrorCallback.AddError(err) } } } // EnterRuleEntry is called when production ruleEntry is entered. -func (s *GruleV3ParserListener) EnterRuleEntry(ctx *grulev3.RuleEntryContext) { - if s.StopParse { +func (thisListener *GruleV3ParserListener) EnterRuleEntry(ctx *grulev3.RuleEntryContext) { + if thisListener.StopParse { return } entry := ast.NewRuleEntry() entry.GrlText = ctx.GetText() - s.Stack.Push(entry) + thisListener.Stack.Push(entry) } // ExitRuleEntry is called when production ruleEntry is exited. -func (s *GruleV3ParserListener) ExitRuleEntry(ctx *grulev3.RuleEntryContext) { - if s.StopParse { +func (thisListener *GruleV3ParserListener) ExitRuleEntry(ctx *grulev3.RuleEntryContext) { + if thisListener.StopParse { return } - entry, ok := s.Stack.Pop().(*ast.RuleEntry) - if !ok { - s.StopParse = true + entry, popOk := thisListener.Stack.Pop().(*ast.RuleEntry) + if !popOk { + thisListener.StopParse = true return } @@ -168,221 +168,221 @@ func (s *GruleV3ParserListener) ExitRuleEntry(ctx *grulev3.RuleEntryContext) { entry.RuleDescription = txt[1 : len(txt)-1] } - entryReceiver, ok := s.Stack.Peek().(ast.RuleEntryReceiver) - if !ok { - s.StopParse = true + entryReceiver, popOk := thisListener.Stack.Peek().(ast.RuleEntryReceiver) + if !popOk { + thisListener.StopParse = true return } err := entryReceiver.ReceiveRuleEntry(entry) if err != nil { - s.ErrorCallback.AddError(err) + thisListener.ErrorCallback.AddError(err) } else { - LoggerV3.Debugf("Added RuleEntry : %s", entry.RuleName) + LoggerV3.Debugf("Added RuleEntry : %thisListener", entry.RuleName) } } // EnterSalience is called when production salience is entered. -func (s *GruleV3ParserListener) EnterSalience(ctx *grulev3.SalienceContext) { +func (thisListener *GruleV3ParserListener) EnterSalience(ctx *grulev3.SalienceContext) { sal := ast.NewSalience(0) - s.Stack.Push(sal) + thisListener.Stack.Push(sal) } // ExitSalience is called when production salience is exited. -func (s *GruleV3ParserListener) ExitSalience(ctx *grulev3.SalienceContext) { - if s.StopParse { +func (thisListener *GruleV3ParserListener) ExitSalience(ctx *grulev3.SalienceContext) { + if thisListener.StopParse { return } - salience, ok := s.Stack.Pop().(*ast.Salience) - if !ok { - s.StopParse = true + salience, popOk := thisListener.Stack.Pop().(*ast.Salience) + if !popOk { + thisListener.StopParse = true return } - salienceReceiver, ok := s.Stack.Peek().(ast.SalienceReceiver) - if !ok { - s.StopParse = true + salienceReceiver, popOk := thisListener.Stack.Peek().(ast.SalienceReceiver) + if !popOk { + thisListener.StopParse = true return } err := salienceReceiver.AcceptSalience(salience) if err != nil { - s.StopParse = true - s.ErrorCallback.AddError(err) + thisListener.StopParse = true + thisListener.ErrorCallback.AddError(err) } } // EnterWhenScope is called when production whenScope is entered. -func (s *GruleV3ParserListener) EnterWhenScope(ctx *grulev3.WhenScopeContext) { - if s.StopParse { +func (thisListener *GruleV3ParserListener) EnterWhenScope(ctx *grulev3.WhenScopeContext) { + if thisListener.StopParse { return } whenScope := ast.NewWhenScope() whenScope.GrlText = ctx.GetText() - s.Stack.Push(whenScope) + thisListener.Stack.Push(whenScope) } // ExitWhenScope is called when production whenScope is exited. -func (s *GruleV3ParserListener) ExitWhenScope(ctx *grulev3.WhenScopeContext) { - if s.StopParse { +func (thisListener *GruleV3ParserListener) ExitWhenScope(ctx *grulev3.WhenScopeContext) { + if thisListener.StopParse { return } - when, ok := s.Stack.Pop().(*ast.WhenScope) - if !ok { - s.StopParse = true + when, popOk := thisListener.Stack.Pop().(*ast.WhenScope) + if !popOk { + thisListener.StopParse = true return } - receiver, ok := s.Stack.Peek().(ast.WhenScopeReceiver) - if !ok { - s.StopParse = true + receiver, popOk := thisListener.Stack.Peek().(ast.WhenScopeReceiver) + if !popOk { + thisListener.StopParse = true return } err := receiver.AcceptWhenScope(when) if err != nil { - s.StopParse = true - s.ErrorCallback.AddError(err) + thisListener.StopParse = true + thisListener.ErrorCallback.AddError(err) } } // EnterThenScope is called when production thenScope is entered. -func (s *GruleV3ParserListener) EnterThenScope(ctx *grulev3.ThenScopeContext) { - if s.StopParse { +func (thisListener *GruleV3ParserListener) EnterThenScope(ctx *grulev3.ThenScopeContext) { + if thisListener.StopParse { return } then := ast.NewThenScope() then.GrlText = ctx.GetText() - s.Stack.Push(then) + thisListener.Stack.Push(then) } // ExitThenScope is called when production thenScope is exited. -func (s *GruleV3ParserListener) ExitThenScope(ctx *grulev3.ThenScopeContext) { - if s.StopParse { +func (thisListener *GruleV3ParserListener) ExitThenScope(ctx *grulev3.ThenScopeContext) { + if thisListener.StopParse { return } - then, ok := s.Stack.Pop().(*ast.ThenScope) - if !ok { - s.StopParse = true + then, popOk := thisListener.Stack.Pop().(*ast.ThenScope) + if !popOk { + thisListener.StopParse = true return } - receiver, ok := s.Stack.Peek().(ast.ThenScopeReceiver) - if !ok { - s.StopParse = true + receiver, popOk := thisListener.Stack.Peek().(ast.ThenScopeReceiver) + if !popOk { + thisListener.StopParse = true return } err := receiver.AcceptThenScope(then) if err != nil { - s.StopParse = true - s.ErrorCallback.AddError(err) + thisListener.StopParse = true + thisListener.ErrorCallback.AddError(err) } } // EnterThenExpressionList is called when production thenExpressionList is entered. -func (s *GruleV3ParserListener) EnterThenExpressionList(ctx *grulev3.ThenExpressionListContext) { - if s.StopParse { +func (thisListener *GruleV3ParserListener) EnterThenExpressionList(ctx *grulev3.ThenExpressionListContext) { + if thisListener.StopParse { return } thenExpList := ast.NewThenExpressionList() thenExpList.GrlText = ctx.GetText() - s.Stack.Push(thenExpList) + thisListener.Stack.Push(thenExpList) } // ExitThenExpressionList is called when production thenExpressionList is exited. -func (s *GruleV3ParserListener) ExitThenExpressionList(ctx *grulev3.ThenExpressionListContext) { - if s.StopParse { +func (thisListener *GruleV3ParserListener) ExitThenExpressionList(ctx *grulev3.ThenExpressionListContext) { + if thisListener.StopParse { return } - thenExpList, ok := s.Stack.Pop().(*ast.ThenExpressionList) - if !ok { - s.StopParse = true + thenExpList, popOk := thisListener.Stack.Pop().(*ast.ThenExpressionList) + if !popOk { + thisListener.StopParse = true return } - receiver, ok := s.Stack.Peek().(ast.ThenExpressionListReceiver) - if !ok { - s.StopParse = true + receiver, popOk := thisListener.Stack.Peek().(ast.ThenExpressionListReceiver) + if !popOk { + thisListener.StopParse = true return } err := receiver.AcceptThenExpressionList(thenExpList) if err != nil { - s.StopParse = true - s.ErrorCallback.AddError(err) + thisListener.StopParse = true + thisListener.ErrorCallback.AddError(err) } } // EnterThenExpression is called when production thenExpression is entered. -func (s *GruleV3ParserListener) EnterThenExpression(ctx *grulev3.ThenExpressionContext) { - if s.StopParse { +func (thisListener *GruleV3ParserListener) EnterThenExpression(ctx *grulev3.ThenExpressionContext) { + if thisListener.StopParse { return } thenExpr := ast.NewThenExpression() thenExpr.GrlText = ctx.GetText() - s.Stack.Push(thenExpr) + thisListener.Stack.Push(thenExpr) } // ExitThenExpression is called when production thenExpression is exited. -func (s *GruleV3ParserListener) ExitThenExpression(ctx *grulev3.ThenExpressionContext) { - if s.StopParse { +func (thisListener *GruleV3ParserListener) ExitThenExpression(ctx *grulev3.ThenExpressionContext) { + if thisListener.StopParse { return } - thenExpr, ok := s.Stack.Pop().(*ast.ThenExpression) - if !ok { - s.StopParse = true + thenExpr, popOk := thisListener.Stack.Pop().(*ast.ThenExpression) + if !popOk { + thisListener.StopParse = true return } - receiver, ok := s.Stack.Peek().(ast.ThenExpressionReceiver) - if !ok { - s.StopParse = true + receiver, popOk := thisListener.Stack.Peek().(ast.ThenExpressionReceiver) + if !popOk { + thisListener.StopParse = true return } err := receiver.AcceptThenExpression(thenExpr) if err != nil { - s.StopParse = true - s.ErrorCallback.AddError(err) + thisListener.StopParse = true + thisListener.ErrorCallback.AddError(err) } } // EnterAssignment is called when production assignment is entered. -func (s *GruleV3ParserListener) EnterAssignment(ctx *grulev3.AssignmentContext) { - if s.StopParse { +func (thisListener *GruleV3ParserListener) EnterAssignment(ctx *grulev3.AssignmentContext) { + if thisListener.StopParse { return } assign := ast.NewAssignment() assign.GrlText = ctx.GetText() - s.Stack.Push(assign) + thisListener.Stack.Push(assign) } // ExitAssignment is called when production assignment is exited. -func (s *GruleV3ParserListener) ExitAssignment(ctx *grulev3.AssignmentContext) { - if s.StopParse { +func (thisListener *GruleV3ParserListener) ExitAssignment(ctx *grulev3.AssignmentContext) { + if thisListener.StopParse { return } - assign, ok := s.Stack.Pop().(*ast.Assignment) - if !ok { - s.StopParse = true + assign, okPop := thisListener.Stack.Pop().(*ast.Assignment) + if !okPop { + thisListener.StopParse = true return } - receiver, ok := s.Stack.Peek().(ast.AssignmentReceiver) - if !ok { - s.StopParse = true + receiver, okPop := thisListener.Stack.Peek().(ast.AssignmentReceiver) + if !okPop { + thisListener.StopParse = true return } @@ -394,37 +394,37 @@ func (s *GruleV3ParserListener) ExitAssignment(ctx *grulev3.AssignmentContext) { err := receiver.AcceptAssignment(assign) if err != nil { - s.StopParse = true - s.ErrorCallback.AddError(err) + thisListener.StopParse = true + thisListener.ErrorCallback.AddError(err) } } // EnterExpression is called when production expression is entered. -func (s *GruleV3ParserListener) EnterExpression(ctx *grulev3.ExpressionContext) { - if s.StopParse { +func (thisListener *GruleV3ParserListener) EnterExpression(ctx *grulev3.ExpressionContext) { + if thisListener.StopParse { return } expr := ast.NewExpression() expr.GrlText = ctx.GetText() - s.Stack.Push(expr) + thisListener.Stack.Push(expr) } // ExitExpression is called when production expression is exited. -func (s *GruleV3ParserListener) ExitExpression(ctx *grulev3.ExpressionContext) { - if s.StopParse { +func (thisListener *GruleV3ParserListener) ExitExpression(ctx *grulev3.ExpressionContext) { + if thisListener.StopParse { return } - expr, ok := s.Stack.Pop().(*ast.Expression) - if !ok { - s.StopParse = true + expr, popOk := thisListener.Stack.Pop().(*ast.Expression) + if !popOk { + thisListener.StopParse = true return } - exprRec, ok := s.Stack.Peek().(ast.ExpressionReceiver) - if !ok { - s.StopParse = true + exprRec, popOk := thisListener.Stack.Peek().(ast.ExpressionReceiver) + if !popOk { + thisListener.StopParse = true return } @@ -433,22 +433,22 @@ func (s *GruleV3ParserListener) ExitExpression(ctx *grulev3.ExpressionContext) { expr.Negated = ctx.NEGATION() != nil } - err := exprRec.AcceptExpression(s.KnowledgeBase.WorkingMemory.AddExpression(expr)) + err := exprRec.AcceptExpression(thisListener.KnowledgeBase.WorkingMemory.AddExpression(expr)) if err != nil { - s.StopParse = true - s.ErrorCallback.AddError(err) + thisListener.StopParse = true + thisListener.ErrorCallback.AddError(err) } } // EnterMulDivOperators is called when production mulDivOperators is entered. -func (s *GruleV3ParserListener) EnterMulDivOperators(ctx *grulev3.MulDivOperatorsContext) { - if s.StopParse { +func (thisListener *GruleV3ParserListener) EnterMulDivOperators(ctx *grulev3.MulDivOperatorsContext) { + if thisListener.StopParse { return } - expr, ok := s.Stack.Peek().(*ast.Expression) + expr, ok := thisListener.Stack.Peek().(*ast.Expression) if !ok { - s.StopParse = true + thisListener.StopParse = true return } @@ -463,17 +463,17 @@ func (s *GruleV3ParserListener) EnterMulDivOperators(ctx *grulev3.MulDivOperator } // ExitMulDivOperators is called when production mulDivOperators is exited. -func (s *GruleV3ParserListener) ExitMulDivOperators(ctx *grulev3.MulDivOperatorsContext) {} +func (thisListener *GruleV3ParserListener) ExitMulDivOperators(ctx *grulev3.MulDivOperatorsContext) {} // EnterAddMinusOperators is called when production addMinusOperators is entered. -func (s *GruleV3ParserListener) EnterAddMinusOperators(ctx *grulev3.AddMinusOperatorsContext) { - if s.StopParse { +func (thisListener *GruleV3ParserListener) EnterAddMinusOperators(ctx *grulev3.AddMinusOperatorsContext) { + if thisListener.StopParse { return } - expr, ok := s.Stack.Peek().(*ast.Expression) + expr, ok := thisListener.Stack.Peek().(*ast.Expression) if !ok { - s.StopParse = true + thisListener.StopParse = true return } @@ -490,17 +490,18 @@ func (s *GruleV3ParserListener) EnterAddMinusOperators(ctx *grulev3.AddMinusOper } // ExitAddMinusOperators is called when production addMinusOperators is exited. -func (s *GruleV3ParserListener) ExitAddMinusOperators(ctx *grulev3.AddMinusOperatorsContext) {} +func (thisListener *GruleV3ParserListener) ExitAddMinusOperators(ctx *grulev3.AddMinusOperatorsContext) { +} // EnterComparisonOperator is called when production comparisonOperator is entered. -func (s *GruleV3ParserListener) EnterComparisonOperator(ctx *grulev3.ComparisonOperatorContext) { - if s.StopParse { +func (thisListener *GruleV3ParserListener) EnterComparisonOperator(ctx *grulev3.ComparisonOperatorContext) { + if thisListener.StopParse { return } - expr, ok := s.Stack.Peek().(*ast.Expression) + expr, ok := thisListener.Stack.Peek().(*ast.Expression) if !ok { - s.StopParse = true + thisListener.StopParse = true return } @@ -521,17 +522,18 @@ func (s *GruleV3ParserListener) EnterComparisonOperator(ctx *grulev3.ComparisonO } // ExitComparisonOperator is called when production comparisonOperator is exited. -func (s *GruleV3ParserListener) ExitComparisonOperator(ctx *grulev3.ComparisonOperatorContext) {} +func (thisListener *GruleV3ParserListener) ExitComparisonOperator(ctx *grulev3.ComparisonOperatorContext) { +} // EnterAndLogicOperator is called when production andLogicOperator is entered. -func (s *GruleV3ParserListener) EnterAndLogicOperator(ctx *grulev3.AndLogicOperatorContext) { - if s.StopParse { +func (thisListener *GruleV3ParserListener) EnterAndLogicOperator(ctx *grulev3.AndLogicOperatorContext) { + if thisListener.StopParse { return } - expr, ok := s.Stack.Peek().(*ast.Expression) + expr, ok := thisListener.Stack.Peek().(*ast.Expression) if !ok { - s.StopParse = true + thisListener.StopParse = true return } @@ -539,17 +541,18 @@ func (s *GruleV3ParserListener) EnterAndLogicOperator(ctx *grulev3.AndLogicOpera } // ExitAndLogicOperator is called when production andLogicOperator is exited. -func (s *GruleV3ParserListener) ExitAndLogicOperator(ctx *grulev3.AndLogicOperatorContext) {} +func (thisListener *GruleV3ParserListener) ExitAndLogicOperator(ctx *grulev3.AndLogicOperatorContext) { +} // EnterOrLogicOperator is called when production orLogicOperator is entered. -func (s *GruleV3ParserListener) EnterOrLogicOperator(ctx *grulev3.OrLogicOperatorContext) { - if s.StopParse { +func (thisListener *GruleV3ParserListener) EnterOrLogicOperator(ctx *grulev3.OrLogicOperatorContext) { + if thisListener.StopParse { return } - expr, ok := s.Stack.Peek().(*ast.Expression) + expr, ok := thisListener.Stack.Peek().(*ast.Expression) if !ok { - s.StopParse = true + thisListener.StopParse = true return } @@ -557,158 +560,158 @@ func (s *GruleV3ParserListener) EnterOrLogicOperator(ctx *grulev3.OrLogicOperato } // ExitOrLogicOperator is called when production orLogicOperator is exited. -func (s *GruleV3ParserListener) ExitOrLogicOperator(ctx *grulev3.OrLogicOperatorContext) {} +func (thisListener *GruleV3ParserListener) ExitOrLogicOperator(ctx *grulev3.OrLogicOperatorContext) {} // EnterExpressionAtom is called when production expressionAtom is entered. -func (s *GruleV3ParserListener) EnterExpressionAtom(ctx *grulev3.ExpressionAtomContext) { - if s.StopParse { +func (thisListener *GruleV3ParserListener) EnterExpressionAtom(ctx *grulev3.ExpressionAtomContext) { + if thisListener.StopParse { return } atm := ast.NewExpressionAtom() atm.GrlText = ctx.GetText() - s.Stack.Push(atm) + thisListener.Stack.Push(atm) } // ExitExpressionAtom is called when production expressionAtom is exited. -func (s *GruleV3ParserListener) ExitExpressionAtom(ctx *grulev3.ExpressionAtomContext) { - if s.StopParse { +func (thisListener *GruleV3ParserListener) ExitExpressionAtom(ctx *grulev3.ExpressionAtomContext) { + if thisListener.StopParse { return } - atm, ok := s.Stack.Pop().(*ast.ExpressionAtom) - if !ok { - s.StopParse = true + expressionAtm, okPop := thisListener.Stack.Pop().(*ast.ExpressionAtom) + if !okPop { + thisListener.StopParse = true return } - expr, ok := s.Stack.Peek().(ast.ExpressionAtomReceiver) - if !ok { - s.StopParse = true + expr, okPop := thisListener.Stack.Peek().(ast.ExpressionAtomReceiver) + if !okPop { + thisListener.StopParse = true return } - atm.Negated = ctx.NEGATION() != nil + expressionAtm.Negated = ctx.NEGATION() != nil - err := expr.AcceptExpressionAtom(s.KnowledgeBase.WorkingMemory.AddExpressionAtom(atm)) + err := expr.AcceptExpressionAtom(thisListener.KnowledgeBase.WorkingMemory.AddExpressionAtom(expressionAtm)) if err != nil { - s.StopParse = true - s.ErrorCallback.AddError(err) + thisListener.StopParse = true + thisListener.ErrorCallback.AddError(err) } } // EnterArrayMapSelector is called when production arrayMapSelector is entered. -func (s *GruleV3ParserListener) EnterArrayMapSelector(ctx *grulev3.ArrayMapSelectorContext) { - if s.StopParse { +func (thisListener *GruleV3ParserListener) EnterArrayMapSelector(ctx *grulev3.ArrayMapSelectorContext) { + if thisListener.StopParse { return } sel := ast.NewArrayMapSelector() sel.GrlText = ctx.GetText() - s.Stack.Push(sel) + thisListener.Stack.Push(sel) } // ExitArrayMapSelector is called when production arrayMapSelector is exited. -func (s *GruleV3ParserListener) ExitArrayMapSelector(ctx *grulev3.ArrayMapSelectorContext) { - if s.StopParse { +func (thisListener *GruleV3ParserListener) ExitArrayMapSelector(ctx *grulev3.ArrayMapSelectorContext) { + if thisListener.StopParse { return } - sel, ok := s.Stack.Pop().(*ast.ArrayMapSelector) - if !ok { - s.StopParse = true + sel, popOk := thisListener.Stack.Pop().(*ast.ArrayMapSelector) + if !popOk { + thisListener.StopParse = true return } - receiver, ok := s.Stack.Peek().(ast.ArrayMapSelectorReceiver) - if !ok { - s.StopParse = true + receiver, popOk := thisListener.Stack.Peek().(ast.ArrayMapSelectorReceiver) + if !popOk { + thisListener.StopParse = true return } err := receiver.AcceptArrayMapSelector(sel) if err != nil { - s.StopParse = true - s.ErrorCallback.AddError(err) + thisListener.StopParse = true + thisListener.ErrorCallback.AddError(err) } } // EnterFunctionCall is called when production functionCall is entered. -func (s *GruleV3ParserListener) EnterFunctionCall(ctx *grulev3.FunctionCallContext) { - if s.StopParse { +func (thisListener *GruleV3ParserListener) EnterFunctionCall(ctx *grulev3.FunctionCallContext) { + if thisListener.StopParse { return } fun := ast.NewFunctionCall() fun.FunctionName = ctx.SIMPLENAME().GetText() - s.Stack.Push(fun) + thisListener.Stack.Push(fun) } // ExitFunctionCall is called when production functionCall is exited. -func (s *GruleV3ParserListener) ExitFunctionCall(ctx *grulev3.FunctionCallContext) { - if s.StopParse { +func (thisListener *GruleV3ParserListener) ExitFunctionCall(ctx *grulev3.FunctionCallContext) { + if thisListener.StopParse { return } - fun, ok := s.Stack.Pop().(*ast.FunctionCall) - if !ok { - s.StopParse = true + fun, popOk := thisListener.Stack.Pop().(*ast.FunctionCall) + if !popOk { + thisListener.StopParse = true return } - metRec, ok := s.Stack.Peek().(ast.FunctionCallReceiver) - if !ok { - s.StopParse = true + metRec, popOk := thisListener.Stack.Peek().(ast.FunctionCallReceiver) + if !popOk { + thisListener.StopParse = true return } err := metRec.AcceptFunctionCall(fun) if err != nil { - s.StopParse = true - s.ErrorCallback.AddError(err) + thisListener.StopParse = true + thisListener.ErrorCallback.AddError(err) } } // EnterArgumentList is called when production argumentList is entered. -func (s *GruleV3ParserListener) EnterArgumentList(ctx *grulev3.ArgumentListContext) { - if s.StopParse { +func (thisListener *GruleV3ParserListener) EnterArgumentList(ctx *grulev3.ArgumentListContext) { + if thisListener.StopParse { return } argList := ast.NewArgumentList() argList.GrlText = ctx.GetText() - s.Stack.Push(argList) + thisListener.Stack.Push(argList) } // ExitArgumentList is called when production argumentList is exited. -func (s *GruleV3ParserListener) ExitArgumentList(ctx *grulev3.ArgumentListContext) { - if s.StopParse { +func (thisListener *GruleV3ParserListener) ExitArgumentList(ctx *grulev3.ArgumentListContext) { + if thisListener.StopParse { return } - argList, ok := s.Stack.Pop().(*ast.ArgumentList) - if !ok { - s.StopParse = true + argList, popOk := thisListener.Stack.Pop().(*ast.ArgumentList) + if !popOk { + thisListener.StopParse = true return } - argListRec, ok := s.Stack.Peek().(ast.ArgumentListReceiver) - if !ok { - s.StopParse = true + argListRec, popOk := thisListener.Stack.Peek().(ast.ArgumentListReceiver) + if !popOk { + thisListener.StopParse = true return } LoggerV3.Tracef("Adding Argument List To Receiver") err := argListRec.AcceptArgumentList(argList) if err != nil { - s.StopParse = true - s.ErrorCallback.AddError(err) + thisListener.StopParse = true + thisListener.ErrorCallback.AddError(err) } } // EnterVariable is called when production variable is entered. -func (s *GruleV3ParserListener) EnterVariable(ctx *grulev3.VariableContext) { - if s.StopParse { +func (thisListener *GruleV3ParserListener) EnterVariable(ctx *grulev3.VariableContext) { + if thisListener.StopParse { return } @@ -720,43 +723,43 @@ func (s *GruleV3ParserListener) EnterVariable(ctx *grulev3.VariableContext) { vari.Name = ctx.MemberVariable().GetText()[1:] } vari.GrlText = ctx.GetText() - s.Stack.Push(vari) + thisListener.Stack.Push(vari) } // ExitVariable is called when production variable is exited. -func (s *GruleV3ParserListener) ExitVariable(ctx *grulev3.VariableContext) { - if s.StopParse { +func (thisListener *GruleV3ParserListener) ExitVariable(ctx *grulev3.VariableContext) { + if thisListener.StopParse { return } - vari, ok := s.Stack.Pop().(*ast.Variable) - if !ok { - s.StopParse = true + vari, okPop := thisListener.Stack.Pop().(*ast.Variable) + if !okPop { + thisListener.StopParse = true return } - variRec, ok := s.Stack.Peek().(ast.VariableReceiver) - if !ok { - s.StopParse = true + variRec, okPop := thisListener.Stack.Peek().(ast.VariableReceiver) + if !okPop { + thisListener.StopParse = true return } - err := variRec.AcceptVariable(s.KnowledgeBase.WorkingMemory.AddVariable(vari)) + err := variRec.AcceptVariable(thisListener.KnowledgeBase.WorkingMemory.AddVariable(vari)) if err != nil { - s.StopParse = true - s.ErrorCallback.AddError(err) + thisListener.StopParse = true + thisListener.ErrorCallback.AddError(err) } } // EnterMemberVariable is called when production memberVariable is entered. -func (s *GruleV3ParserListener) EnterMemberVariable(ctx *grulev3.MemberVariableContext) {} +func (thisListener *GruleV3ParserListener) EnterMemberVariable(ctx *grulev3.MemberVariableContext) {} // ExitMemberVariable is called when production memberVariable is exited. -func (s *GruleV3ParserListener) ExitMemberVariable(ctx *grulev3.MemberVariableContext) { - vari, ok := s.Stack.Peek().(ast.MemberVariableReceiver) +func (thisListener *GruleV3ParserListener) ExitMemberVariable(ctx *grulev3.MemberVariableContext) { + vari, ok := thisListener.Stack.Peek().(ast.MemberVariableReceiver) if !ok { - s.StopParse = true + thisListener.StopParse = true return } @@ -764,31 +767,31 @@ func (s *GruleV3ParserListener) ExitMemberVariable(ctx *grulev3.MemberVariableCo } // EnterConstant is called when production constant is entered. -func (s *GruleV3ParserListener) EnterConstant(ctx *grulev3.ConstantContext) { - if s.StopParse { +func (thisListener *GruleV3ParserListener) EnterConstant(ctx *grulev3.ConstantContext) { + if thisListener.StopParse { return } cons := ast.NewConstant() cons.GrlText = ctx.GetText() - s.Stack.Push(cons) + thisListener.Stack.Push(cons) } // ExitConstant is called when production constant is exited. -func (s *GruleV3ParserListener) ExitConstant(ctx *grulev3.ConstantContext) { - if s.StopParse { +func (thisListener *GruleV3ParserListener) ExitConstant(ctx *grulev3.ConstantContext) { + if thisListener.StopParse { return } - cons, ok := s.Stack.Pop().(*ast.Constant) - if !ok { - s.StopParse = true + cons, popOk := thisListener.Stack.Pop().(*ast.Constant) + if !popOk { + thisListener.StopParse = true return } - conRec, ok := s.Stack.Peek().(ast.ConstantReceiver) - if !ok { - s.StopParse = true + conRec, popOk := thisListener.Stack.Peek().(ast.ConstantReceiver) + if !popOk { + thisListener.StopParse = true return } @@ -797,30 +800,30 @@ func (s *GruleV3ParserListener) ExitConstant(ctx *grulev3.ConstantContext) { } err := conRec.AcceptConstant(cons) if err != nil { - s.StopParse = true - s.ErrorCallback.AddError(err) + thisListener.StopParse = true + thisListener.ErrorCallback.AddError(err) } } // EnterStringLiteral is called when production stringLiteral is entered. -func (s *GruleV3ParserListener) EnterStringLiteral(ctx *grulev3.StringLiteralContext) { +func (thisListener *GruleV3ParserListener) EnterStringLiteral(ctx *grulev3.StringLiteralContext) { } // ExitStringLiteral is called when production stringLiteral is exited. -func (s *GruleV3ParserListener) ExitStringLiteral(ctx *grulev3.StringLiteralContext) { - if s.StopParse { +func (thisListener *GruleV3ParserListener) ExitStringLiteral(ctx *grulev3.StringLiteralContext) { + if thisListener.StopParse { return } dec, err := unquoteString(ctx.GetText()) if err != nil { - s.ErrorCallback.AddError(fmt.Errorf("error parsing quoted string (%s): %s", ctx.GetText(), err.Error())) + thisListener.ErrorCallback.AddError(fmt.Errorf("error parsing quoted string (%s): %s", ctx.GetText(), err.Error())) return } - receiver, ok := s.Stack.Peek().(ast.StringLiteralReceiver) + receiver, ok := thisListener.Stack.Peek().(ast.StringLiteralReceiver) if !ok { - s.StopParse = true + thisListener.StopParse = true return } @@ -828,17 +831,17 @@ func (s *GruleV3ParserListener) ExitStringLiteral(ctx *grulev3.StringLiteralCont } // EnterBooleanLiteral is called when production booleanLiteral is entered. -func (s *GruleV3ParserListener) EnterBooleanLiteral(ctx *grulev3.BooleanLiteralContext) {} +func (thisListener *GruleV3ParserListener) EnterBooleanLiteral(ctx *grulev3.BooleanLiteralContext) {} // ExitBooleanLiteral is called when production booleanLiteral is exited. -func (s *GruleV3ParserListener) ExitBooleanLiteral(ctx *grulev3.BooleanLiteralContext) { - if s.StopParse { +func (thisListener *GruleV3ParserListener) ExitBooleanLiteral(ctx *grulev3.BooleanLiteralContext) { + if thisListener.StopParse { return } - receiver, ok := s.Stack.Peek().(ast.BooleanLiteralReceiver) + receiver, ok := thisListener.Stack.Peek().(ast.BooleanLiteralReceiver) if !ok { - s.StopParse = true + thisListener.StopParse = true return } @@ -854,21 +857,21 @@ func (s *GruleV3ParserListener) ExitBooleanLiteral(ctx *grulev3.BooleanLiteralCo } // EnterIntegerLiteral is called when production integerLiteral is entered. -func (s *GruleV3ParserListener) EnterIntegerLiteral(ctx *grulev3.IntegerLiteralContext) {} +func (thisListener *GruleV3ParserListener) EnterIntegerLiteral(ctx *grulev3.IntegerLiteralContext) {} // ExitIntegerLiteral is called when production integerLiteral is exited. -func (s *GruleV3ParserListener) ExitIntegerLiteral(ctx *grulev3.IntegerLiteralContext) { +func (thisListener *GruleV3ParserListener) ExitIntegerLiteral(ctx *grulev3.IntegerLiteralContext) { lit := &ast.IntegerLiteral{} i, err := strconv.ParseInt(ctx.GetText(), 0, 64) if err != nil { - s.StopParse = true - s.ErrorCallback.AddError(err) + thisListener.StopParse = true + thisListener.ErrorCallback.AddError(err) } else { lit.Integer = i } - receiver, ok := s.Stack.Peek().(ast.IntegerLiteralReceiver) + receiver, ok := thisListener.Stack.Peek().(ast.IntegerLiteralReceiver) if !ok { - s.StopParse = true + thisListener.StopParse = true return } @@ -876,21 +879,21 @@ func (s *GruleV3ParserListener) ExitIntegerLiteral(ctx *grulev3.IntegerLiteralCo } // EnterFloatLiteral is called when production floatLiteral is entered. -func (s *GruleV3ParserListener) EnterFloatLiteral(ctx *grulev3.FloatLiteralContext) {} +func (thisListener *GruleV3ParserListener) EnterFloatLiteral(ctx *grulev3.FloatLiteralContext) {} // ExitFloatLiteral is called when production floatLiteral is exited. -func (s *GruleV3ParserListener) ExitFloatLiteral(ctx *grulev3.FloatLiteralContext) { +func (thisListener *GruleV3ParserListener) ExitFloatLiteral(ctx *grulev3.FloatLiteralContext) { lit := &ast.FloatLiteral{} i, err := strconv.ParseFloat(ctx.GetText(), 64) if err != nil { - s.StopParse = true - s.ErrorCallback.AddError(err) + thisListener.StopParse = true + thisListener.ErrorCallback.AddError(err) } else { lit.Float = i } - receiver, ok := s.Stack.Peek().(ast.FloatLiteralReceiver) + receiver, ok := thisListener.Stack.Peek().(ast.FloatLiteralReceiver) if !ok { - s.StopParse = true + thisListener.StopParse = true return } diff --git a/antlr/ParserCommon.go b/antlr/ParserCommon.go index f114b269..a9d00aac 100755 --- a/antlr/ParserCommon.go +++ b/antlr/ParserCommon.go @@ -20,37 +20,37 @@ import ( "unicode/utf8" ) -func unquoteString(s string) (string, error) { - n := len(s) - if n < 2 { +func unquoteString(theStr string) (string, error) { + strLen := len(theStr) + if strLen < 2 { return "", strconv.ErrSyntax } - quote := s[0] - if quote != s[n-1] { + quote := theStr[0] + if quote != theStr[strLen-1] { return "", strconv.ErrSyntax } - s = s[1 : n-1] + theStr = theStr[1 : strLen-1] if quote != '"' && quote != '\'' { return "", strconv.ErrSyntax } - if !contains(s, '\\') && !contains(s, quote) && utf8.ValidString(s) { - return s, nil + if !contains(theStr, '\\') && !contains(theStr, quote) && utf8.ValidString(theStr) { + return theStr, nil } var runeTmp [utf8.UTFMax]byte - buf := make([]byte, 0, 3*len(s)/2) - for len(s) > 0 { - c, multibyte, ss, err := strconv.UnquoteChar(s, quote) + buf := make([]byte, 0, 3*len(theStr)/2) + for len(theStr) > 0 { + theRune, multibyte, ss, err := strconv.UnquoteChar(theStr, quote) if err != nil { return "", err } - s = ss - if c < utf8.RuneSelf || !multibyte { - buf = append(buf, byte(c)) + theStr = ss + if theRune < utf8.RuneSelf || !multibyte { + buf = append(buf, byte(theRune)) } else { - n := utf8.EncodeRune(runeTmp[:], c) + n := utf8.EncodeRune(runeTmp[:], theRune) buf = append(buf, runeTmp[:n]...) } } diff --git a/ast/DataContext.go b/ast/DataContext.go index 0c0c7265..ca739a37 100755 --- a/ast/DataContext.go +++ b/ast/DataContext.go @@ -44,7 +44,7 @@ type DataContext struct { func (ctx *DataContext) GetKeys() []string { ret := make([]string, len(ctx.ObjectStore)) c := 0 - for k, _ := range ctx.ObjectStore { + for k := range ctx.ObjectStore { ret[c] = k c++ } diff --git a/ast/KnowledgeBase.go b/ast/KnowledgeBase.go index 4e95a3d8..160a86bf 100755 --- a/ast/KnowledgeBase.go +++ b/ast/KnowledgeBase.go @@ -44,20 +44,20 @@ type KnowledgeLibrary struct { // Although this KnowledgeBase blueprint works, It SHOULD NOT be used directly in the engine. // You should obtain KnowledgeBase instance by calling NewKnowledgeBaseInstance func (lib *KnowledgeLibrary) GetKnowledgeBase(name, version string) *KnowledgeBase { - kb, ok := lib.Library[fmt.Sprintf("%s:%s", name, version)] + knowledgeBase, ok := lib.Library[fmt.Sprintf("%s:%s", name, version)] if ok { - return kb + return knowledgeBase } - kb = &KnowledgeBase{ + knowledgeBase = &KnowledgeBase{ Name: name, Version: version, RuleEntries: make(map[string]*RuleEntry), WorkingMemory: NewWorkingMemory(name, version), } - lib.Library[fmt.Sprintf("%s:%s", name, version)] = kb + lib.Library[fmt.Sprintf("%s:%s", name, version)] = knowledgeBase - return kb + return knowledgeBase } // RemoveRuleEntry mark the rule entry as deleted @@ -94,19 +94,19 @@ func (lib *KnowledgeLibrary) LoadKnowledgeBaseFromReader(reader io.Reader, overw return nil, err } - kb := catalog.BuildKnowledgeBase() + knowledgeBase := catalog.BuildKnowledgeBase() if overwrite { - lib.Library[fmt.Sprintf("%s:%s", kb.Name, kb.Version)] = kb + lib.Library[fmt.Sprintf("%s:%s", knowledgeBase.Name, knowledgeBase.Version)] = knowledgeBase - return kb, nil + return knowledgeBase, nil } - if _, ok := lib.Library[fmt.Sprintf("%s:%s", kb.Name, kb.Version)]; !ok { - lib.Library[fmt.Sprintf("%s:%s", kb.Name, kb.Version)] = kb + if _, ok := lib.Library[fmt.Sprintf("%s:%s", knowledgeBase.Name, knowledgeBase.Version)]; !ok { + lib.Library[fmt.Sprintf("%s:%s", knowledgeBase.Name, knowledgeBase.Version)] = knowledgeBase - return kb, nil + return knowledgeBase, nil } - return nil, fmt.Errorf("KnowledgeBase %s version %s exist", kb.Name, kb.Version) + return nil, fmt.Errorf("KnowledgeBase %s version %s exist", knowledgeBase.Name, knowledgeBase.Version) } // StoreKnowledgeBaseToWriter will store a KnowledgeBase in binary form @@ -128,15 +128,15 @@ func (lib *KnowledgeLibrary) StoreKnowledgeBaseToWriter(writer io.Writer, name, // NewKnowledgeBaseInstance will create a new instance based on KnowledgeBase blue print // identified by its name and version func (lib *KnowledgeLibrary) NewKnowledgeBaseInstance(name, version string) *KnowledgeBase { - kb, ok := lib.Library[fmt.Sprintf("%s:%s", name, version)] + knowledgeBase, ok := lib.Library[fmt.Sprintf("%s:%s", name, version)] if ok { - newClone := kb.Clone(pkg.NewCloneTable()) - if kb.IsIdentical(newClone) { + newClone := knowledgeBase.Clone(pkg.NewCloneTable()) + if knowledgeBase.IsIdentical(newClone) { AstLog.Debugf("Successfully create instance [%s:%s]", newClone.Name, newClone.Version) return newClone } - AstLog.Fatalf("ORIGIN : %s", kb.GetSnapshot()) + AstLog.Fatalf("ORIGIN : %s", knowledgeBase.GetSnapshot()) AstLog.Fatalf("CLONE : %s", newClone.GetSnapshot()) panic("The clone is not identical") } diff --git a/ast/Serializer.go b/ast/Serializer.go index e2ac84b4..fcacbe38 100755 --- a/ast/Serializer.go +++ b/ast/Serializer.go @@ -102,7 +102,7 @@ type Catalog struct { // the rebuilt KnowledgeBase is identical to the original KnowledgeBase from // which this Catalog was built. func (cat *Catalog) BuildKnowledgeBase() *KnowledgeBase { - wm := &WorkingMemory{ + workingMem := &WorkingMemory{ Name: cat.MemoryName, Version: cat.MemoryVersion, expressionSnapshotMap: make(map[string]*Expression), @@ -112,11 +112,11 @@ func (cat *Catalog) BuildKnowledgeBase() *KnowledgeBase { expressionAtomVariableMap: make(map[*Variable][]*ExpressionAtom), ID: unique.NewID(), } - kb := &KnowledgeBase{ + knowledgeBase := &KnowledgeBase{ Name: cat.KnowledgeBaseName, Version: cat.KnowledgeBaseVersion, DataContext: nil, - WorkingMemory: wm, + WorkingMemory: workingMem, RuleEntries: make(map[string]*RuleEntry), } importTable := make(map[string]Node) @@ -133,15 +133,15 @@ func (cat *Catalog) BuildKnowledgeBase() *KnowledgeBase { } importTable[meta.GetAstID()] = n case TypeArrayMapSelector: - n := &ArrayMapSelector{ + arrayMapSelect := &ArrayMapSelector{ AstID: meta.GetAstID(), GrlText: meta.GetGrlText(), Expression: nil, } - importTable[meta.GetAstID()] = n + importTable[meta.GetAstID()] = arrayMapSelect case TypeAssignment: amet := meta.(*AssigmentMeta) - n := &Assignment{ + assignment := &Assignment{ AstID: amet.AstID, GrlText: amet.GrlText, Variable: nil, @@ -152,10 +152,10 @@ func (cat *Catalog) BuildKnowledgeBase() *KnowledgeBase { IsDivAssign: amet.IsDivAssign, IsMulAssign: amet.IsMulAssign, } - importTable[amet.AstID] = n + importTable[amet.AstID] = assignment case TypeExpression: amet := meta.(*ExpressionMeta) - n := &Expression{ + expression := &Expression{ AstID: amet.AstID, GrlText: amet.GrlText, LeftExpression: nil, @@ -165,10 +165,10 @@ func (cat *Catalog) BuildKnowledgeBase() *KnowledgeBase { Operator: amet.Operator, Negated: amet.Negated, } - importTable[amet.AstID] = n + importTable[amet.AstID] = expression case TypeConstant: amet := meta.(*ConstantMeta) - n := &Constant{ + newConst := &Constant{ AstID: amet.AstID, GrlText: amet.GrlText, Snapshot: amet.Snapshot, @@ -183,26 +183,26 @@ func (cat *Catalog) BuildKnowledgeBase() *KnowledgeBase { dLen := binary.LittleEndian.Uint64(length) byteArr := make([]byte, dLen) buffer.Read(byteArr) - n.Value = reflect.ValueOf(string(byteArr)) + newConst.Value = reflect.ValueOf(string(byteArr)) case TypeBoolean: arr := make([]byte, 1) buffer.Read(arr) - n.Value = reflect.ValueOf(arr[0] == 1) + newConst.Value = reflect.ValueOf(arr[0] == 1) case TypeInteger: arr := make([]byte, 8) buffer.Read(arr) - n.Value = reflect.ValueOf(int64(binary.LittleEndian.Uint64(arr))) + newConst.Value = reflect.ValueOf(int64(binary.LittleEndian.Uint64(arr))) case TypeFloat: arr := make([]byte, 8) buffer.Read(arr) bits := binary.LittleEndian.Uint64(arr) float := math.Float64frombits(bits) - n.Value = reflect.ValueOf(float) + newConst.Value = reflect.ValueOf(float) } - importTable[amet.AstID] = n + importTable[amet.AstID] = newConst case TypeExpressionAtom: amet := meta.(*ExpressionAtomMeta) - n := &ExpressionAtom{ + expressionAtm := &ExpressionAtom{ AstID: amet.AstID, GrlText: amet.GrlText, VariableName: amet.VariableName, @@ -213,19 +213,19 @@ func (cat *Catalog) BuildKnowledgeBase() *KnowledgeBase { ExpressionAtom: nil, ArrayMapSelector: nil, } - importTable[amet.AstID] = n + importTable[amet.AstID] = expressionAtm case TypeFunctionCall: amet := meta.(*FunctionCallMeta) - n := &FunctionCall{ + funcCall := &FunctionCall{ AstID: amet.AstID, GrlText: amet.GrlText, FunctionName: amet.FunctionName, ArgumentList: nil, } - importTable[amet.AstID] = n + importTable[amet.AstID] = funcCall case TypeRuleEntry: amet := meta.(*RuleEntryMeta) - n := &RuleEntry{ + ruleEntry := &RuleEntry{ AstID: amet.AstID, GrlText: amet.GrlText, RuleName: amet.RuleName, @@ -234,25 +234,25 @@ func (cat *Catalog) BuildKnowledgeBase() *KnowledgeBase { WhenScope: nil, ThenScope: nil, } - importTable[amet.AstID] = n - kb.RuleEntries[n.RuleName] = n + importTable[amet.AstID] = ruleEntry + knowledgeBase.RuleEntries[ruleEntry.RuleName] = ruleEntry case TypeThenExpression: amet := meta.(*ThenExpressionMeta) - n := &ThenExpression{ + thenExp := &ThenExpression{ AstID: amet.AstID, GrlText: amet.GrlText, Assignment: nil, ExpressionAtom: nil, } - importTable[amet.AstID] = n + importTable[amet.AstID] = thenExp case TypeThenExpressionList: amet := meta.(*ThenExpressionListMeta) - n := &ThenExpressionList{ + thenExprList := &ThenExpressionList{ AstID: amet.AstID, GrlText: amet.GrlText, ThenExpressions: nil, } - importTable[amet.AstID] = n + importTable[amet.AstID] = thenExprList case TypeThenScope: amet := meta.(*ThenScopeMeta) n := &ThenScope{ @@ -263,14 +263,14 @@ func (cat *Catalog) BuildKnowledgeBase() *KnowledgeBase { importTable[amet.AstID] = n case TypeVariable: amet := meta.(*VariableMeta) - n := &Variable{ + variable := &Variable{ AstID: amet.AstID, GrlText: amet.GrlText, Name: amet.Name, Variable: nil, ArrayMapSelector: nil, } - importTable[amet.AstID] = n + importTable[amet.AstID] = variable case TypeWhenScope: amet := meta.(*WhenScopeMeta) n := &WhenScope{ @@ -289,122 +289,122 @@ func (cat *Catalog) BuildKnowledgeBase() *KnowledgeBase { node := importTable[astID] switch meta.GetASTType() { case TypeArgumentList: - n := node.(*ArgumentList) + argList := node.(*ArgumentList) amet := meta.(*ArgumentListMeta) if amet.ArgumentASTIDs != nil && len(amet.ArgumentASTIDs) > 0 { - n.Arguments = make([]*Expression, len(amet.ArgumentASTIDs)) + argList.Arguments = make([]*Expression, len(amet.ArgumentASTIDs)) for k, v := range amet.ArgumentASTIDs { - n.Arguments[k] = importTable[v].(*Expression) + argList.Arguments[k] = importTable[v].(*Expression) } } case TypeArrayMapSelector: - n := node.(*ArrayMapSelector) + arrayMapSel := node.(*ArrayMapSelector) amet := meta.(*ArrayMapSelectorMeta) if len(amet.ExpressionID) > 0 { - n.Expression = importTable[amet.ExpressionID].(*Expression) + arrayMapSel.Expression = importTable[amet.ExpressionID].(*Expression) } case TypeAssignment: - n := node.(*Assignment) + assignment := node.(*Assignment) amet := meta.(*AssigmentMeta) if len(amet.ExpressionID) > 0 { - n.Expression = importTable[amet.ExpressionID].(*Expression) + assignment.Expression = importTable[amet.ExpressionID].(*Expression) } if len(amet.VariableID) > 0 { - n.Variable = importTable[amet.VariableID].(*Variable) + assignment.Variable = importTable[amet.VariableID].(*Variable) } case TypeExpression: - n := node.(*Expression) + expr := node.(*Expression) amet := meta.(*ExpressionMeta) if len(amet.LeftExpressionID) > 0 { - n.LeftExpression = importTable[amet.LeftExpressionID].(*Expression) + expr.LeftExpression = importTable[amet.LeftExpressionID].(*Expression) } if len(amet.RightExpressionID) > 0 { - n.RightExpression = importTable[amet.RightExpressionID].(*Expression) + expr.RightExpression = importTable[amet.RightExpressionID].(*Expression) } if len(amet.SingleExpressionID) > 0 { - n.SingleExpression = importTable[amet.SingleExpressionID].(*Expression) + expr.SingleExpression = importTable[amet.SingleExpressionID].(*Expression) } if len(amet.ExpressionAtomID) > 0 { - n.ExpressionAtom = importTable[amet.ExpressionAtomID].(*ExpressionAtom) + expr.ExpressionAtom = importTable[amet.ExpressionAtomID].(*ExpressionAtom) } case TypeConstant: // nothing todo case TypeExpressionAtom: - n := node.(*ExpressionAtom) + expressAtm := node.(*ExpressionAtom) amet := meta.(*ExpressionAtomMeta) if len(amet.ConstantID) > 0 { - n.Constant = importTable[amet.ConstantID].(*Constant) + expressAtm.Constant = importTable[amet.ConstantID].(*Constant) } if len(amet.ExpressionAtomID) > 0 { - n.ExpressionAtom = importTable[amet.ExpressionAtomID].(*ExpressionAtom) + expressAtm.ExpressionAtom = importTable[amet.ExpressionAtomID].(*ExpressionAtom) } if len(amet.VariableID) > 0 { - n.Variable = importTable[amet.VariableID].(*Variable) + expressAtm.Variable = importTable[amet.VariableID].(*Variable) } if len(amet.FunctionCallID) > 0 { - n.FunctionCall = importTable[amet.FunctionCallID].(*FunctionCall) + expressAtm.FunctionCall = importTable[amet.FunctionCallID].(*FunctionCall) } if len(amet.ArrayMapSelectorID) > 0 { - n.ArrayMapSelector = importTable[amet.ArrayMapSelectorID].(*ArrayMapSelector) + expressAtm.ArrayMapSelector = importTable[amet.ArrayMapSelectorID].(*ArrayMapSelector) } case TypeFunctionCall: - n := node.(*FunctionCall) + funcCall := node.(*FunctionCall) amet := meta.(*FunctionCallMeta) if len(amet.ArgumentListID) > 0 { - n.ArgumentList = importTable[amet.ArgumentListID].(*ArgumentList) + funcCall.ArgumentList = importTable[amet.ArgumentListID].(*ArgumentList) } case TypeRuleEntry: - n := node.(*RuleEntry) + ruleEntry := node.(*RuleEntry) amet := meta.(*RuleEntryMeta) if len(amet.WhenScopeID) > 0 { - n.WhenScope = importTable[amet.WhenScopeID].(*WhenScope) + ruleEntry.WhenScope = importTable[amet.WhenScopeID].(*WhenScope) } if len(amet.ThenScopeID) > 0 { - n.ThenScope = importTable[amet.ThenScopeID].(*ThenScope) + ruleEntry.ThenScope = importTable[amet.ThenScopeID].(*ThenScope) } case TypeThenExpression: - n := node.(*ThenExpression) + thenExpr := node.(*ThenExpression) amet := meta.(*ThenExpressionMeta) if len(amet.AssignmentID) > 0 { - n.Assignment = importTable[amet.AssignmentID].(*Assignment) + thenExpr.Assignment = importTable[amet.AssignmentID].(*Assignment) } if len(amet.ExpressionAtomID) > 0 { - n.ExpressionAtom = importTable[amet.ExpressionAtomID].(*ExpressionAtom) + thenExpr.ExpressionAtom = importTable[amet.ExpressionAtomID].(*ExpressionAtom) } case TypeThenExpressionList: - n := node.(*ThenExpressionList) + ThenExprList := node.(*ThenExpressionList) amet := meta.(*ThenExpressionListMeta) if amet.ThenExpressionIDs != nil && len(amet.ThenExpressionIDs) > 0 { - n.ThenExpressions = make([]*ThenExpression, len(amet.ThenExpressionIDs)) + ThenExprList.ThenExpressions = make([]*ThenExpression, len(amet.ThenExpressionIDs)) for k, v := range amet.ThenExpressionIDs { if node, ok := importTable[v]; ok { - n.ThenExpressions[k] = node.(*ThenExpression) + ThenExprList.ThenExpressions[k] = node.(*ThenExpression) } else { logrus.Errorf("then expression with ast id %s not catalogued", v) } } } case TypeThenScope: - n := node.(*ThenScope) + thenScope := node.(*ThenScope) amet := meta.(*ThenScopeMeta) if len(amet.ThenExpressionListID) > 0 { - n.ThenExpressionList = importTable[amet.ThenExpressionListID].(*ThenExpressionList) + thenScope.ThenExpressionList = importTable[amet.ThenExpressionListID].(*ThenExpressionList) } case TypeVariable: - n := node.(*Variable) + variable := node.(*Variable) amet := meta.(*VariableMeta) if len(amet.VariableID) > 0 { - n.Variable = importTable[amet.VariableID].(*Variable) + variable.Variable = importTable[amet.VariableID].(*Variable) } if len(amet.ArrayMapSelectorID) > 0 { - n.ArrayMapSelector = importTable[amet.ArrayMapSelectorID].(*ArrayMapSelector) + variable.ArrayMapSelector = importTable[amet.ArrayMapSelectorID].(*ArrayMapSelector) } case TypeWhenScope: - n := node.(*WhenScope) + whenScope := node.(*WhenScope) amet := meta.(*WhenScopeMeta) if len(amet.ExpressionID) > 0 { - n.Expression = importTable[amet.ExpressionID].(*Expression) + whenScope.Expression = importTable[amet.ExpressionID].(*Expression) } default: panic("Unrecognized meta type") @@ -413,44 +413,44 @@ func (cat *Catalog) BuildKnowledgeBase() *KnowledgeBase { // Rebuilding Working Memory if cat.MemoryVariableSnapshotMap != nil && len(cat.MemoryVariableSnapshotMap) > 0 { - for k, v := range cat.MemoryVariableSnapshotMap { - if n, ok := importTable[v]; ok { - wm.variableSnapshotMap[k] = n.(*Variable) + for key, value := range cat.MemoryVariableSnapshotMap { + if n, ok := importTable[value]; ok { + workingMem.variableSnapshotMap[key] = n.(*Variable) } else { - logrus.Warnf("snapshot %s in working memory have no referenced variable with ASTID %s", k, v) + logrus.Warnf("snapshot %s in working memory have no referenced variable with ASTID %s", key, value) } } } if cat.MemoryExpressionSnapshotMap != nil && len(cat.MemoryExpressionSnapshotMap) > 0 { - for k, v := range cat.MemoryExpressionSnapshotMap { - wm.expressionSnapshotMap[k] = importTable[v].(*Expression) + for key, value := range cat.MemoryExpressionSnapshotMap { + workingMem.expressionSnapshotMap[key] = importTable[value].(*Expression) } } if cat.MemoryExpressionAtomSnapshotMap != nil && len(cat.MemoryExpressionAtomSnapshotMap) > 0 { - for k, v := range cat.MemoryExpressionAtomSnapshotMap { - wm.expressionAtomSnapshotMap[k] = importTable[v].(*ExpressionAtom) + for key, value := range cat.MemoryExpressionAtomSnapshotMap { + workingMem.expressionAtomSnapshotMap[key] = importTable[value].(*ExpressionAtom) } } if cat.MemoryExpressionVariableMap != nil && len(cat.MemoryExpressionVariableMap) > 0 { - for k, v := range cat.MemoryExpressionVariableMap { - variable := importTable[k].(*Variable) - wm.expressionVariableMap[variable] = make([]*Expression, len(v)) - for i, j := range v { - wm.expressionVariableMap[variable][i] = importTable[j].(*Expression) + for key, value := range cat.MemoryExpressionVariableMap { + variable := importTable[key].(*Variable) + workingMem.expressionVariableMap[variable] = make([]*Expression, len(value)) + for i, j := range value { + workingMem.expressionVariableMap[variable][i] = importTable[j].(*Expression) } } } if cat.MemoryExpressionAtomVariableMap != nil && len(cat.MemoryExpressionAtomVariableMap) > 0 { - for k, v := range cat.MemoryExpressionAtomVariableMap { - variable := importTable[k].(*Variable) - wm.expressionAtomVariableMap[variable] = make([]*ExpressionAtom, len(v)) - for i, j := range v { - wm.expressionAtomVariableMap[variable][i] = importTable[j].(*ExpressionAtom) + for key, value := range cat.MemoryExpressionAtomVariableMap { + variable := importTable[key].(*Variable) + workingMem.expressionAtomVariableMap[variable] = make([]*ExpressionAtom, len(value)) + for i, j := range value { + workingMem.expressionAtomVariableMap[variable][i] = importTable[j].(*ExpressionAtom) } } } - return kb + return knowledgeBase } // Equals used for testing purpose, to ensure that two catalog @@ -477,9 +477,9 @@ func (cat *Catalog) Equals(that *Catalog) bool { return false } - for k, v := range cat.Data { - if j, ok := that.Data[k]; ok { - if !j.Equals(v) { + for key, value := range cat.Data { + if j, ok := that.Data[key]; ok { + if !j.Equals(value) { return false } @@ -488,9 +488,9 @@ func (cat *Catalog) Equals(that *Catalog) bool { return false } } - for k, v := range cat.MemoryVariableSnapshotMap { - if j, ok := that.MemoryVariableSnapshotMap[k]; ok { - if j != v { + for key, value := range cat.MemoryVariableSnapshotMap { + if j, ok := that.MemoryVariableSnapshotMap[key]; ok { + if j != value { return false } @@ -499,9 +499,9 @@ func (cat *Catalog) Equals(that *Catalog) bool { return false } } - for k, v := range cat.MemoryExpressionSnapshotMap { - if j, ok := that.MemoryExpressionSnapshotMap[k]; ok { - if j != v { + for key, value := range cat.MemoryExpressionSnapshotMap { + if j, ok := that.MemoryExpressionSnapshotMap[key]; ok { + if j != value { return false } @@ -510,9 +510,9 @@ func (cat *Catalog) Equals(that *Catalog) bool { return false } } - for k, v := range cat.MemoryExpressionAtomSnapshotMap { - if j, ok := that.MemoryExpressionAtomSnapshotMap[k]; ok { - if j != v { + for key, value := range cat.MemoryExpressionAtomSnapshotMap { + if j, ok := that.MemoryExpressionAtomSnapshotMap[key]; ok { + if j != value { return false } @@ -521,14 +521,14 @@ func (cat *Catalog) Equals(that *Catalog) bool { return false } } - for k, v := range cat.MemoryExpressionVariableMap { - if j, ok := that.MemoryExpressionVariableMap[k]; ok { - if len(j) != len(v) { + for key, value := range cat.MemoryExpressionVariableMap { + if mapValue, ok := that.MemoryExpressionVariableMap[key]; ok { + if len(mapValue) != len(value) { return false } - for in, st := range v { - if j[in] != st { + for in, st := range value { + if mapValue[in] != st { return false } @@ -538,14 +538,14 @@ func (cat *Catalog) Equals(that *Catalog) bool { return false } } - for k, v := range cat.MemoryExpressionAtomVariableMap { - if j, ok := that.MemoryExpressionAtomVariableMap[k]; ok { - if len(j) != len(v) { + for key, value := range cat.MemoryExpressionAtomVariableMap { + if vari, ok := that.MemoryExpressionAtomVariableMap[key]; ok { + if len(vari) != len(value) { return false } - for in, st := range v { - if j[in] != st { + for in, st := range value { + if vari[in] != st { return false } @@ -563,9 +563,9 @@ func (cat *Catalog) Equals(that *Catalog) bool { // ReadCatalogFromReader would read a byte stream from reader // It will replace all values already sets in a catalog. // You are responsible for closing the reader stream once its done. -func (cat *Catalog) ReadCatalogFromReader(r io.Reader) error { +func (cat *Catalog) ReadCatalogFromReader(reader io.Reader) error { // Read the catalog file version. - str, err := ReadStringFromReader(r) // V + str, err := ReadStringFromReader(reader) // V if err != nil { return err @@ -576,7 +576,7 @@ func (cat *Catalog) ReadCatalogFromReader(r io.Reader) error { } // Read the knowledgebase name. - str, err = ReadStringFromReader(r) // V + str, err = ReadStringFromReader(reader) // V if err != nil { return err @@ -584,7 +584,7 @@ func (cat *Catalog) ReadCatalogFromReader(r io.Reader) error { cat.KnowledgeBaseName = str // Read the knowledgebase version. - str, err = ReadStringFromReader(r) // V + str, err = ReadStringFromReader(reader) // V if err != nil { return err @@ -592,7 +592,7 @@ func (cat *Catalog) ReadCatalogFromReader(r io.Reader) error { cat.KnowledgeBaseVersion = str // Writedown meta counts. - count, err := ReadIntFromReader(r) // V + count, err := ReadIntFromReader(reader) // V if err != nil { return err @@ -601,12 +601,12 @@ func (cat *Catalog) ReadCatalogFromReader(r io.Reader) error { cat.Data = make(map[string]Meta) for i := uint64(0); i < count; i++ { - key, err := ReadStringFromReader(r) // V + key, err := ReadStringFromReader(reader) // V if err != nil { return err } - metaType, err := ReadIntFromReader(r) // V + metaType, err := ReadIntFromReader(reader) // V if err != nil { return err @@ -643,7 +643,7 @@ func (cat *Catalog) ReadCatalogFromReader(r io.Reader) error { return fmt.Errorf("unknown meta number %d", metaType) } - err = meta.ReadMetaFrom(r) // V + err = meta.ReadMetaFrom(reader) // V if err != nil { return err @@ -651,14 +651,14 @@ func (cat *Catalog) ReadCatalogFromReader(r io.Reader) error { cat.Data[key] = meta } - str, err = ReadStringFromReader(r) + str, err = ReadStringFromReader(reader) if err != nil { return err } cat.MemoryName = str - str, err = ReadStringFromReader(r) + str, err = ReadStringFromReader(reader) if err != nil { return err @@ -666,20 +666,20 @@ func (cat *Catalog) ReadCatalogFromReader(r io.Reader) error { cat.MemoryVersion = str // Writedown meta counts. - count, err = ReadIntFromReader(r) + count, err = ReadIntFromReader(reader) if err != nil { return err } cat.MemoryVariableSnapshotMap = make(map[string]string) - for i := uint64(0); i < count; i++ { - key, err := ReadStringFromReader(r) + for index := uint64(0); index < count; index++ { + key, err := ReadStringFromReader(reader) if err != nil { return err } - val, err := ReadStringFromReader(r) + val, err := ReadStringFromReader(reader) if err != nil { return err @@ -688,20 +688,20 @@ func (cat *Catalog) ReadCatalogFromReader(r io.Reader) error { } // MemoryExpressionSnapshotMap meta counts. - count, err = ReadIntFromReader(r) + count, err = ReadIntFromReader(reader) if err != nil { return err } cat.MemoryExpressionSnapshotMap = make(map[string]string) - for i := uint64(0); i < count; i++ { - key, err := ReadStringFromReader(r) + for index := uint64(0); index < count; index++ { + key, err := ReadStringFromReader(reader) if err != nil { return err } - val, err := ReadStringFromReader(r) + val, err := ReadStringFromReader(reader) if err != nil { return err @@ -710,20 +710,20 @@ func (cat *Catalog) ReadCatalogFromReader(r io.Reader) error { } // MemoryExpressionAtomSnapshotMap meta counts. - count, err = ReadIntFromReader(r) + count, err = ReadIntFromReader(reader) if err != nil { return err } cat.MemoryExpressionAtomSnapshotMap = make(map[string]string) - for i := uint64(0); i < count; i++ { - key, err := ReadStringFromReader(r) + for index := uint64(0); index < count; index++ { + key, err := ReadStringFromReader(reader) if err != nil { return err } - val, err := ReadStringFromReader(r) + val, err := ReadStringFromReader(reader) if err != nil { return err @@ -732,63 +732,63 @@ func (cat *Catalog) ReadCatalogFromReader(r io.Reader) error { } // MemoryExpressionVariableMap meta counts. - count, err = ReadIntFromReader(r) + count, err = ReadIntFromReader(reader) if err != nil { return err } cat.MemoryExpressionVariableMap = make(map[string][]string) - for i := uint64(0); i < count; i++ { - key, err := ReadStringFromReader(r) + for index := uint64(0); index < count; index++ { + key, err := ReadStringFromReader(reader) if err != nil { return err } - incount, err := ReadIntFromReader(r) + incount, err := ReadIntFromReader(reader) if err != nil { return err } content := make([]string, incount) - for j := uint64(0); j < incount; j++ { - str, err := ReadStringFromReader(r) + for subIndex := uint64(0); subIndex < incount; subIndex++ { + str, err := ReadStringFromReader(reader) if err != nil { return err } - content[j] = str + content[subIndex] = str } cat.MemoryExpressionVariableMap[key] = content } // MemoryExpressionAtomVariableMap meta counts. - count, err = ReadIntFromReader(r) + count, err = ReadIntFromReader(reader) if err != nil { return err } cat.MemoryExpressionAtomVariableMap = make(map[string][]string) - for i := uint64(0); i < count; i++ { - key, err := ReadStringFromReader(r) + for index := uint64(0); index < count; index++ { + key, err := ReadStringFromReader(reader) if err != nil { return err } - incount, err := ReadIntFromReader(r) + incount, err := ReadIntFromReader(reader) if err != nil { return err } content := make([]string, incount) - for j := uint64(0); j < incount; j++ { - str, err := ReadStringFromReader(r) + for subIndex := uint64(0); subIndex < incount; subIndex++ { + str, err := ReadStringFromReader(reader) if err != nil { return err } - content[j] = str + content[subIndex] = str } cat.MemoryExpressionAtomVariableMap[key] = content } @@ -799,53 +799,53 @@ func (cat *Catalog) ReadCatalogFromReader(r io.Reader) error { // WriteCatalogToWriter will store the content of this Catalog // into a byte stream using provided writer. // You are responsible for closing the writing stream once its done. -func (cat *Catalog) WriteCatalogToWriter(w io.Writer) error { +func (cat *Catalog) WriteCatalogToWriter(writer io.Writer) error { // Write the catalog file version. - err := WriteStringToWriter(w, Version) + err := WriteStringToWriter(writer, Version) if err != nil { return err } // Write the knowledgebase name. - err = WriteStringToWriter(w, cat.KnowledgeBaseName) + err = WriteStringToWriter(writer, cat.KnowledgeBaseName) if err != nil { return err } // Write the knowledgebase version. - err = WriteStringToWriter(w, cat.KnowledgeBaseVersion) + err = WriteStringToWriter(writer, cat.KnowledgeBaseVersion) if err != nil { return err } // Writedown meta counts. - err = WriteIntToWriter(w, uint64(len(cat.Data))) + err = WriteIntToWriter(writer, uint64(len(cat.Data))) if err != nil { return err } // For each meta.. write them down - for k, v := range cat.Data { + for key, value := range cat.Data { // Write the AST ID - err = WriteStringToWriter(w, k) + err = WriteStringToWriter(writer, key) if err != nil { return err } - err := WriteIntToWriter(w, uint64(v.GetASTType())) + err := WriteIntToWriter(writer, uint64(value.GetASTType())) if err != nil { return err } // Write the meta - err = v.WriteMetaTo(w) + err = value.WriteMetaTo(writer) if err != nil { return err @@ -853,32 +853,32 @@ func (cat *Catalog) WriteCatalogToWriter(w io.Writer) error { } // Write the MemoryName version. - err = WriteStringToWriter(w, cat.MemoryName) + err = WriteStringToWriter(writer, cat.MemoryName) if err != nil { return err } // Write the MemoryVersion version. - err = WriteStringToWriter(w, cat.MemoryVersion) + err = WriteStringToWriter(writer, cat.MemoryVersion) if err != nil { return err } // MemoryVariableSnapshotMap meta counts. - err = WriteIntToWriter(w, uint64(len(cat.MemoryVariableSnapshotMap))) + err = WriteIntToWriter(writer, uint64(len(cat.MemoryVariableSnapshotMap))) if err != nil { return err } - for k, v := range cat.MemoryVariableSnapshotMap { - err = WriteStringToWriter(w, k) + for key, value := range cat.MemoryVariableSnapshotMap { + err = WriteStringToWriter(writer, key) if err != nil { return err } - err = WriteStringToWriter(w, v) + err = WriteStringToWriter(writer, value) if err != nil { return err @@ -886,18 +886,18 @@ func (cat *Catalog) WriteCatalogToWriter(w io.Writer) error { } // MemoryExpressionSnapshotMap meta counts. - err = WriteIntToWriter(w, uint64(len(cat.MemoryExpressionSnapshotMap))) + err = WriteIntToWriter(writer, uint64(len(cat.MemoryExpressionSnapshotMap))) if err != nil { return err } - for k, v := range cat.MemoryExpressionSnapshotMap { - err = WriteStringToWriter(w, k) + for key, value := range cat.MemoryExpressionSnapshotMap { + err = WriteStringToWriter(writer, key) if err != nil { return err } - err = WriteStringToWriter(w, v) + err = WriteStringToWriter(writer, value) if err != nil { return err @@ -905,18 +905,18 @@ func (cat *Catalog) WriteCatalogToWriter(w io.Writer) error { } // MemoryExpressionAtomSnapshotMap meta counts. - err = WriteIntToWriter(w, uint64(len(cat.MemoryExpressionAtomSnapshotMap))) + err = WriteIntToWriter(writer, uint64(len(cat.MemoryExpressionAtomSnapshotMap))) if err != nil { return err } - for k, v := range cat.MemoryExpressionAtomSnapshotMap { - err = WriteStringToWriter(w, k) + for key, value := range cat.MemoryExpressionAtomSnapshotMap { + err = WriteStringToWriter(writer, key) if err != nil { return err } - err = WriteStringToWriter(w, v) + err = WriteStringToWriter(writer, value) if err != nil { return err @@ -924,24 +924,24 @@ func (cat *Catalog) WriteCatalogToWriter(w io.Writer) error { } // MemoryExpressionVariableMap meta counts. - err = WriteIntToWriter(w, uint64(len(cat.MemoryExpressionVariableMap))) + err = WriteIntToWriter(writer, uint64(len(cat.MemoryExpressionVariableMap))) if err != nil { return err } - for k, v := range cat.MemoryExpressionVariableMap { - err = WriteStringToWriter(w, k) + for key, value := range cat.MemoryExpressionVariableMap { + err = WriteStringToWriter(writer, key) if err != nil { return err } - err = WriteIntToWriter(w, uint64(len(v))) + err = WriteIntToWriter(writer, uint64(len(value))) if err != nil { return err } - for _, j := range v { - err = WriteStringToWriter(w, j) + for _, j := range value { + err = WriteStringToWriter(writer, j) if err != nil { return err @@ -950,24 +950,24 @@ func (cat *Catalog) WriteCatalogToWriter(w io.Writer) error { } // MemoryExpressionAtomVariableMap meta counts. - err = WriteIntToWriter(w, uint64(len(cat.MemoryExpressionAtomVariableMap))) + err = WriteIntToWriter(writer, uint64(len(cat.MemoryExpressionAtomVariableMap))) if err != nil { return err } - for k, v := range cat.MemoryExpressionAtomVariableMap { - err = WriteStringToWriter(w, k) + for key, value := range cat.MemoryExpressionAtomVariableMap { + err = WriteStringToWriter(writer, key) if err != nil { return err } - err = WriteIntToWriter(w, uint64(len(v))) + err = WriteIntToWriter(writer, uint64(len(value))) if err != nil { return err } - for _, j := range v { - err = WriteStringToWriter(w, j) + for _, j := range value { + err = WriteStringToWriter(writer, j) if err != nil { return err @@ -1034,21 +1034,21 @@ func (meta *NodeMeta) GetSnapshot() string { // WriteMetaTo write basic AST Node information meta data into writer. // One should not use this function directly, unless for testing // serialization of single ASTNode. -func (meta *NodeMeta) WriteMetaTo(w io.Writer) error { +func (meta *NodeMeta) WriteMetaTo(writer io.Writer) error { // First write the AST ID. this may be redundant. - err := WriteStringToWriter(w, meta.AstID) + err := WriteStringToWriter(writer, meta.AstID) if err != nil { return err } // Second write the GRL Text. - err = WriteStringToWriter(w, meta.GrlText) + err = WriteStringToWriter(writer, meta.GrlText) if err != nil { return err } // Third write the snapshot. This might be un-necessary. - err = WriteStringToWriter(w, meta.Snapshot) + err = WriteStringToWriter(writer, meta.Snapshot) if err != nil { return err @@ -1140,15 +1140,15 @@ func (meta *ArgumentListMeta) GetASTType() NodeType { // WriteMetaTo write basic AST Node information meta data into writer. // One should not use this function directly, unless for testing // serialization of single ASTNode. -func (meta *ArgumentListMeta) WriteMetaTo(w io.Writer) error { - err := meta.NodeMeta.WriteMetaTo(w) +func (meta *ArgumentListMeta) WriteMetaTo(writer io.Writer) error { + err := meta.NodeMeta.WriteMetaTo(writer) if err != nil { return err } // Write the number of arguments - err = WriteIntToWriter(w, uint64(len(meta.ArgumentASTIDs))) + err = WriteIntToWriter(writer, uint64(len(meta.ArgumentASTIDs))) if err != nil { return err @@ -1156,7 +1156,7 @@ func (meta *ArgumentListMeta) WriteMetaTo(w io.Writer) error { // Write the array content for _, v := range meta.ArgumentASTIDs { - err = WriteStringToWriter(w, v) + err = WriteStringToWriter(writer, v) if err != nil { return err @@ -1169,27 +1169,27 @@ func (meta *ArgumentListMeta) WriteMetaTo(w io.Writer) error { // ReadMetaFrom write basic AST Node information meta data from reader. // One should not use this function directly, unless for testing // serialization of single ASTNode. -func (meta *ArgumentListMeta) ReadMetaFrom(r io.Reader) error { - err := meta.NodeMeta.ReadMetaFrom(r) +func (meta *ArgumentListMeta) ReadMetaFrom(reader io.Reader) error { + err := meta.NodeMeta.ReadMetaFrom(reader) if err != nil { return err } - in, err := ReadIntFromReader(r) + integer, err := ReadIntFromReader(reader) if err != nil { return err } - meta.ArgumentASTIDs = make([]string, in) - for i := uint64(0); i < in; i++ { - s, err := ReadStringFromReader(r) + meta.ArgumentASTIDs = make([]string, integer) + for index := uint64(0); index < integer; index++ { + s, err := ReadStringFromReader(reader) if err != nil { return err } - meta.ArgumentASTIDs[i] = s + meta.ArgumentASTIDs[index] = s } return nil @@ -1228,13 +1228,13 @@ func (meta *ArrayMapSelectorMeta) GetASTType() NodeType { // WriteMetaTo write basic AST Node information meta data into writer. // One should not use this function directly, unless for testing // serialization of single ASTNode. -func (meta *ArrayMapSelectorMeta) WriteMetaTo(w io.Writer) error { - err := meta.NodeMeta.WriteMetaTo(w) +func (meta *ArrayMapSelectorMeta) WriteMetaTo(writer io.Writer) error { + err := meta.NodeMeta.WriteMetaTo(writer) if err != nil { return err } - err = WriteStringToWriter(w, meta.ExpressionID) + err = WriteStringToWriter(writer, meta.ExpressionID) if err != nil { return err @@ -1246,13 +1246,13 @@ func (meta *ArrayMapSelectorMeta) WriteMetaTo(w io.Writer) error { // ReadMetaFrom write basic AST Node information meta data from reader. // One should not use this function directly, unless for testing // serialization of single ASTNode. -func (meta *ArrayMapSelectorMeta) ReadMetaFrom(r io.Reader) error { - err := meta.NodeMeta.ReadMetaFrom(r) +func (meta *ArrayMapSelectorMeta) ReadMetaFrom(reader io.Reader) error { + err := meta.NodeMeta.ReadMetaFrom(reader) if err != nil { return err } - s, err := ReadStringFromReader(r) + s, err := ReadStringFromReader(reader) if err != nil { return err @@ -1325,45 +1325,45 @@ func (meta *AssigmentMeta) GetASTType() NodeType { // WriteMetaTo write basic AST Node information meta data into writer. // One should not use this function directly, unless for testing // serialization of single ASTNode. -func (meta *AssigmentMeta) WriteMetaTo(w io.Writer) error { - err := meta.NodeMeta.WriteMetaTo(w) +func (meta *AssigmentMeta) WriteMetaTo(writer io.Writer) error { + err := meta.NodeMeta.WriteMetaTo(writer) if err != nil { return err } - err = WriteStringToWriter(w, meta.VariableID) + err = WriteStringToWriter(writer, meta.VariableID) if err != nil { return err } - err = WriteStringToWriter(w, meta.ExpressionID) + err = WriteStringToWriter(writer, meta.ExpressionID) if err != nil { return err } - err = WriteBoolToWriter(w, meta.IsAssign) + err = WriteBoolToWriter(writer, meta.IsAssign) if err != nil { return err } - err = WriteBoolToWriter(w, meta.IsPlusAssign) + err = WriteBoolToWriter(writer, meta.IsPlusAssign) if err != nil { return err } - err = WriteBoolToWriter(w, meta.IsMinusAssign) + err = WriteBoolToWriter(writer, meta.IsMinusAssign) if err != nil { return err } - err = WriteBoolToWriter(w, meta.IsDivAssign) + err = WriteBoolToWriter(writer, meta.IsDivAssign) if err != nil { return err } - err = WriteBoolToWriter(w, meta.IsMulAssign) + err = WriteBoolToWriter(writer, meta.IsMulAssign) if err != nil { return err @@ -1375,60 +1375,60 @@ func (meta *AssigmentMeta) WriteMetaTo(w io.Writer) error { // ReadMetaFrom write basic AST Node information meta data from reader. // One should not use this function directly, unless for testing // serialization of single ASTNode. -func (meta *AssigmentMeta) ReadMetaFrom(r io.Reader) error { - err := meta.NodeMeta.ReadMetaFrom(r) +func (meta *AssigmentMeta) ReadMetaFrom(reader io.Reader) error { + err := meta.NodeMeta.ReadMetaFrom(reader) if err != nil { return err } - s, err := ReadStringFromReader(r) + stringFromReader, err := ReadStringFromReader(reader) if err != nil { return err } - meta.VariableID = s - s, err = ReadStringFromReader(r) + meta.VariableID = stringFromReader + stringFromReader, err = ReadStringFromReader(reader) if err != nil { return err } - meta.ExpressionID = s + meta.ExpressionID = stringFromReader - b, err := ReadBoolFromReader(r) + boolReaded, err := ReadBoolFromReader(reader) if err != nil { return err } - meta.IsAssign = b + meta.IsAssign = boolReaded - b, err = ReadBoolFromReader(r) + boolReaded, err = ReadBoolFromReader(reader) if err != nil { return err } - meta.IsPlusAssign = b + meta.IsPlusAssign = boolReaded - b, err = ReadBoolFromReader(r) + boolReaded, err = ReadBoolFromReader(reader) if err != nil { return err } - meta.IsMinusAssign = b + meta.IsMinusAssign = boolReaded - b, err = ReadBoolFromReader(r) + boolReaded, err = ReadBoolFromReader(reader) if err != nil { return err } - meta.IsDivAssign = b + meta.IsDivAssign = boolReaded - b, err = ReadBoolFromReader(r) + boolReaded, err = ReadBoolFromReader(reader) if err != nil { return err } - meta.IsMulAssign = b + meta.IsMulAssign = boolReaded return nil } @@ -1483,28 +1483,28 @@ func (meta *ConstantMeta) GetASTType() NodeType { // WriteMetaTo write basic AST Node information meta data into writer. // One should not use this function directly, unless for testing // serialization of single ASTNode. -func (meta *ConstantMeta) WriteMetaTo(w io.Writer) error { - err := meta.NodeMeta.WriteMetaTo(w) +func (meta *ConstantMeta) WriteMetaTo(writer io.Writer) error { + err := meta.NodeMeta.WriteMetaTo(writer) if err != nil { return err } - err = WriteIntToWriter(w, uint64(meta.ValueType)) + err = WriteIntToWriter(writer, uint64(meta.ValueType)) if err != nil { return err } - err = WriteIntToWriter(w, uint64(len(meta.ValueBytes))) + err = WriteIntToWriter(writer, uint64(len(meta.ValueBytes))) if err != nil { return err } - _, err = w.Write(meta.ValueBytes) + _, err = writer.Write(meta.ValueBytes) if err != nil { return err } - err = WriteBoolToWriter(w, meta.IsNil) + err = WriteBoolToWriter(writer, meta.IsNil) if err != nil { return err @@ -1516,33 +1516,33 @@ func (meta *ConstantMeta) WriteMetaTo(w io.Writer) error { // ReadMetaFrom write basic AST Node information meta data from reader. // One should not use this function directly, unless for testing // serialization of single ASTNode. -func (meta *ConstantMeta) ReadMetaFrom(r io.Reader) error { - err := meta.NodeMeta.ReadMetaFrom(r) +func (meta *ConstantMeta) ReadMetaFrom(reader io.Reader) error { + err := meta.NodeMeta.ReadMetaFrom(reader) if err != nil { return err } - i, err := ReadIntFromReader(r) + i, err := ReadIntFromReader(reader) if err != nil { return err } meta.ValueType = ValueType(i) - length, err := ReadIntFromReader(r) + length, err := ReadIntFromReader(reader) if err != nil { return err } byteArr := make([]byte, length) - _, err = r.Read(byteArr) + _, err = reader.Read(byteArr) if err != nil { return err } meta.ValueBytes = byteArr - b, err := ReadBoolFromReader(r) + b, err := ReadBoolFromReader(reader) if err != nil { return err @@ -1610,38 +1610,38 @@ func (meta *ExpressionMeta) GetASTType() NodeType { // WriteMetaTo write basic AST Node information meta data into writer. // One should not use this function directly, unless for testing // serialization of single ASTNode. -func (meta *ExpressionMeta) WriteMetaTo(w io.Writer) error { - err := meta.NodeMeta.WriteMetaTo(w) +func (meta *ExpressionMeta) WriteMetaTo(writer io.Writer) error { + err := meta.NodeMeta.WriteMetaTo(writer) if err != nil { return err } - err = WriteStringToWriter(w, meta.LeftExpressionID) + err = WriteStringToWriter(writer, meta.LeftExpressionID) if err != nil { return err } - err = WriteStringToWriter(w, meta.RightExpressionID) + err = WriteStringToWriter(writer, meta.RightExpressionID) if err != nil { return err } - err = WriteStringToWriter(w, meta.SingleExpressionID) + err = WriteStringToWriter(writer, meta.SingleExpressionID) if err != nil { return err } - err = WriteStringToWriter(w, meta.ExpressionAtomID) + err = WriteStringToWriter(writer, meta.ExpressionAtomID) if err != nil { return err } - err = WriteIntToWriter(w, uint64(meta.Operator)) + err = WriteIntToWriter(writer, uint64(meta.Operator)) if err != nil { return err } - err = WriteBoolToWriter(w, meta.Negated) + err = WriteBoolToWriter(writer, meta.Negated) if err != nil { return err @@ -1653,43 +1653,43 @@ func (meta *ExpressionMeta) WriteMetaTo(w io.Writer) error { // ReadMetaFrom write basic AST Node information meta data from reader. // One should not use this function directly, unless for testing // serialization of single ASTNode. -func (meta *ExpressionMeta) ReadMetaFrom(r io.Reader) error { - err := meta.NodeMeta.ReadMetaFrom(r) +func (meta *ExpressionMeta) ReadMetaFrom(reader io.Reader) error { + err := meta.NodeMeta.ReadMetaFrom(reader) if err != nil { return err } - s, err := ReadStringFromReader(r) + theString, err := ReadStringFromReader(reader) if err != nil { return err } - meta.LeftExpressionID = s - s, err = ReadStringFromReader(r) + meta.LeftExpressionID = theString + theString, err = ReadStringFromReader(reader) if err != nil { return err } - meta.RightExpressionID = s - s, err = ReadStringFromReader(r) + meta.RightExpressionID = theString + theString, err = ReadStringFromReader(reader) if err != nil { return err } - meta.SingleExpressionID = s - s, err = ReadStringFromReader(r) + meta.SingleExpressionID = theString + theString, err = ReadStringFromReader(reader) if err != nil { return err } - meta.ExpressionAtomID = s - i, err := ReadIntFromReader(r) + meta.ExpressionAtomID = theString + i, err := ReadIntFromReader(reader) if err != nil { return err } meta.Operator = int(i) - b, err := ReadBoolFromReader(r) + b, err := ReadBoolFromReader(reader) if err != nil { return err @@ -1763,43 +1763,43 @@ func (meta *ExpressionAtomMeta) GetASTType() NodeType { // WriteMetaTo write basic AST Node information meta data into writer. // One should not use this function directly, unless for testing // serialization of single ASTNode. -func (meta *ExpressionAtomMeta) WriteMetaTo(w io.Writer) error { - err := meta.NodeMeta.WriteMetaTo(w) +func (meta *ExpressionAtomMeta) WriteMetaTo(writer io.Writer) error { + err := meta.NodeMeta.WriteMetaTo(writer) if err != nil { return err } - err = WriteStringToWriter(w, meta.VariableName) + err = WriteStringToWriter(writer, meta.VariableName) if err != nil { return err } - err = WriteStringToWriter(w, meta.ConstantID) + err = WriteStringToWriter(writer, meta.ConstantID) if err != nil { return err } - err = WriteStringToWriter(w, meta.FunctionCallID) + err = WriteStringToWriter(writer, meta.FunctionCallID) if err != nil { return err } - err = WriteStringToWriter(w, meta.VariableID) + err = WriteStringToWriter(writer, meta.VariableID) if err != nil { return err } - err = WriteBoolToWriter(w, meta.Negated) + err = WriteBoolToWriter(writer, meta.Negated) if err != nil { return err } - err = WriteStringToWriter(w, meta.ExpressionAtomID) + err = WriteStringToWriter(writer, meta.ExpressionAtomID) if err != nil { return err } - err = WriteStringToWriter(w, meta.ArrayMapSelectorID) + err = WriteStringToWriter(writer, meta.ArrayMapSelectorID) if err != nil { return err @@ -1811,54 +1811,54 @@ func (meta *ExpressionAtomMeta) WriteMetaTo(w io.Writer) error { // ReadMetaFrom write basic AST Node information meta data from reader. // One should not use this function directly, unless for testing // serialization of single ASTNode. -func (meta *ExpressionAtomMeta) ReadMetaFrom(r io.Reader) error { - err := meta.NodeMeta.ReadMetaFrom(r) +func (meta *ExpressionAtomMeta) ReadMetaFrom(reader io.Reader) error { + err := meta.NodeMeta.ReadMetaFrom(reader) if err != nil { return err } - s, err := ReadStringFromReader(r) + stringFromReader, err := ReadStringFromReader(reader) if err != nil { return err } - meta.VariableName = s - s, err = ReadStringFromReader(r) + meta.VariableName = stringFromReader + stringFromReader, err = ReadStringFromReader(reader) if err != nil { return err } - meta.ConstantID = s - s, err = ReadStringFromReader(r) + meta.ConstantID = stringFromReader + stringFromReader, err = ReadStringFromReader(reader) if err != nil { return err } - meta.FunctionCallID = s - s, err = ReadStringFromReader(r) + meta.FunctionCallID = stringFromReader + stringFromReader, err = ReadStringFromReader(reader) if err != nil { return err } - meta.VariableID = s - b, err := ReadBoolFromReader(r) + meta.VariableID = stringFromReader + b, err := ReadBoolFromReader(reader) if err != nil { return err } meta.Negated = b - s, err = ReadStringFromReader(r) + stringFromReader, err = ReadStringFromReader(reader) if err != nil { return err } - meta.ExpressionAtomID = s - s, err = ReadStringFromReader(r) + meta.ExpressionAtomID = stringFromReader + stringFromReader, err = ReadStringFromReader(reader) if err != nil { return err } - meta.ArrayMapSelectorID = s + meta.ArrayMapSelectorID = stringFromReader return nil } @@ -1901,18 +1901,18 @@ func (meta *FunctionCallMeta) GetASTType() NodeType { // WriteMetaTo write basic AST Node information meta data into writer. // One should not use this function directly, unless for testing // serialization of single ASTNode. -func (meta *FunctionCallMeta) WriteMetaTo(w io.Writer) error { - err := meta.NodeMeta.WriteMetaTo(w) +func (meta *FunctionCallMeta) WriteMetaTo(writer io.Writer) error { + err := meta.NodeMeta.WriteMetaTo(writer) if err != nil { return err } - err = WriteStringToWriter(w, meta.FunctionName) + err = WriteStringToWriter(writer, meta.FunctionName) if err != nil { return err } - err = WriteStringToWriter(w, meta.ArgumentListID) + err = WriteStringToWriter(writer, meta.ArgumentListID) if err != nil { return err @@ -1924,24 +1924,24 @@ func (meta *FunctionCallMeta) WriteMetaTo(w io.Writer) error { // ReadMetaFrom write basic AST Node information meta data from reader. // One should not use this function directly, unless for testing // serialization of single ASTNode. -func (meta *FunctionCallMeta) ReadMetaFrom(r io.Reader) error { - err := meta.NodeMeta.ReadMetaFrom(r) +func (meta *FunctionCallMeta) ReadMetaFrom(reader io.Reader) error { + err := meta.NodeMeta.ReadMetaFrom(reader) if err != nil { return err } - s, err := ReadStringFromReader(r) + stringFromReader, err := ReadStringFromReader(reader) if err != nil { return err } - meta.FunctionName = s - s, err = ReadStringFromReader(r) + meta.FunctionName = stringFromReader + stringFromReader, err = ReadStringFromReader(reader) if err != nil { return err } - meta.ArgumentListID = s + meta.ArgumentListID = stringFromReader return nil } @@ -2000,33 +2000,33 @@ func (meta *RuleEntryMeta) GetASTType() NodeType { // WriteMetaTo write basic AST Node information meta data into writer. // One should not use this function directly, unless for testing // serialization of single ASTNode. -func (meta *RuleEntryMeta) WriteMetaTo(w io.Writer) error { - err := meta.NodeMeta.WriteMetaTo(w) +func (meta *RuleEntryMeta) WriteMetaTo(writer io.Writer) error { + err := meta.NodeMeta.WriteMetaTo(writer) if err != nil { return err } - err = WriteStringToWriter(w, meta.RuleName) + err = WriteStringToWriter(writer, meta.RuleName) if err != nil { return err } - err = WriteStringToWriter(w, meta.RuleDescription) + err = WriteStringToWriter(writer, meta.RuleDescription) if err != nil { return err } - err = WriteIntToWriter(w, uint64(meta.Salience)) + err = WriteIntToWriter(writer, uint64(meta.Salience)) if err != nil { return err } - err = WriteStringToWriter(w, meta.WhenScopeID) + err = WriteStringToWriter(writer, meta.WhenScopeID) if err != nil { return err } - err = WriteStringToWriter(w, meta.ThenScopeID) + err = WriteStringToWriter(writer, meta.ThenScopeID) if err != nil { return err @@ -2038,42 +2038,42 @@ func (meta *RuleEntryMeta) WriteMetaTo(w io.Writer) error { // ReadMetaFrom write basic AST Node information meta data from reader. // One should not use this function directly, unless for testing // serialization of single ASTNode. -func (meta *RuleEntryMeta) ReadMetaFrom(r io.Reader) error { - err := meta.NodeMeta.ReadMetaFrom(r) +func (meta *RuleEntryMeta) ReadMetaFrom(reader io.Reader) error { + err := meta.NodeMeta.ReadMetaFrom(reader) if err != nil { return err } - s, err := ReadStringFromReader(r) + stringFromReader, err := ReadStringFromReader(reader) if err != nil { return err } - meta.RuleName = s - s, err = ReadStringFromReader(r) + meta.RuleName = stringFromReader + stringFromReader, err = ReadStringFromReader(reader) if err != nil { return err } - meta.RuleDescription = s - i, err := ReadIntFromReader(r) + meta.RuleDescription = stringFromReader + i, err := ReadIntFromReader(reader) if err != nil { return err } meta.Salience = int(i) - s, err = ReadStringFromReader(r) + stringFromReader, err = ReadStringFromReader(reader) if err != nil { return err } - meta.WhenScopeID = s - s, err = ReadStringFromReader(r) + meta.WhenScopeID = stringFromReader + stringFromReader, err = ReadStringFromReader(reader) if err != nil { return err } - meta.ThenScopeID = s + meta.ThenScopeID = stringFromReader return nil } @@ -2117,18 +2117,18 @@ func (meta *ThenExpressionMeta) GetASTType() NodeType { // WriteMetaTo write basic AST Node information meta data into writer. // One should not use this function directly, unless for testing // serialization of single ASTNode. -func (meta *ThenExpressionMeta) WriteMetaTo(w io.Writer) error { - err := meta.NodeMeta.WriteMetaTo(w) +func (meta *ThenExpressionMeta) WriteMetaTo(writer io.Writer) error { + err := meta.NodeMeta.WriteMetaTo(writer) if err != nil { return err } - err = WriteStringToWriter(w, meta.AssignmentID) + err = WriteStringToWriter(writer, meta.AssignmentID) if err != nil { return err } - err = WriteStringToWriter(w, meta.ExpressionAtomID) + err = WriteStringToWriter(writer, meta.ExpressionAtomID) if err != nil { return err @@ -2140,24 +2140,24 @@ func (meta *ThenExpressionMeta) WriteMetaTo(w io.Writer) error { // ReadMetaFrom write basic AST Node information meta data from reader. // One should not use this function directly, unless for testing // serialization of single ASTNode. -func (meta *ThenExpressionMeta) ReadMetaFrom(r io.Reader) error { - err := meta.NodeMeta.ReadMetaFrom(r) +func (meta *ThenExpressionMeta) ReadMetaFrom(reader io.Reader) error { + err := meta.NodeMeta.ReadMetaFrom(reader) if err != nil { return err } - s, err := ReadStringFromReader(r) + theString, err := ReadStringFromReader(reader) if err != nil { return err } - meta.AssignmentID = s - s, err = ReadStringFromReader(r) + meta.AssignmentID = theString + theString, err = ReadStringFromReader(reader) if err != nil { return err } - meta.ExpressionAtomID = s + meta.ExpressionAtomID = theString return nil } @@ -2202,21 +2202,21 @@ func (meta *ThenExpressionListMeta) GetASTType() NodeType { // WriteMetaTo write basic AST Node information meta data into writer. // One should not use this function directly, unless for testing // serialization of single ASTNode. -func (meta *ThenExpressionListMeta) WriteMetaTo(w io.Writer) error { - err := meta.NodeMeta.WriteMetaTo(w) +func (meta *ThenExpressionListMeta) WriteMetaTo(writer io.Writer) error { + err := meta.NodeMeta.WriteMetaTo(writer) if err != nil { return err } - err = WriteIntToWriter(w, uint64(len(meta.ThenExpressionIDs))) + err = WriteIntToWriter(writer, uint64(len(meta.ThenExpressionIDs))) if err != nil { return err } for _, v := range meta.ThenExpressionIDs { - err = WriteStringToWriter(w, v) + err = WriteStringToWriter(writer, v) if err != nil { return err @@ -2229,27 +2229,27 @@ func (meta *ThenExpressionListMeta) WriteMetaTo(w io.Writer) error { // ReadMetaFrom write basic AST Node information meta data from reader. // One should not use this function directly, unless for testing // serialization of single ASTNode. -func (meta *ThenExpressionListMeta) ReadMetaFrom(r io.Reader) error { - err := meta.NodeMeta.ReadMetaFrom(r) +func (meta *ThenExpressionListMeta) ReadMetaFrom(reader io.Reader) error { + err := meta.NodeMeta.ReadMetaFrom(reader) if err != nil { return err } - count, err := ReadIntFromReader(r) + count, err := ReadIntFromReader(reader) if err != nil { return err } meta.ThenExpressionIDs = make([]string, count) - for i := uint64(0); i < count; i++ { - s, err := ReadStringFromReader(r) + for index := uint64(0); index < count; index++ { + s, err := ReadStringFromReader(reader) if err != nil { return err } - meta.ThenExpressionIDs[i] = s + meta.ThenExpressionIDs[index] = s } return nil @@ -2288,13 +2288,13 @@ func (meta *ThenScopeMeta) GetASTType() NodeType { // WriteMetaTo write basic AST Node information meta data into writer. // One should not use this function directly, unless for testing // serialization of single ASTNode. -func (meta *ThenScopeMeta) WriteMetaTo(w io.Writer) error { - err := meta.NodeMeta.WriteMetaTo(w) +func (meta *ThenScopeMeta) WriteMetaTo(writer io.Writer) error { + err := meta.NodeMeta.WriteMetaTo(writer) if err != nil { return err } - err = WriteStringToWriter(w, meta.ThenExpressionListID) + err = WriteStringToWriter(writer, meta.ThenExpressionListID) if err != nil { return err @@ -2306,13 +2306,13 @@ func (meta *ThenScopeMeta) WriteMetaTo(w io.Writer) error { // ReadMetaFrom write basic AST Node information meta data from reader. // One should not use this function directly, unless for testing // serialization of single ASTNode. -func (meta *ThenScopeMeta) ReadMetaFrom(r io.Reader) error { - err := meta.NodeMeta.ReadMetaFrom(r) +func (meta *ThenScopeMeta) ReadMetaFrom(reader io.Reader) error { + err := meta.NodeMeta.ReadMetaFrom(reader) if err != nil { return err } - s, err := ReadStringFromReader(r) + s, err := ReadStringFromReader(reader) if err != nil { return err @@ -2366,23 +2366,23 @@ func (meta *VariableMeta) GetASTType() NodeType { // WriteMetaTo write basic AST Node information meta data into writer. // One should not use this function directly, unless for testing // serialization of single ASTNode. -func (meta *VariableMeta) WriteMetaTo(w io.Writer) error { - err := meta.NodeMeta.WriteMetaTo(w) +func (meta *VariableMeta) WriteMetaTo(writer io.Writer) error { + err := meta.NodeMeta.WriteMetaTo(writer) if err != nil { return err } - err = WriteStringToWriter(w, meta.Name) + err = WriteStringToWriter(writer, meta.Name) if err != nil { return err } - err = WriteStringToWriter(w, meta.VariableID) + err = WriteStringToWriter(writer, meta.VariableID) if err != nil { return err } - err = WriteStringToWriter(w, meta.ArrayMapSelectorID) + err = WriteStringToWriter(writer, meta.ArrayMapSelectorID) if err != nil { return err @@ -2394,30 +2394,30 @@ func (meta *VariableMeta) WriteMetaTo(w io.Writer) error { // ReadMetaFrom write basic AST Node information meta data from reader. // One should not use this function directly, unless for testing // serialization of single ASTNode. -func (meta *VariableMeta) ReadMetaFrom(r io.Reader) error { - err := meta.NodeMeta.ReadMetaFrom(r) +func (meta *VariableMeta) ReadMetaFrom(reader io.Reader) error { + err := meta.NodeMeta.ReadMetaFrom(reader) if err != nil { return err } - s, err := ReadStringFromReader(r) + stringFromReader, err := ReadStringFromReader(reader) if err != nil { return err } - meta.Name = s - s, err = ReadStringFromReader(r) + meta.Name = stringFromReader + stringFromReader, err = ReadStringFromReader(reader) if err != nil { return err } - meta.VariableID = s - s, err = ReadStringFromReader(r) + meta.VariableID = stringFromReader + stringFromReader, err = ReadStringFromReader(reader) if err != nil { return err } - meta.ArrayMapSelectorID = s + meta.ArrayMapSelectorID = stringFromReader return nil } @@ -2455,13 +2455,13 @@ func (meta *WhenScopeMeta) GetASTType() NodeType { // WriteMetaTo write basic AST Node information meta data into writer. // One should not use this function directly, unless for testing // serialization of single ASTNode. -func (meta *WhenScopeMeta) WriteMetaTo(w io.Writer) error { - err := meta.NodeMeta.WriteMetaTo(w) +func (meta *WhenScopeMeta) WriteMetaTo(writer io.Writer) error { + err := meta.NodeMeta.WriteMetaTo(writer) if err != nil { return err } - err = WriteStringToWriter(w, meta.ExpressionID) + err = WriteStringToWriter(writer, meta.ExpressionID) if err != nil { return err @@ -2473,13 +2473,13 @@ func (meta *WhenScopeMeta) WriteMetaTo(w io.Writer) error { // ReadMetaFrom write basic AST Node information meta data from reader. // One should not use this function directly, unless for testing // serialization of single ASTNode. -func (meta *WhenScopeMeta) ReadMetaFrom(r io.Reader) error { - err := meta.NodeMeta.ReadMetaFrom(r) +func (meta *WhenScopeMeta) ReadMetaFrom(reader io.Reader) error { + err := meta.NodeMeta.ReadMetaFrom(reader) if err != nil { return err } - s, err := ReadStringFromReader(r) + s, err := ReadStringFromReader(reader) if err != nil { return err @@ -2519,38 +2519,38 @@ func WriteFull(w io.Writer, bytes []byte) (int, error) { // WriteStringToWriter write a string into writer. // the structure is that there's length value written // prior writing the actual string. -func WriteStringToWriter(w io.Writer, s string) error { +func WriteStringToWriter(writer io.Writer, s string) error { length := make([]byte, 8) data := []byte(s) binary.LittleEndian.PutUint64(length, uint64(len(data))) - c, err := WriteFull(w, length) + writeCount, err := WriteFull(writer, length) - TotalWrite += uint64(c) + TotalWrite += uint64(writeCount) if err != nil { return err } - c, err = WriteFull(w, data) - TotalWrite += uint64(c) + writeCount, err = WriteFull(writer, data) + TotalWrite += uint64(writeCount) WriteCount++ return err } // ReadStringFromReader read a string from reader. -func ReadStringFromReader(r io.Reader) (string, error) { +func ReadStringFromReader(reader io.Reader) (string, error) { length := make([]byte, 8) - c, err := io.ReadFull(r, length) - TotalRead += uint64(c) + counter, err := io.ReadFull(reader, length) + TotalRead += uint64(counter) if err != nil { return "", err } strLen := binary.LittleEndian.Uint64(length) strByte := make([]byte, int(strLen)) - c, err = io.ReadFull(r, strByte) - TotalRead += uint64(c) + counter, err = io.ReadFull(reader, strByte) + TotalRead += uint64(counter) if err != nil { return "", err @@ -2587,14 +2587,14 @@ func ReadIntFromReader(r io.Reader) (uint64, error) { } // WriteBoolToWriter writes a simple boolean into writer -func WriteBoolToWriter(w io.Writer, b bool) error { +func WriteBoolToWriter(writer io.Writer, aBoolean bool) error { data := make([]byte, 1) - if b { + if aBoolean { data[0] = 1 } else { data[0] = 0 } - c, err := WriteFull(w, data) + c, err := WriteFull(writer, data) TotalWrite += uint64(c) return err diff --git a/ast/WorkingMemory.go b/ast/WorkingMemory.go index cc47ba28..46b81078 100755 --- a/ast/WorkingMemory.go +++ b/ast/WorkingMemory.go @@ -51,44 +51,44 @@ type WorkingMemory struct { } // MakeCatalog create a catalog entry of this working memory -func (e *WorkingMemory) MakeCatalog(cat *Catalog) { - cat.MemoryName = e.Name - cat.MemoryVersion = e.Version +func (workingMem *WorkingMemory) MakeCatalog(cat *Catalog) { + cat.MemoryName = workingMem.Name + cat.MemoryVersion = workingMem.Version cat.MemoryExpressionSnapshotMap = make(map[string]string) - for k, v := range e.expressionSnapshotMap { - cat.MemoryExpressionSnapshotMap[k] = v.AstID + for key, value := range workingMem.expressionSnapshotMap { + cat.MemoryExpressionSnapshotMap[key] = value.AstID } cat.MemoryExpressionAtomSnapshotMap = make(map[string]string) - for k, v := range e.expressionAtomSnapshotMap { - cat.MemoryExpressionAtomSnapshotMap[k] = v.AstID + for key, value := range workingMem.expressionAtomSnapshotMap { + cat.MemoryExpressionAtomSnapshotMap[key] = value.AstID } cat.MemoryVariableSnapshotMap = make(map[string]string) - for k, v := range e.variableSnapshotMap { - cat.MemoryVariableSnapshotMap[k] = v.AstID + for key, value := range workingMem.variableSnapshotMap { + cat.MemoryVariableSnapshotMap[key] = value.AstID } cat.MemoryExpressionVariableMap = make(map[string][]string) - for k, v := range e.expressionVariableMap { - cat.MemoryExpressionVariableMap[k.AstID] = make([]string, len(v)) - for i, j := range v { - cat.MemoryExpressionVariableMap[k.AstID][i] = j.AstID + for key, value := range workingMem.expressionVariableMap { + cat.MemoryExpressionVariableMap[key.AstID] = make([]string, len(value)) + for i, j := range value { + cat.MemoryExpressionVariableMap[key.AstID][i] = j.AstID } } cat.MemoryExpressionAtomVariableMap = make(map[string][]string) - for k, v := range e.expressionAtomVariableMap { - cat.MemoryExpressionAtomVariableMap[k.AstID] = make([]string, len(v)) - for i, j := range v { - cat.MemoryExpressionAtomVariableMap[k.AstID][i] = j.AstID + for key, value := range workingMem.expressionAtomVariableMap { + cat.MemoryExpressionAtomVariableMap[key.AstID] = make([]string, len(value)) + for i, j := range value { + cat.MemoryExpressionAtomVariableMap[key.AstID][i] = j.AstID } } } // DebugContent will shows the working memory mapping content -func (e *WorkingMemory) DebugContent() { +func (workingMem *WorkingMemory) DebugContent() { if AstLog.Level <= logger.DebugLevel { - for varName, vari := range e.variableSnapshotMap { + for varName, vari := range workingMem.variableSnapshotMap { AstLog.Debugf("Variable %s : %s : %s", varName, vari.GrlText, vari.AstID) - if exprs, ok := e.expressionVariableMap[vari]; ok { + if exprs, ok := workingMem.expressionVariableMap[vari]; ok { AstLog.Debugf(" %d expressions", len(exprs)) for i, ex := range exprs { AstLog.Debugf(" expr %d: %s", i, ex.GrlText) @@ -96,7 +96,7 @@ func (e *WorkingMemory) DebugContent() { } else { AstLog.Debugf(" no expressions mapped for variable %s", vari.GrlText) } - if expratms, ok := e.expressionAtomVariableMap[vari]; ok { + if expratms, ok := workingMem.expressionAtomVariableMap[vari]; ok { AstLog.Debugf(" %d expression atoms", len(expratms)) for i, ex := range expratms { AstLog.Debugf(" expr atm %d: %s", i, ex.GrlText) @@ -109,32 +109,32 @@ func (e *WorkingMemory) DebugContent() { } // Equals shallowly equals check this Working Memory against other working memory -func (e *WorkingMemory) Equals(that *WorkingMemory) bool { - if e.Name != that.Name { +func (workingMem *WorkingMemory) Equals(that *WorkingMemory) bool { + if workingMem.Name != that.Name { return false } - if e.Version != that.Version { + if workingMem.Version != that.Version { return false } - if len(e.expressionSnapshotMap) != len(that.expressionSnapshotMap) { + if len(workingMem.expressionSnapshotMap) != len(that.expressionSnapshotMap) { return false } - if len(e.expressionAtomSnapshotMap) != len(that.expressionAtomSnapshotMap) { + if len(workingMem.expressionAtomSnapshotMap) != len(that.expressionAtomSnapshotMap) { return false } - if len(e.variableSnapshotMap) != len(that.variableSnapshotMap) { + if len(workingMem.variableSnapshotMap) != len(that.variableSnapshotMap) { return false } - if len(e.expressionVariableMap) != len(that.expressionVariableMap) { + if len(workingMem.expressionVariableMap) != len(that.expressionVariableMap) { return false } - if len(e.expressionAtomVariableMap) != len(that.expressionAtomVariableMap) { + if len(workingMem.expressionAtomVariableMap) != len(that.expressionAtomVariableMap) { return false } @@ -143,13 +143,13 @@ func (e *WorkingMemory) Equals(that *WorkingMemory) bool { } // Clone will clone this WorkingMemory. The new clone will have an identical structure -func (e *WorkingMemory) Clone(cloneTable *pkg.CloneTable) *WorkingMemory { - AstLog.Debugf("Cloning working memory %s:%s", e.Name, e.Version) - clone := NewWorkingMemory(e.Name, e.Version) +func (workingMem *WorkingMemory) Clone(cloneTable *pkg.CloneTable) *WorkingMemory { + AstLog.Debugf("Cloning working memory %s:%s", workingMem.Name, workingMem.Version) + clone := NewWorkingMemory(workingMem.Name, workingMem.Version) - if e.expressionSnapshotMap != nil { - AstLog.Debugf("Cloning %d expressionSnapshotMap entries", len(e.expressionSnapshotMap)) - for k, expr := range e.expressionSnapshotMap { + if workingMem.expressionSnapshotMap != nil { + AstLog.Debugf("Cloning %d expressionSnapshotMap entries", len(workingMem.expressionSnapshotMap)) + for k, expr := range workingMem.expressionSnapshotMap { if cloneTable.IsCloned(expr.AstID) { clone.expressionSnapshotMap[k] = cloneTable.Records[expr.AstID].CloneInstance.(*Expression) } else { @@ -159,9 +159,9 @@ func (e *WorkingMemory) Clone(cloneTable *pkg.CloneTable) *WorkingMemory { } } - if e.expressionAtomSnapshotMap != nil { - AstLog.Debugf("Cloning %d expressionAtomSnapshotMap entries", len(e.expressionAtomSnapshotMap)) - for k, exprAtm := range e.expressionAtomSnapshotMap { + if workingMem.expressionAtomSnapshotMap != nil { + AstLog.Debugf("Cloning %d expressionAtomSnapshotMap entries", len(workingMem.expressionAtomSnapshotMap)) + for k, exprAtm := range workingMem.expressionAtomSnapshotMap { if cloneTable.IsCloned(exprAtm.AstID) { clone.expressionAtomSnapshotMap[k] = cloneTable.Records[exprAtm.AstID].CloneInstance.(*ExpressionAtom) } else { @@ -171,23 +171,23 @@ func (e *WorkingMemory) Clone(cloneTable *pkg.CloneTable) *WorkingMemory { } } - if e.variableSnapshotMap != nil { - AstLog.Debugf("Cloning %d variableSnapshotMap entries", len(e.variableSnapshotMap)) - for k, vari := range e.variableSnapshotMap { - if cloneTable.IsCloned(vari.AstID) { - clone.variableSnapshotMap[k] = cloneTable.Records[vari.AstID].CloneInstance.(*Variable) + if workingMem.variableSnapshotMap != nil { + AstLog.Debugf("Cloning %d variableSnapshotMap entries", len(workingMem.variableSnapshotMap)) + for key, variable := range workingMem.variableSnapshotMap { + if cloneTable.IsCloned(variable.AstID) { + clone.variableSnapshotMap[key] = cloneTable.Records[variable.AstID].CloneInstance.(*Variable) } else { - panic(fmt.Sprintf("variable %s is not on the clone table", vari.GrlText)) + panic(fmt.Sprintf("variable %s is not on the clone table", variable.GrlText)) } } } - if e.expressionVariableMap != nil { - AstLog.Debugf("Cloning %d expressionVariableMap entries", len(e.expressionVariableMap)) - for k, exprArr := range e.expressionVariableMap { - if cloneTable.IsCloned(k.AstID) { - clonedVari := cloneTable.Records[k.AstID].CloneInstance.(*Variable) + if workingMem.expressionVariableMap != nil { + AstLog.Debugf("Cloning %d expressionVariableMap entries", len(workingMem.expressionVariableMap)) + for key, exprArr := range workingMem.expressionVariableMap { + if cloneTable.IsCloned(key.AstID) { + clonedVari := cloneTable.Records[key.AstID].CloneInstance.(*Variable) clone.expressionVariableMap[clonedVari] = make([]*Expression, len(exprArr)) for k2, expr := range exprArr { if cloneTable.IsCloned(expr.AstID) { @@ -199,16 +199,16 @@ func (e *WorkingMemory) Clone(cloneTable *pkg.CloneTable) *WorkingMemory { } } else { - panic(fmt.Sprintf("variable %s is not on the clone table", k.GrlText)) + panic(fmt.Sprintf("variable %s is not on the clone table", key.GrlText)) } } } - if e.expressionAtomVariableMap != nil { - AstLog.Debugf("Cloning %d expressionAtomVariableMap entries", len(e.expressionAtomVariableMap)) - for k, exprAtmArr := range e.expressionAtomVariableMap { - if cloneTable.IsCloned(k.AstID) { - clonedVari := cloneTable.Records[k.AstID].CloneInstance.(*Variable) + if workingMem.expressionAtomVariableMap != nil { + AstLog.Debugf("Cloning %d expressionAtomVariableMap entries", len(workingMem.expressionAtomVariableMap)) + for key, exprAtmArr := range workingMem.expressionAtomVariableMap { + if cloneTable.IsCloned(key.AstID) { + clonedVari := cloneTable.Records[key.AstID].CloneInstance.(*Variable) clone.expressionAtomVariableMap[clonedVari] = make([]*ExpressionAtom, len(exprAtmArr)) for k2, expr := range exprAtmArr { if cloneTable.IsCloned(expr.AstID) { @@ -220,12 +220,12 @@ func (e *WorkingMemory) Clone(cloneTable *pkg.CloneTable) *WorkingMemory { } } else { - panic(fmt.Sprintf("variable %s is not on the clone table", k.GrlText)) + panic(fmt.Sprintf("variable %s is not on the clone table", key.GrlText)) } } } - if e.Equals(clone) { + if workingMem.Equals(clone) { clone.DebugContent() return clone @@ -235,103 +235,103 @@ func (e *WorkingMemory) Clone(cloneTable *pkg.CloneTable) *WorkingMemory { } // IndexVariables will index all expression and expression atoms that contains a speciffic variable name -func (e *WorkingMemory) IndexVariables() { +func (workingMem *WorkingMemory) IndexVariables() { if AstLog.Level <= logger.DebugLevel { - AstLog.Debugf("Indexing %d expressions, %d expression atoms and %d variables.", len(e.expressionSnapshotMap), len(e.expressionAtomSnapshotMap), len(e.variableSnapshotMap)) + AstLog.Debugf("Indexing %d expressions, %d expression atoms and %d variables.", len(workingMem.expressionSnapshotMap), len(workingMem.expressionAtomSnapshotMap), len(workingMem.variableSnapshotMap)) } start := time.Now() defer func() { dur := time.Since(start) AstLog.Tracef("Working memory indexing takes %d ms", dur/time.Millisecond) }() - e.expressionVariableMap = make(map[*Variable][]*Expression) - e.expressionAtomVariableMap = make(map[*Variable][]*ExpressionAtom) + workingMem.expressionVariableMap = make(map[*Variable][]*Expression) + workingMem.expressionAtomVariableMap = make(map[*Variable][]*ExpressionAtom) - for varSnapshot, variable := range e.variableSnapshotMap { - if _, ok := e.expressionVariableMap[variable]; ok == false { - e.expressionVariableMap[variable] = make([]*Expression, 0) + for varSnapshot, variable := range workingMem.variableSnapshotMap { + if _, ok := workingMem.expressionVariableMap[variable]; ok == false { + workingMem.expressionVariableMap[variable] = make([]*Expression, 0) } - if _, ok := e.expressionAtomVariableMap[variable]; ok == false { - e.expressionAtomVariableMap[variable] = make([]*ExpressionAtom, 0) + if _, ok := workingMem.expressionAtomVariableMap[variable]; ok == false { + workingMem.expressionAtomVariableMap[variable] = make([]*ExpressionAtom, 0) } - for exprSnapshot, expr := range e.expressionSnapshotMap { + for exprSnapshot, expr := range workingMem.expressionSnapshotMap { if strings.Contains(exprSnapshot, varSnapshot) { - e.expressionVariableMap[variable] = append(e.expressionVariableMap[variable], expr) + workingMem.expressionVariableMap[variable] = append(workingMem.expressionVariableMap[variable], expr) } } - for exprAtmSnapshot, exprAtm := range e.expressionAtomSnapshotMap { + for exprAtmSnapshot, exprAtm := range workingMem.expressionAtomSnapshotMap { if strings.Contains(exprAtmSnapshot, varSnapshot) { - e.expressionAtomVariableMap[variable] = append(e.expressionAtomVariableMap[variable], exprAtm) + workingMem.expressionAtomVariableMap[variable] = append(workingMem.expressionAtomVariableMap[variable], exprAtm) } } } - e.DebugContent() + workingMem.DebugContent() } // AddExpression will add expression into its map if the expression signature is unique // if the expression is already in its map, it will return one from the map. -func (e *WorkingMemory) AddExpression(exp *Expression) *Expression { +func (workingMem *WorkingMemory) AddExpression(exp *Expression) *Expression { snapshot := exp.GetSnapshot() - if expr, ok := e.expressionSnapshotMap[snapshot]; ok { - AstLog.Tracef("%s : Ignored Expression Snapshot : %s", e.ID, snapshot) + if expr, ok := workingMem.expressionSnapshotMap[snapshot]; ok { + AstLog.Tracef("%s : Ignored Expression Snapshot : %s", workingMem.ID, snapshot) return expr } - AstLog.Tracef("%s : Added Expression Snapshot : %s", e.ID, snapshot) - e.expressionSnapshotMap[snapshot] = exp + AstLog.Tracef("%s : Added Expression Snapshot : %s", workingMem.ID, snapshot) + workingMem.expressionSnapshotMap[snapshot] = exp return exp } // AddExpressionAtom will add expression atom into its map if the expression signature is unique // if the expression is already in its map, it will return one from the map. -func (e *WorkingMemory) AddExpressionAtom(exp *ExpressionAtom) *ExpressionAtom { +func (workingMem *WorkingMemory) AddExpressionAtom(exp *ExpressionAtom) *ExpressionAtom { snapshot := exp.GetSnapshot() - if expr, ok := e.expressionAtomSnapshotMap[snapshot]; ok { - AstLog.Tracef("%s : Ignored ExpressionAtom Snapshot : %s", e.ID, snapshot) + if expr, ok := workingMem.expressionAtomSnapshotMap[snapshot]; ok { + AstLog.Tracef("%s : Ignored ExpressionAtom Snapshot : %s", workingMem.ID, snapshot) return expr } - AstLog.Tracef("%s : Added ExpressionAtom Snapshot : %s", e.ID, snapshot) - e.expressionAtomSnapshotMap[snapshot] = exp + AstLog.Tracef("%s : Added ExpressionAtom Snapshot : %s", workingMem.ID, snapshot) + workingMem.expressionAtomSnapshotMap[snapshot] = exp return exp } // AddVariable will add variable into its map if the expression signature is unique // if the expression is already in its map, it will return one from the map. -func (e *WorkingMemory) AddVariable(vari *Variable) *Variable { +func (workingMem *WorkingMemory) AddVariable(vari *Variable) *Variable { snapshot := vari.GetSnapshot() - if v, ok := e.variableSnapshotMap[snapshot]; ok { - AstLog.Tracef("%s : Ignored Variable Snapshot : %s", e.ID, snapshot) + if v, ok := workingMem.variableSnapshotMap[snapshot]; ok { + AstLog.Tracef("%s : Ignored Variable Snapshot : %s", workingMem.ID, snapshot) return v } - AstLog.Tracef("%s : Added Variable Snapshot : %s", e.ID, snapshot) - e.variableSnapshotMap[snapshot] = vari + AstLog.Tracef("%s : Added Variable Snapshot : %s", workingMem.ID, snapshot) + workingMem.variableSnapshotMap[snapshot] = vari return vari } // Reset will reset the evaluated status of a specific variable if its contains a variable name in its signature. // Returns true if any expression was reset, false if otherwise -func (e *WorkingMemory) Reset(name string) bool { +func (workingMem *WorkingMemory) Reset(name string) bool { AstLog.Tracef("------- resetting %s", name) - for _, vari := range e.variableSnapshotMap { + for _, vari := range workingMem.variableSnapshotMap { if vari.GrlText == name { - return e.ResetVariable(vari) + return workingMem.ResetVariable(vari) } } - for snap, expr := range e.expressionSnapshotMap { + for snap, expr := range workingMem.expressionSnapshotMap { if strings.Contains(snap, name) || strings.Contains(expr.GrlText, name) { expr.Evaluated = false } } - for snap, expr := range e.expressionAtomSnapshotMap { + for snap, expr := range workingMem.expressionAtomSnapshotMap { if strings.Contains(snap, name) || strings.Contains(expr.GrlText, name) { expr.Evaluated = false } @@ -342,13 +342,13 @@ func (e *WorkingMemory) Reset(name string) bool { // ResetVariable will reset the evaluated status of a specific expression if its contains a variable name in its signature. // Returns true if any expression was reset, false if otherwise -func (e *WorkingMemory) ResetVariable(variable *Variable) bool { +func (workingMem *WorkingMemory) ResetVariable(variable *Variable) bool { AstLog.Tracef("------- resetting variable %s : %s", variable.GrlText, variable.AstID) if AstLog.Level == logger.TraceLevel { - AstLog.Tracef("%s : Resetting %s", e.ID, variable.GetSnapshot()) + AstLog.Tracef("%s : Resetting %s", workingMem.ID, variable.GetSnapshot()) } reseted := false - if arr, ok := e.expressionVariableMap[variable]; ok { + if arr, ok := workingMem.expressionVariableMap[variable]; ok { for _, expr := range arr { AstLog.Tracef("------ reset expr : %s", expr.GrlText) expr.Evaluated = false @@ -357,7 +357,7 @@ func (e *WorkingMemory) ResetVariable(variable *Variable) bool { } else { AstLog.Warnf("No expression to reset for variable %s", variable.GrlText) } - if arr, ok := e.expressionAtomVariableMap[variable]; ok { + if arr, ok := workingMem.expressionAtomVariableMap[variable]; ok { for _, expr := range arr { AstLog.Tracef("------ reset expr atm : %s", expr.GrlText) expr.Evaluated = false @@ -372,13 +372,13 @@ func (e *WorkingMemory) ResetVariable(variable *Variable) bool { // ResetAll sets all expression evaluated status to false. // Returns true if any expression was reset, false if otherwise -func (e *WorkingMemory) ResetAll() bool { +func (workingMem *WorkingMemory) ResetAll() bool { reseted := false - for _, expr := range e.expressionSnapshotMap { + for _, expr := range workingMem.expressionSnapshotMap { expr.Evaluated = false reseted = true } - for _, expr := range e.expressionAtomSnapshotMap { + for _, expr := range workingMem.expressionAtomSnapshotMap { expr.Evaluated = false reseted = true } diff --git a/ast/unique/Unique.go b/ast/unique/Unique.go index 1168ed0d..64c39c4d 100755 --- a/ast/unique/Unique.go +++ b/ast/unique/Unique.go @@ -31,13 +31,13 @@ var ( func NewID() string { mutex.Lock() defer mutex.Unlock() - MS := time.Now().Unix() - if lastMS == MS { + millisUnix := time.Now().Unix() + if lastMS == millisUnix { offset++ return fmt.Sprintf("%d-%d", lastMS, offset) } - lastMS = MS + lastMS = millisUnix offset = 0 return fmt.Sprintf("%d-%d", lastMS, offset) diff --git a/builder/RuleBuilder.go b/builder/RuleBuilder.go index f51ddd64..5887e6df 100755 --- a/builder/RuleBuilder.go +++ b/builder/RuleBuilder.go @@ -151,13 +151,13 @@ func (builder *RuleBuilder) BuildRuleFromResource(name, version string, resource stream := antlr.NewCommonTokenStream(lexer, antlr.TokenDefaultChannel) - kb := builder.KnowledgeLibrary.GetKnowledgeBase(name, version) - if kb == nil { + knowledgeBase := builder.KnowledgeLibrary.GetKnowledgeBase(name, version) + if knowledgeBase == nil { return fmt.Errorf("KnowledgeBase %s:%s is not in this library", name, version) } - listener := antlr2.NewGruleV3ParserListener(kb, errReporter) + listener := antlr2.NewGruleV3ParserListener(knowledgeBase, errReporter) psr := parser.Newgrulev3Parser(stream) @@ -169,13 +169,13 @@ func (builder *RuleBuilder) BuildRuleFromResource(name, version string, resource grl := listener.Grl for _, ruleEntry := range grl.RuleEntries { - err := kb.AddRuleEntry(ruleEntry) + err := knowledgeBase.AddRuleEntry(ruleEntry) if err != nil && err.Error() != "rule entry TestNoDesc already exist" { BuilderLog.Tracef("warning while adding rule entry : %s. got %s, possibly already added by antlr listener", ruleEntry.RuleName, err.Error()) } } - kb.WorkingMemory.IndexVariables() + knowledgeBase.WorkingMemory.IndexVariables() // Get the loading duration. dur := time.Now().Sub(startTime) diff --git a/editor/EvaluationRoute.go b/editor/EvaluationRoute.go index fd5cf2cc..39544040 100644 --- a/editor/EvaluationRoute.go +++ b/editor/EvaluationRoute.go @@ -24,67 +24,67 @@ type EvaluateRequest struct { } func InitializeEvaluationRoute(router *mux.HyperMux) { - router.AddRoute("/evaluate", http.MethodPost, func(w http.ResponseWriter, r *http.Request) { - bodyBytes, err := ioutil.ReadAll(r.Body) + router.AddRoute("/evaluate", http.MethodPost, func(writer http.ResponseWriter, reader *http.Request) { + bodyBytes, err := ioutil.ReadAll(reader.Body) if err != nil { - w.WriteHeader(http.StatusInternalServerError) - _, _ = w.Write([]byte(fmt.Sprintf("error while reading body stream. got %v", err))) + writer.WriteHeader(http.StatusInternalServerError) + _, _ = writer.Write([]byte(fmt.Sprintf("error while reading body stream. got %v", err))) return } evReq := &EvaluateRequest{} err = json.Unmarshal(bodyBytes, evReq) if err != nil { - w.WriteHeader(http.StatusBadRequest) - _, _ = w.Write([]byte(fmt.Sprintf("wrong json format. got %v \n\n Json : %s", err, string(bodyBytes)))) + writer.WriteHeader(http.StatusBadRequest) + _, _ = writer.Write([]byte(fmt.Sprintf("wrong json format. got %v \n\n Json : %s", err, string(bodyBytes)))) return } dataContext := ast.NewDataContext() - for _, jd := range evReq.Input { - jsonByte, err := base64.StdEncoding.DecodeString(jd.JSONData) + for _, JSONDat := range evReq.Input { + jsonByte, err := base64.StdEncoding.DecodeString(JSONDat.JSONData) if err != nil { - w.WriteHeader(http.StatusBadRequest) - _, _ = w.Write([]byte(fmt.Sprintf("json data named %s should be sent using base64. got %v", jd.Name, err))) + writer.WriteHeader(http.StatusBadRequest) + _, _ = writer.Write([]byte(fmt.Sprintf("json data named %s should be sent using base64. got %v", JSONDat.Name, err))) return } - err = dataContext.AddJSON(jd.Name, jsonByte) + err = dataContext.AddJSON(JSONDat.Name, jsonByte) if err != nil { - w.WriteHeader(http.StatusBadRequest) - _, _ = w.Write([]byte(fmt.Sprintf("invalid JSON data named %s when add json to context got %v", jd.Name, err))) + writer.WriteHeader(http.StatusBadRequest) + _, _ = writer.Write([]byte(fmt.Sprintf("invalid JSON data named %s when add json to context got %v", JSONDat.Name, err))) return } } - lib := ast.NewKnowledgeLibrary() - rb := builder.NewRuleBuilder(lib) + knowledgeLibrary := ast.NewKnowledgeLibrary() + ruleBuilder := builder.NewRuleBuilder(knowledgeLibrary) grlByte, err := base64.StdEncoding.DecodeString(evReq.GrlText) if err != nil { - w.WriteHeader(http.StatusBadRequest) - _, _ = w.Write([]byte(fmt.Sprintf("GRL data should be sent using base64. got %v", err))) + writer.WriteHeader(http.StatusBadRequest) + _, _ = writer.Write([]byte(fmt.Sprintf("GRL data should be sent using base64. got %v", err))) return } - err = rb.BuildRuleFromResource("Evaluator", "0.0.1", pkg.NewBytesResource(grlByte)) + err = ruleBuilder.BuildRuleFromResource("Evaluator", "0.0.1", pkg.NewBytesResource(grlByte)) if err != nil { - w.WriteHeader(http.StatusBadRequest) - _, _ = w.Write([]byte(fmt.Sprintf("invalid GRL : %s", err.Error()))) + writer.WriteHeader(http.StatusBadRequest) + _, _ = writer.Write([]byte(fmt.Sprintf("invalid GRL : %s", err.Error()))) return } eng1 := &engine.GruleEngine{MaxCycle: 5} - kb := lib.NewKnowledgeBaseInstance("Evaluator", "0.0.1") + kb := knowledgeLibrary.NewKnowledgeBaseInstance("Evaluator", "0.0.1") err = eng1.Execute(dataContext, kb) if err != nil { - w.WriteHeader(http.StatusBadRequest) - _, _ = w.Write([]byte(fmt.Sprintf("Grule Error : %s", err.Error()))) + writer.WriteHeader(http.StatusBadRequest) + _, _ = writer.Write([]byte(fmt.Sprintf("Grule Error : %s", err.Error()))) return } @@ -96,14 +96,14 @@ func InitializeEvaluationRoute(router *mux.HyperMux) { resultBytes, err := json.Marshal(respData) if err != nil { - w.WriteHeader(http.StatusInternalServerError) - _, _ = w.Write([]byte(fmt.Sprintf("Error marshaling result : %s", err.Error()))) + writer.WriteHeader(http.StatusInternalServerError) + _, _ = writer.Write([]byte(fmt.Sprintf("Error marshaling result : %s", err.Error()))) return } - w.Header().Add("Content-Type", "application/json") - w.WriteHeader(http.StatusOK) - _, _ = w.Write(resultBytes) + writer.Header().Add("Content-Type", "application/json") + writer.WriteHeader(http.StatusOK) + _, _ = writer.Write(resultBytes) }) } diff --git a/editor/Server.go b/editor/Server.go index bb878d61..58c45853 100644 --- a/editor/Server.go +++ b/editor/Server.go @@ -41,13 +41,13 @@ func Start() { } }() - c := make(chan os.Signal, 1) + channel := make(chan os.Signal, 1) // We'll accept graceful shutdowns when quit via SIGINT (Ctrl+C) // SIGKILL, SIGQUIT or SIGTERM (Ctrl+/) will not be caught. - signal.Notify(c, os.Interrupt) + signal.Notify(channel, os.Interrupt) // Block until we receive our signal. - <-c + <-channel // Create a deadline to wait for. ctx, cancel := context.WithTimeout(context.Background(), wait) diff --git a/editor/StaticRoute.go b/editor/StaticRoute.go index 0daac25d..8f22fb98 100644 --- a/editor/StaticRoute.go +++ b/editor/StaticRoute.go @@ -2,7 +2,6 @@ package editor import ( "embed" - "fmt" mux "github.com/hyperjumptech/hyper-mux" "github.com/sirupsen/logrus" "grule-rule-engine/editor/mime" @@ -12,11 +11,7 @@ import ( ) //go:embed statics -var fs embed.FS - -var ( - errFileNotFound = fmt.Errorf("file not found") -) +var fileSyst embed.FS type FileData struct { Bytes []byte @@ -38,9 +33,9 @@ func GetPathTree(path string) []string { var entries []os.DirEntry var err error if strings.HasPrefix(path, "./") { - entries, err = fs.ReadDir(path[2:]) + entries, err = fileSyst.ReadDir(path[2:]) } else { - entries, err = fs.ReadDir(path) + entries, err = fileSyst.ReadDir(path) } ret := make([]string, 0) if err != nil { @@ -60,11 +55,11 @@ func GetPathTree(path string) []string { } func GetFile(path string) (*FileData, error) { - bytes, err := fs.ReadFile(path) + bytes, err := fileSyst.ReadFile(path) if err != nil { return nil, err } - mimeType, err := mime.MimeForFileName(path) + mimeType, err := mime.GetMimeForFileName(path) if err != nil { return &FileData{ diff --git a/editor/mime/Mime.go b/editor/mime/Mime.go index d2d685a9..22e0f303 100644 --- a/editor/mime/Mime.go +++ b/editor/mime/Mime.go @@ -721,16 +721,16 @@ var ( } ) -// MimeForFileName utility mimetype lokup -func MimeForFileName(filename string) (string, error) { +// GetMimeForFileName utility mimetype lokup +func GetMimeForFileName(filename string) (string, error) { reg := regexp.MustCompile(`[a-zA-Z0-9]+$`) if reg.MatchString(filename) { ext := reg.FindString(filename) - return MimeForExtension(ext) - } else { - return "", fmt.Errorf("can not find extension of file : %s", filename) + return GetMimeForExtension(ext) } + + return "", fmt.Errorf("can not find extension of file : %s", filename) } // IsPrintableChar checks if a type is printable @@ -738,10 +738,11 @@ func IsPrintableChar(char byte) bool { if char&0x80 == 0x80 { return false } else if char >= 0x20 || char == 0x09 || char == 0x0A || char == 0x0D || char == 239 { + return true - } else { - return false } + + return false } // IsAllPrintableChar checks for the printable chars @@ -756,12 +757,14 @@ func IsAllPrintableChar(bytes []byte) bool { return float64(nonprintable)/float64(len(bytes)) < 0.03 } -// MimeForExtension lookup short code to string extensions -func MimeForExtension(extension string) (string, error) { +// GetMimeForExtension lookup short code to string extensions +func GetMimeForExtension(extension string) (string, error) { ex := strings.ToLower(extension) if typ, ok := mimeMap[ex]; ok { + return typ, nil - } else { - return "", fmt.Errorf("no mime type for extension : %s", extension) } + + return "", fmt.Errorf("no mime type for extension : %s", extension) + } diff --git a/engine/GruleEngine.go b/engine/GruleEngine.go index 8a27d71b..2685c840 100755 --- a/engine/GruleEngine.go +++ b/engine/GruleEngine.go @@ -161,17 +161,17 @@ func (g *GruleEngine) ExecuteWithContext(ctx context.Context, dataCtx ast.IDataC // Select all rule entry that can be executed. log.Tracef("Select all rule entry that can be executed.") runnable := make([]*ast.RuleEntry, 0) - for _, v := range knowledge.RuleEntries { + for _, ruleEntry := range knowledge.RuleEntries { if ctx.Err() != nil { log.Error("Context canceled") return ctx.Err() } - if !v.Retracted && !v.Deleted { + if !ruleEntry.Retracted && !ruleEntry.Deleted { // test if this rule entry v can execute. - can, err := v.Evaluate(ctx, dataCtx, knowledge.WorkingMemory) + can, err := ruleEntry.Evaluate(ctx, dataCtx, knowledge.WorkingMemory) if err != nil { - log.Errorf("Failed testing condition for rule : %s. Got error %v", v.RuleName, err) + log.Errorf("Failed testing condition for rule : %s. Got error %v", ruleEntry.RuleName, err) if g.ReturnErrOnFailedRuleEvaluation { return err @@ -179,10 +179,10 @@ func (g *GruleEngine) ExecuteWithContext(ctx context.Context, dataCtx ast.IDataC } // if can, add into runnable array if can { - runnable = append(runnable, v) + runnable = append(runnable, ruleEntry) } // notify all listeners that a rule's when scope is been evaluated. - g.notifyEvaluateRuleEntry(cycle+1, v, can) + g.notifyEvaluateRuleEntry(cycle+1, ruleEntry, can) } } @@ -261,19 +261,19 @@ func (g *GruleEngine) FetchMatchingRules(dataCtx ast.IDataContext, knowledge *as // Select all rule entry that can be executed. log.Tracef("Select all rule entry that can be executed.") runnable := make([]*ast.RuleEntry, 0) - for _, v := range knowledge.RuleEntries { - if !v.Deleted { + for _, entries := range knowledge.RuleEntries { + if !entries.Deleted { // test if this rule entry v can execute. - can, err := v.Evaluate(context.Background(), dataCtx, knowledge.WorkingMemory) + can, err := entries.Evaluate(context.Background(), dataCtx, knowledge.WorkingMemory) if err != nil { - log.Errorf("Failed testing condition for rule : %s. Got error %v", v.RuleName, err) + log.Errorf("Failed testing condition for rule : %s. Got error %v", entries.RuleName, err) if g.ReturnErrOnFailedRuleEvaluation { return nil, err } } // if can, add into runnable array if can { - runnable = append(runnable, v) + runnable = append(runnable, entries) } } } diff --git a/model/DataAccessLayer.go b/model/DataAccessLayer.go index 6a685217..4b9cc256 100755 --- a/model/DataAccessLayer.go +++ b/model/DataAccessLayer.go @@ -259,13 +259,13 @@ func StrMatchRegexPattern(str string, arg []reflect.Value) (reflect.Value, error return reflect.ValueOf(nil), fmt.Errorf("function StrMatchRegexPattern requires 1 string argument") } - m, err := regexp.MatchString(arg[0].String(), str) + match, err := regexp.MatchString(arg[0].String(), str) if err != nil { return reflect.ValueOf(nil), fmt.Errorf("function StrMatchRegexPattern requires valid regex pattern") } - return reflect.ValueOf(m), nil + return reflect.ValueOf(match), nil } // ArrMapLen will return the size of underlying map, array or slice diff --git a/model/JsonDataAccessLayer.go b/model/JsonDataAccessLayer.go index f941f43d..59ca8ba0 100755 --- a/model/JsonDataAccessLayer.go +++ b/model/JsonDataAccessLayer.go @@ -29,8 +29,8 @@ var ( // NewJSONValueNode will create a new ValueNode structure backend using data structure as provided by JSON parser. func NewJSONValueNode(JSONString, identifiedAs string) (ValueNode, error) { - var d interface{} - err := json.Unmarshal([]byte(JSONString), &d) + var object interface{} + err := json.Unmarshal([]byte(JSONString), &object) if err != nil { return nil, err @@ -39,7 +39,7 @@ func NewJSONValueNode(JSONString, identifiedAs string) (ValueNode, error) { return &JSONValueNode{ parent: nil, identifiedAs: identifiedAs, - data: reflect.ValueOf(d), + data: reflect.ValueOf(object), }, nil } diff --git a/pkg/JsonResource.go b/pkg/JsonResource.go index f268a0ad..2e9f6389 100644 --- a/pkg/JsonResource.go +++ b/pkg/JsonResource.go @@ -63,12 +63,12 @@ func (jr *JSONResource) Load() ([]byte, error) { } firstRune := string(bytes.TrimSpace(data)[0]) - var rs string + var ruleSet string if firstRune == "[" { - rs, err = ParseJSONRuleset(data) + ruleSet, err = ParseJSONRuleset(data) } else if firstRune == "{" { - rs, err = ParseJSONRule(data) + ruleSet, err = ParseJSONRule(data) } else { err = errors.New("invalid JSON input") } @@ -77,7 +77,7 @@ func (jr *JSONResource) Load() ([]byte, error) { return nil, err } - return []byte(rs), nil + return []byte(ruleSet), nil } // String will state the resource source. @@ -189,38 +189,38 @@ func parseRule(rule *GruleJSON) string { panic("rule thenn condition cannot be nil") } - var sb strings.Builder - sb.WriteString("rule ") - sb.WriteString(rule.Name) - sb.WriteString(" ") - sb.WriteString(strconv.Quote(rule.Description)) - sb.WriteString(" salience ") - sb.WriteString(strconv.Itoa(rule.Salience)) - sb.WriteString(" {\n when\n ") - sb.WriteString(parseWhen(rule.When)) - sb.WriteString("\n then\n") + var stringBuilder strings.Builder + stringBuilder.WriteString("rule ") + stringBuilder.WriteString(rule.Name) + stringBuilder.WriteString(" ") + stringBuilder.WriteString(strconv.Quote(rule.Description)) + stringBuilder.WriteString(" salience ") + stringBuilder.WriteString(strconv.Itoa(rule.Salience)) + stringBuilder.WriteString(" {\n when\n ") + stringBuilder.WriteString(parseWhen(rule.When)) + stringBuilder.WriteString("\n then\n") thens := parseThen(rule.Then) for i := 0; i < len(thens); i++ { - sb.WriteString(" ") - sb.WriteString(thens[i]) - sb.WriteString("\n") + stringBuilder.WriteString(" ") + stringBuilder.WriteString(thens[i]) + stringBuilder.WriteString("\n") } - sb.WriteString("}\n") + stringBuilder.WriteString("}\n") - return sb.String() + return stringBuilder.String() } func parseThen(ts []interface{}) []string { thens := make([]string, len(ts)) - for i := 0; i < len(ts); i++ { - switch x := ts[i].(type) { + for thenItem := 0; thenItem < len(ts); thenItem++ { + switch thenType := ts[thenItem].(type) { case string: - thens[i] = x - if !strings.HasSuffix(thens[i], ";") { - thens[i] += ";" + thens[thenItem] = thenType + if !strings.HasSuffix(thens[thenItem], ";") { + thens[thenItem] += ";" } case map[string]interface{}: - thens[i] = buildExpression(x, 0) + ";" + thens[thenItem] = buildExpression(thenType, 0) + ";" default: panic("invalid then type, must be a string or an array of action objects") @@ -231,13 +231,13 @@ func parseThen(ts []interface{}) []string { } func parseWhen(w interface{}) string { - switch x := w.(type) { + switch whenType := w.(type) { case string: - return x + return whenType case map[string]interface{}: - return buildExpression(x, 0) + return buildExpression(whenType, 0) default: panic("invalid when type, must be a string or an array of condition objects") @@ -259,76 +259,76 @@ func buildExpressionEx(input map[string]interface{}, depth int) (string, bool) { panic("expression objects can only contain a single operation type") } - for k, v := range input { - switch k { + for key, value := range input { + switch key { case "and": - return buildCompoundOperator(v, depth, " && ") + return buildCompoundOperator(value, depth, " && ") case "or": - return buildCompoundOperator(v, depth, " || ") + return buildCompoundOperator(value, depth, " || ") case "eq": - return joinOperator(v, " == "), false + return joinOperator(value, " == "), false case "not": - return joinOperator(v, " != "), false + return joinOperator(value, " != "), false case "gt": - return joinOperator(v, " > "), false + return joinOperator(value, " > "), false case "gte": - return joinOperator(v, " >= "), false + return joinOperator(value, " >= "), false case "lt": - return joinOperator(v, " < "), false + return joinOperator(value, " < "), false case "lte": - return joinOperator(v, " <= "), false + return joinOperator(value, " <= "), false case "bor": - return joinOperator(v, " | "), false + return joinOperator(value, " | "), false case "band": - return joinOperator(v, " & "), false + return joinOperator(value, " & "), false case "plus": - return joinOperator(v, " + "), false + return joinOperator(value, " + "), false case "minus": - return joinOperator(v, " - "), false + return joinOperator(value, " - "), false case "div": - return joinOperator(v, " / "), false + return joinOperator(value, " / "), false case "mul": - return joinOperator(v, " * "), false + return joinOperator(value, " * "), false case "mod": - return joinOperator(v, " % "), false + return joinOperator(value, " % "), false case "set": - return joinSet(v, " = "), true + return joinSet(value, " = "), true case "call": - return joinCall(v), true + return joinCall(value), true case "obj": - if s, ok := v.(string); ok { + if s, ok := value.(string); ok { return s, true } panic("object must be a string") case "const": - switch x := v.(type) { + switch valueType := value.(type) { case string: - return strconv.Quote(x), true + return strconv.Quote(valueType), true case float64: - return strconv.FormatFloat(x, 'f', -1, 64), true + return strconv.FormatFloat(valueType, 'f', -1, 64), true case bool: - if x { + if valueType { return "true", true } @@ -339,14 +339,14 @@ func buildExpressionEx(input map[string]interface{}, depth int) (string, bool) { panic("constant must be a string or a numeric value") default: - panic("unknown operator type: " + k) + panic("unknown operator type: " + key) } } panic("boolean expression cannot be empty") } -func buildCompoundOperator(o interface{}, depth int, op string) (string, bool) { +func buildCompoundOperator(o interface{}, depth int, operator string) (string, bool) { if andarr, ok := o.([]interface{}); ok { var ands []string if len(andarr) < 2 { @@ -363,10 +363,10 @@ func buildCompoundOperator(o interface{}, depth int, op string) (string, bool) { } if depth > 0 { - return "(" + strings.Join(ands, op) + ")", false + return "(" + strings.Join(ands, operator) + ")", false } - return strings.Join(ands, op), false + return strings.Join(ands, operator), false } panic("compound operator must be an array") @@ -378,9 +378,9 @@ func joinCall(v interface{}) string { panic("call operator must have at least one operand") } - var f string + var firstCallOperand string var ok bool - if f, ok = arr[0].(string); !ok { + if firstCallOperand, ok = arr[0].(string); !ok { panic("first call operand must be a string") } @@ -390,28 +390,28 @@ func joinCall(v interface{}) string { sars[i-1] = parseCallOperand(arr[i]) } - return f + "(" + strings.Join(sars, ", ") + ")" + return firstCallOperand + "(" + strings.Join(sars, ", ") + ")" } - return f + "()" + return firstCallOperand + "()" } panic("operator has an unexpected type") } func parseCallOperand(o interface{}) string { - switch x := o.(type) { + switch operandType := o.(type) { case string: - if len(x) == 0 { + if len(operandType) == 0 { panic("operand cannnot be empty") } - return x + return operandType case float64: - return fmt.Sprint(x) + return fmt.Sprint(operandType) case bool: - if x { + if operandType { return "true" } @@ -419,14 +419,14 @@ func parseCallOperand(o interface{}) string { return "false" case map[string]interface{}: - return buildExpression(x, 0) + return buildExpression(operandType, 0) default: panic("operand has an invalid type") } } -func joinOperator(v interface{}, op string) string { +func joinOperator(v interface{}, operator string) string { if arr, ok := v.([]interface{}); ok { if len(arr) == 0 { @@ -437,43 +437,43 @@ func joinOperator(v interface{}, op string) string { ops[i] = parseOperand(arr[i], false) } - return strings.Join(ops, op) + return strings.Join(ops, operator) } panic("operator has an unexpected type") } -func joinSet(v interface{}, op string) string { +func joinSet(v interface{}, operator string) string { if arr, ok := v.([]interface{}); ok { if len(arr) != 2 { panic("set operand count must be 2") } - return parseOperand(arr[0], true) + op + parseOperand(arr[1], true) + return parseOperand(arr[0], true) + operator + parseOperand(arr[1], true) } panic("operator has an unexpected type") } func parseOperand(o interface{}, noWrap bool) string { - switch x := o.(type) { + switch operandType := o.(type) { case string: - return x + return operandType case float64: - return fmt.Sprint(x) + return fmt.Sprint(operandType) case bool: - if x { + if operandType { return "true" } return "false" case map[string]interface{}: - expr, expNoWrap := buildExpressionEx(x, 0) + expr, expNoWrap := buildExpressionEx(operandType, 0) if expNoWrap || noWrap { return expr diff --git a/pkg/gitresource.go b/pkg/gitresource.go index d3f2f74f..25fc7da1 100644 --- a/pkg/gitresource.go +++ b/pkg/gitresource.go @@ -15,7 +15,7 @@ import ( // Load will load the file from your git repository func (bundle *GITResourceBundle) Load() ([]Resource, error) { - fs := memfs.New() + fileSystem := memfs.New() CloneOpts := &git.CloneOptions{} if len(bundle.URL) == 0 { @@ -46,11 +46,11 @@ func (bundle *GITResourceBundle) Load() ([]Resource, error) { } } - _, err := git.Clone(memory.NewStorage(), fs, CloneOpts) + _, err := git.Clone(memory.NewStorage(), fileSystem, CloneOpts) if err != nil { return nil, err } - return bundle.loadPath(bundle.URL, "/", fs) + return bundle.loadPath(bundle.URL, "/", fileSystem) } diff --git a/pkg/jsontool/JsonDom.go b/pkg/jsontool/JsonDom.go index b9664851..1aecf7dd 100644 --- a/pkg/jsontool/JsonDom.go +++ b/pkg/jsontool/JsonDom.go @@ -24,14 +24,14 @@ import ( // NewJSONData will create a new instance of JSONData func NewJSONData(jsonData []byte) (*JSONData, error) { - var tm interface{} + var instance interface{} - err := json.Unmarshal(jsonData, &tm) + err := json.Unmarshal(jsonData, &instance) if err != nil { return nil, err } - return &JSONData{jsonRoot: tm}, nil + return &JSONData{jsonRoot: instance}, nil } // JSONNode represent a node in JSON Tree @@ -252,27 +252,27 @@ func (jo *JSONData) validPathCheck(pathArr []string, node *JSONNode) bool { return true } - p := pathArr[0] - if len(p) == 0 { + path := pathArr[0] + if len(path) == 0 { return false } - if p[:1] == "[" && p[len(p)-1:] == "]" { + if path[:1] == "[" && path[len(path)-1:] == "]" { if node.IsArray() { - pn := p[1 : len(p)-1] + pn := path[1 : len(path)-1] if len(pn) == 0 { return false } - n, err := strconv.Atoi(pn) + theInt, err := strconv.Atoi(pn) if err != nil { return false } - if n < 0 || n >= node.Len() { + if theInt < 0 || theInt >= node.Len() { return false } - nNode := node.GetNodeAt(n) + nNode := node.GetNodeAt(theInt) nPathArr := pathArr[1:] return jo.validPathCheck(nPathArr, nNode) @@ -281,20 +281,20 @@ func (jo *JSONData) validPathCheck(pathArr []string, node *JSONNode) bool { return false } if node.IsMap() { - if strings.Contains(p, "[") { - k := p[:strings.Index(p, "[")] + if strings.Contains(path, "[") { + k := path[:strings.Index(path, "[")] if !node.HaveKey(k) { return false } nNode := node.Get(k) - nPathArr := []string{p[strings.Index(p, "["):]} + nPathArr := []string{path[strings.Index(path, "["):]} nPathArr = append(nPathArr, pathArr[1:]...) return jo.validPathCheck(nPathArr, nNode) } - if node.HaveKey(p) { - nNode := node.Get(p) + if node.HaveKey(path) { + nNode := node.Get(path) nPathArr := pathArr[1:] return jo.validPathCheck(nPathArr, nNode) @@ -323,28 +323,28 @@ func (jo *JSONData) getByPath(pathArr []string, node *JSONNode) *JSONNode { return node } - p := pathArr[0] - if len(p) == 0 { + path := pathArr[0] + if len(path) == 0 { panic("Not a valid path") } - if p[:1] == "[" && p[len(p)-1:] == "]" { + if path[:1] == "[" && path[len(path)-1:] == "]" { if node.IsArray() { - pn := p[1 : len(p)-1] + pn := path[1 : len(path)-1] if len(pn) == 0 { panic("Not a valid path - array do not contain offset number") } - n, err := strconv.Atoi(pn) + theInt, err := strconv.Atoi(pn) if err != nil { panic("Not a valid path - array offset not number") } - if n < 0 || n >= node.Len() { + if theInt < 0 || theInt >= node.Len() { panic("Not a valid path - array offset < 0 or >= length") } - nNode := node.GetNodeAt(n) + nNode := node.GetNodeAt(theInt) nPathArr := pathArr[1:] return jo.getByPath(nPathArr, nNode) @@ -353,20 +353,20 @@ func (jo *JSONData) getByPath(pathArr []string, node *JSONNode) *JSONNode { panic("Not a valid path - not an array") } if node.IsMap() { - if strings.Contains(p, "[") { - k := p[:strings.Index(p, "[")] + if strings.Contains(path, "[") { + k := path[:strings.Index(path, "[")] if !node.HaveKey(k) { panic("Not a valid path - key not exist") } nNode := node.Get(k) - nPathArr := []string{p[strings.Index(p, "["):]} + nPathArr := []string{path[strings.Index(path, "["):]} nPathArr = append(nPathArr, pathArr[1:]...) return jo.getByPath(nPathArr, nNode) } - if node.HaveKey(p) { - nNode := node.Get(p) + if node.HaveKey(path) { + nNode := node.Get(path) nPathArr := pathArr[1:] return jo.getByPath(nPathArr, nNode) diff --git a/pkg/reflectmath.go b/pkg/reflectmath.go index b93fbb1e..e4e03c23 100644 --- a/pkg/reflectmath.go +++ b/pkg/reflectmath.go @@ -25,58 +25,58 @@ func EvaluateMultiplication(left, right reflect.Value) (reflect.Value, error) { left, right = GetValueElem(left), GetValueElem(right) switch left.Kind() { case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: - lv := left.Int() + leftValue := left.Int() switch right.Kind() { case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: - rv := right.Int() + rightValue := right.Int() - return reflect.ValueOf(lv * rv), nil + return reflect.ValueOf(leftValue * rightValue), nil case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: - rv := right.Uint() + rightValue := right.Uint() - return reflect.ValueOf(lv * int64(rv)), nil + return reflect.ValueOf(leftValue * int64(rightValue)), nil case reflect.Float32, reflect.Float64: - rv := right.Float() + rightValue := right.Float() - return reflect.ValueOf(float64(lv) * rv), nil + return reflect.ValueOf(float64(leftValue) * rightValue), nil default: return reflect.ValueOf(nil), fmt.Errorf("can not multiply data type of %s", right.Kind().String()) } case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: - lv := left.Uint() + leftValue := left.Uint() switch right.Kind() { case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: - rv := right.Int() + rightValue := right.Int() - return reflect.ValueOf(int64(lv) * rv), nil + return reflect.ValueOf(int64(leftValue) * rightValue), nil case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: - rv := right.Uint() + rightValue := right.Uint() - return reflect.ValueOf(lv * rv), nil + return reflect.ValueOf(leftValue * rightValue), nil case reflect.Float32, reflect.Float64: - rv := right.Float() + rightValue := right.Float() - return reflect.ValueOf(float64(lv) * rv), nil + return reflect.ValueOf(float64(leftValue) * rightValue), nil default: return reflect.ValueOf(nil), fmt.Errorf("can not multiply data type of %s", right.Kind().String()) } case reflect.Float32, reflect.Float64: - lv := left.Float() + leftValue := left.Float() switch right.Kind() { case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: - rv := right.Int() + rightValue := right.Int() - return reflect.ValueOf(lv * float64(rv)), nil + return reflect.ValueOf(leftValue * float64(rightValue)), nil case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: - rv := right.Uint() + rightValue := right.Uint() - return reflect.ValueOf(lv * float64(rv)), nil + return reflect.ValueOf(leftValue * float64(rightValue)), nil case reflect.Float32, reflect.Float64: - rv := right.Float() + rightValue := right.Float() - return reflect.ValueOf(lv * rv), nil + return reflect.ValueOf(leftValue * rightValue), nil default: return reflect.ValueOf(nil), fmt.Errorf("can not multiply data type of %s", right.Kind().String()) @@ -92,58 +92,58 @@ func EvaluateDivision(left, right reflect.Value) (reflect.Value, error) { left, right = GetValueElem(left), GetValueElem(right) switch left.Kind() { case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: - lv := left.Int() + leftValue := left.Int() switch right.Kind() { case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: - rv := right.Int() + rightValue := right.Int() - return reflect.ValueOf(float64(lv) / float64(rv)), nil + return reflect.ValueOf(float64(leftValue) / float64(rightValue)), nil case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: - rv := right.Uint() + rightValue := right.Uint() - return reflect.ValueOf(float64(lv) / float64(rv)), nil + return reflect.ValueOf(float64(leftValue) / float64(rightValue)), nil case reflect.Float32, reflect.Float64: - rv := right.Float() + rightValue := right.Float() - return reflect.ValueOf(float64(lv) / rv), nil + return reflect.ValueOf(float64(leftValue) / rightValue), nil default: return reflect.ValueOf(nil), fmt.Errorf("can not use data type of %s in division", right.Kind().String()) } case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: - lv := left.Uint() + leftValue := left.Uint() switch right.Kind() { case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: - rv := right.Int() + rightValue := right.Int() - return reflect.ValueOf(float64(lv) / float64(rv)), nil + return reflect.ValueOf(float64(leftValue) / float64(rightValue)), nil case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: - rv := right.Uint() + rightValue := right.Uint() - return reflect.ValueOf(float64(lv) / float64(rv)), nil + return reflect.ValueOf(float64(leftValue) / float64(rightValue)), nil case reflect.Float32, reflect.Float64: - rv := right.Float() + rightValue := right.Float() - return reflect.ValueOf(float64(lv) / rv), nil + return reflect.ValueOf(float64(leftValue) / rightValue), nil default: return reflect.ValueOf(nil), fmt.Errorf("can not use data type of %s in division", right.Kind().String()) } case reflect.Float32, reflect.Float64: - lv := left.Float() + leftValue := left.Float() switch right.Kind() { case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: - rv := right.Int() + rightValue := right.Int() - return reflect.ValueOf(lv / float64(rv)), nil + return reflect.ValueOf(leftValue / float64(rightValue)), nil case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: - rv := right.Uint() + rightValue := right.Uint() - return reflect.ValueOf(lv / float64(rv)), nil + return reflect.ValueOf(leftValue / float64(rightValue)), nil case reflect.Float32, reflect.Float64: - rv := right.Float() + rightValue := right.Float() - return reflect.ValueOf(lv / rv), nil + return reflect.ValueOf(leftValue / rightValue), nil default: return reflect.ValueOf(nil), fmt.Errorf("can not use data type of %s in division", right.Kind().String()) @@ -159,31 +159,31 @@ func EvaluateModulo(left, right reflect.Value) (reflect.Value, error) { left, right = GetValueElem(left), GetValueElem(right) switch left.Kind() { case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: - lv := left.Int() + leftValue := left.Int() switch right.Kind() { case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: - rv := right.Int() + rightValue := right.Int() - return reflect.ValueOf(lv % rv), nil + return reflect.ValueOf(leftValue % rightValue), nil case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: - rv := right.Uint() + rightValue := right.Uint() - return reflect.ValueOf(lv % int64(rv)), nil + return reflect.ValueOf(leftValue % int64(rightValue)), nil default: return reflect.ValueOf(nil), fmt.Errorf("can not use data type of %s in modulo", right.Kind().String()) } case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: - lv := left.Uint() + leftValue := left.Uint() switch right.Kind() { case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: - rv := right.Int() + rightValue := right.Int() - return reflect.ValueOf(int64(lv) % rv), nil + return reflect.ValueOf(int64(leftValue) % rightValue), nil case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: - rv := right.Uint() + rightValue := right.Uint() - return reflect.ValueOf(int64(lv) % int64(rv)), nil + return reflect.ValueOf(int64(leftValue) % int64(rightValue)), nil default: return reflect.ValueOf(nil), fmt.Errorf("can not use data type of %s in modulo", right.Kind().String()) @@ -199,102 +199,102 @@ func EvaluateAddition(left, right reflect.Value) (reflect.Value, error) { left, right = GetValueElem(left), GetValueElem(right) switch left.Kind() { case reflect.String: - lv := left.String() + leftValue := left.String() switch right.Kind() { case reflect.String: - rv := right.String() + rightValue := right.String() - return reflect.ValueOf(fmt.Sprintf("%s%s", lv, rv)), nil + return reflect.ValueOf(fmt.Sprintf("%s%s", leftValue, rightValue)), nil case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: - rv := right.Int() + rightValue := right.Int() - return reflect.ValueOf(fmt.Sprintf("%s%d", lv, rv)), nil + return reflect.ValueOf(fmt.Sprintf("%s%d", leftValue, rightValue)), nil case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: - rv := right.Uint() + rightValue := right.Uint() - return reflect.ValueOf(fmt.Sprintf("%s%d", lv, rv)), nil + return reflect.ValueOf(fmt.Sprintf("%s%d", leftValue, rightValue)), nil case reflect.Float32, reflect.Float64: - rv := right.Float() + rightValue := right.Float() - return reflect.ValueOf(fmt.Sprintf("%s%f", lv, rv)), nil + return reflect.ValueOf(fmt.Sprintf("%s%f", leftValue, rightValue)), nil case reflect.Bool: - rv := right.Bool() + rightValue := right.Bool() - return reflect.ValueOf(fmt.Sprintf("%s%v", lv, rv)), nil + return reflect.ValueOf(fmt.Sprintf("%s%v", leftValue, rightValue)), nil default: if right.Type().String() == "time.Time" { - rv := right.Interface().(time.Time) + rightValue := right.Interface().(time.Time) - return reflect.ValueOf(fmt.Sprintf("%s%s", lv, rv.Format(time.RFC3339))), nil + return reflect.ValueOf(fmt.Sprintf("%s%s", leftValue, rightValue.Format(time.RFC3339))), nil } return reflect.ValueOf(nil), fmt.Errorf("can not use data type of %s in addition", right.Kind().String()) } case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: - lv := left.Int() + leftValue := left.Int() switch right.Kind() { case reflect.String: - rv := right.String() + rightValue := right.String() - return reflect.ValueOf(fmt.Sprintf("%d%s", lv, rv)), nil + return reflect.ValueOf(fmt.Sprintf("%d%s", leftValue, rightValue)), nil case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: - rv := right.Int() + rightValue := right.Int() - return reflect.ValueOf(lv + rv), nil + return reflect.ValueOf(leftValue + rightValue), nil case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: - rv := right.Uint() + rightValue := right.Uint() - return reflect.ValueOf(lv + int64(rv)), nil + return reflect.ValueOf(leftValue + int64(rightValue)), nil case reflect.Float32, reflect.Float64: - rv := right.Float() + rightValue := right.Float() - return reflect.ValueOf(float64(lv) + rv), nil + return reflect.ValueOf(float64(leftValue) + rightValue), nil default: return reflect.ValueOf(nil), fmt.Errorf("can not use data type of %s in addition", right.Kind().String()) } case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: - lv := left.Uint() + leftValue := left.Uint() switch right.Kind() { case reflect.String: - rv := right.String() + rightValue := right.String() - return reflect.ValueOf(fmt.Sprintf("%d%s", lv, rv)), nil + return reflect.ValueOf(fmt.Sprintf("%d%s", leftValue, rightValue)), nil case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: - rv := right.Int() + rightValue := right.Int() - return reflect.ValueOf(int64(lv) + rv), nil + return reflect.ValueOf(int64(leftValue) + rightValue), nil case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: - rv := right.Uint() + rightValue := right.Uint() - return reflect.ValueOf(lv + rv), nil + return reflect.ValueOf(leftValue + rightValue), nil case reflect.Float32, reflect.Float64: - rv := right.Float() + rightValue := right.Float() - return reflect.ValueOf(float64(lv) + rv), nil + return reflect.ValueOf(float64(leftValue) + rightValue), nil default: return reflect.ValueOf(nil), fmt.Errorf("can not use data type of %s in division", right.Kind().String()) } case reflect.Float32, reflect.Float64: - lv := left.Float() + leftValue := left.Float() switch right.Kind() { case reflect.String: - rv := right.String() + rightValue := right.String() - return reflect.ValueOf(fmt.Sprintf("%f%s", lv, rv)), nil + return reflect.ValueOf(fmt.Sprintf("%f%s", leftValue, rightValue)), nil case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: - rv := right.Int() + rightValue := right.Int() - return reflect.ValueOf(lv + float64(rv)), nil + return reflect.ValueOf(leftValue + float64(rightValue)), nil case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: - rv := right.Uint() + rightValue := right.Uint() - return reflect.ValueOf(lv + float64(rv)), nil + return reflect.ValueOf(leftValue + float64(rightValue)), nil case reflect.Float32, reflect.Float64: - rv := right.Float() + rightValue := right.Float() - return reflect.ValueOf(lv + rv), nil + return reflect.ValueOf(leftValue + rightValue), nil default: return reflect.ValueOf(nil), fmt.Errorf("can not use data type of %s in division", right.Kind().String()) @@ -310,58 +310,58 @@ func EvaluateSubtraction(left, right reflect.Value) (reflect.Value, error) { left, right = GetValueElem(left), GetValueElem(right) switch left.Kind() { case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: - lv := left.Int() + leftValue := left.Int() switch right.Kind() { case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: - rv := right.Int() + rightValue := right.Int() - return reflect.ValueOf(lv - rv), nil + return reflect.ValueOf(leftValue - rightValue), nil case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: - rv := right.Uint() + rightValue := right.Uint() - return reflect.ValueOf(lv - int64(rv)), nil + return reflect.ValueOf(leftValue - int64(rightValue)), nil case reflect.Float32, reflect.Float64: - rv := right.Float() + rightValue := right.Float() - return reflect.ValueOf(float64(lv) - rv), nil + return reflect.ValueOf(float64(leftValue) - rightValue), nil default: return reflect.ValueOf(nil), fmt.Errorf("can not use data type of %s in subtraction", right.Kind().String()) } case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: - lv := left.Uint() + leftValue := left.Uint() switch right.Kind() { case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: - rv := right.Int() + rightValue := right.Int() - return reflect.ValueOf(int64(lv) - rv), nil + return reflect.ValueOf(int64(leftValue) - rightValue), nil case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: - rv := right.Uint() + rightValue := right.Uint() - return reflect.ValueOf(lv - rv), nil + return reflect.ValueOf(leftValue - rightValue), nil case reflect.Float32, reflect.Float64: - rv := right.Float() + rightValue := right.Float() - return reflect.ValueOf(float64(lv) - rv), nil + return reflect.ValueOf(float64(leftValue) - rightValue), nil default: return reflect.ValueOf(nil), fmt.Errorf("can not use data type of %s in subtraction", right.Kind().String()) } case reflect.Float32, reflect.Float64: - lv := left.Float() + leftValue := left.Float() switch right.Kind() { case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: - rv := right.Int() + rightValue := right.Int() - return reflect.ValueOf(lv - float64(rv)), nil + return reflect.ValueOf(leftValue - float64(rightValue)), nil case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: - rv := right.Uint() + rightValue := right.Uint() - return reflect.ValueOf(lv - float64(rv)), nil + return reflect.ValueOf(leftValue - float64(rightValue)), nil case reflect.Float32, reflect.Float64: - rv := right.Float() + rightValue := right.Float() - return reflect.ValueOf(lv - rv), nil + return reflect.ValueOf(leftValue - rightValue), nil default: return reflect.ValueOf(nil), fmt.Errorf("can not use data type of %s in subtraction", right.Kind().String()) @@ -377,31 +377,31 @@ func EvaluateBitAnd(left, right reflect.Value) (reflect.Value, error) { left, right = GetValueElem(left), GetValueElem(right) switch left.Kind() { case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: - lv := left.Int() + leftValue := left.Int() switch right.Kind() { case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: - rv := right.Int() + rightValue := right.Int() - return reflect.ValueOf(lv & rv), nil + return reflect.ValueOf(leftValue & rightValue), nil case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: - rv := right.Uint() + rightValue := right.Uint() - return reflect.ValueOf(lv & int64(rv)), nil + return reflect.ValueOf(leftValue & int64(rightValue)), nil default: return reflect.ValueOf(nil), fmt.Errorf("can not use data type of %s in bitwise AND operation", right.Kind().String()) } case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: - lv := left.Uint() + leftValue := left.Uint() switch right.Kind() { case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: - rv := right.Int() + rightValue := right.Int() - return reflect.ValueOf(int64(lv) & rv), nil + return reflect.ValueOf(int64(leftValue) & rightValue), nil case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: - rv := right.Uint() + rightValue := right.Uint() - return reflect.ValueOf(lv & rv), nil + return reflect.ValueOf(leftValue & rightValue), nil default: return reflect.ValueOf(nil), fmt.Errorf("can not use data type of %s in bitwise AND operation", right.Kind().String()) @@ -417,31 +417,31 @@ func EvaluateBitOr(left, right reflect.Value) (reflect.Value, error) { left, right = GetValueElem(left), GetValueElem(right) switch left.Kind() { case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: - lv := left.Int() + leftValue := left.Int() switch right.Kind() { case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: - rv := right.Int() + rightValue := right.Int() - return reflect.ValueOf(lv | rv), nil + return reflect.ValueOf(leftValue | rightValue), nil case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: - rv := right.Uint() + rightValue := right.Uint() - return reflect.ValueOf(lv | int64(rv)), nil + return reflect.ValueOf(leftValue | int64(rightValue)), nil default: return reflect.ValueOf(nil), fmt.Errorf("can not use data type of %s in bitwise OR operation", right.Kind().String()) } case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: - lv := left.Uint() + leftValue := left.Uint() switch right.Kind() { case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: - rv := right.Int() + rightValue := right.Int() - return reflect.ValueOf(int64(lv) | rv), nil + return reflect.ValueOf(int64(leftValue) | rightValue), nil case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: - rv := right.Uint() + rightValue := right.Uint() - return reflect.ValueOf(lv | rv), nil + return reflect.ValueOf(leftValue | rightValue), nil default: return reflect.ValueOf(nil), fmt.Errorf("can not use data type of %s in bitwise OR operation", right.Kind().String()) @@ -457,79 +457,79 @@ func EvaluateGreaterThan(left, right reflect.Value) (reflect.Value, error) { left, right = GetValueElem(left), GetValueElem(right) switch left.Kind() { case reflect.String: - lv := left.String() + leftValue := left.String() switch right.Kind() { case reflect.String: - rv := right.String() + rightValue := right.String() - return reflect.ValueOf(lv > rv), nil + return reflect.ValueOf(leftValue > rightValue), nil default: return reflect.ValueOf(nil), fmt.Errorf("can not compare data type of string to %s in GT comparison", right.Kind().String()) } case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: - lv := left.Int() + leftValue := left.Int() switch right.Kind() { case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: - rv := right.Int() + rightValue := right.Int() - return reflect.ValueOf(lv > rv), nil + return reflect.ValueOf(leftValue > rightValue), nil case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: - rv := right.Uint() + rightValue := right.Uint() - return reflect.ValueOf(lv > int64(rv)), nil + return reflect.ValueOf(leftValue > int64(rightValue)), nil case reflect.Float32, reflect.Float64: - rv := right.Float() + rightValue := right.Float() - return reflect.ValueOf(float64(lv) > rv), nil + return reflect.ValueOf(float64(leftValue) > rightValue), nil default: return reflect.ValueOf(nil), fmt.Errorf("can not use data type of %s in GT comparison", right.Kind().String()) } case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: - lv := left.Uint() + leftValue := left.Uint() switch right.Kind() { case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: - rv := right.Int() + rightValue := right.Int() - return reflect.ValueOf(int64(lv) > rv), nil + return reflect.ValueOf(int64(leftValue) > rightValue), nil case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: - rv := right.Uint() + rightValue := right.Uint() - return reflect.ValueOf(lv > rv), nil + return reflect.ValueOf(leftValue > rightValue), nil case reflect.Float32, reflect.Float64: - rv := right.Float() + rightValue := right.Float() - return reflect.ValueOf(float64(lv) > rv), nil + return reflect.ValueOf(float64(leftValue) > rightValue), nil default: return reflect.ValueOf(nil), fmt.Errorf("can not use data type of %s in GT comparison", right.Kind().String()) } case reflect.Float32, reflect.Float64: - lv := left.Float() + leftValue := left.Float() switch right.Kind() { case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: - rv := right.Int() + rightValue := right.Int() - return reflect.ValueOf(lv > float64(rv)), nil + return reflect.ValueOf(leftValue > float64(rightValue)), nil case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: - rv := right.Uint() + rightValue := right.Uint() - return reflect.ValueOf(lv > float64(rv)), nil + return reflect.ValueOf(leftValue > float64(rightValue)), nil case reflect.Float32, reflect.Float64: - rv := right.Float() + rightValue := right.Float() - return reflect.ValueOf(lv > rv), nil + return reflect.ValueOf(leftValue > rightValue), nil default: return reflect.ValueOf(nil), fmt.Errorf("can not use data type of %s in GT comparison", right.Kind().String()) } default: if left.Type().String() == "time.Time" && right.Type().String() == "time.Time" { - lv := left.Interface().(time.Time) - rv := right.Interface().(time.Time) + leftValue := left.Interface().(time.Time) + rightValue := right.Interface().(time.Time) - return reflect.ValueOf(lv.After(rv)), nil + return reflect.ValueOf(leftValue.After(rightValue)), nil } return reflect.ValueOf(nil), fmt.Errorf("can not use data type of %s in GT comparison", left.Kind().String()) @@ -541,79 +541,79 @@ func EvaluateLesserThan(left, right reflect.Value) (reflect.Value, error) { left, right = GetValueElem(left), GetValueElem(right) switch left.Kind() { case reflect.String: - lv := left.String() + leftValue := left.String() switch right.Kind() { case reflect.String: - rv := right.String() + rightValue := right.String() - return reflect.ValueOf(lv < rv), nil + return reflect.ValueOf(leftValue < rightValue), nil default: return reflect.ValueOf(nil), fmt.Errorf("can not compare data type of string to %s in LT comparison", right.Kind().String()) } case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: - lv := left.Int() + leftValue := left.Int() switch right.Kind() { case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: - rv := right.Int() + rightValue := right.Int() - return reflect.ValueOf(lv < rv), nil + return reflect.ValueOf(leftValue < rightValue), nil case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: - rv := right.Uint() + rightValue := right.Uint() - return reflect.ValueOf(lv < int64(rv)), nil + return reflect.ValueOf(leftValue < int64(rightValue)), nil case reflect.Float32, reflect.Float64: - rv := right.Float() + rightValue := right.Float() - return reflect.ValueOf(float64(lv) < rv), nil + return reflect.ValueOf(float64(leftValue) < rightValue), nil default: return reflect.ValueOf(nil), fmt.Errorf("can not use data type of %s in LT comparison", right.Kind().String()) } case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: - lv := left.Uint() + leftValue := left.Uint() switch right.Kind() { case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: - rv := right.Int() + rightValue := right.Int() - return reflect.ValueOf(int64(lv) < rv), nil + return reflect.ValueOf(int64(leftValue) < rightValue), nil case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: - rv := right.Uint() + rightValue := right.Uint() - return reflect.ValueOf(lv < rv), nil + return reflect.ValueOf(leftValue < rightValue), nil case reflect.Float32, reflect.Float64: - rv := right.Float() + rightValue := right.Float() - return reflect.ValueOf(float64(lv) < rv), nil + return reflect.ValueOf(float64(leftValue) < rightValue), nil default: return reflect.ValueOf(nil), fmt.Errorf("can not use data type of %s in LT comparison", right.Kind().String()) } case reflect.Float32, reflect.Float64: - lv := left.Float() + leftValue := left.Float() switch right.Kind() { case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: - rv := right.Int() + rightValue := right.Int() - return reflect.ValueOf(lv < float64(rv)), nil + return reflect.ValueOf(leftValue < float64(rightValue)), nil case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: - rv := right.Uint() + rightValue := right.Uint() - return reflect.ValueOf(lv < float64(rv)), nil + return reflect.ValueOf(leftValue < float64(rightValue)), nil case reflect.Float32, reflect.Float64: - rv := right.Float() + rightValue := right.Float() - return reflect.ValueOf(lv < rv), nil + return reflect.ValueOf(leftValue < rightValue), nil default: return reflect.ValueOf(nil), fmt.Errorf("can not use data type of %s in LT comparison", right.Kind().String()) } default: if left.Type().String() == "time.Time" && right.Type().String() == "time.Time" { - lv := left.Interface().(time.Time) - rv := right.Interface().(time.Time) + leftValue := left.Interface().(time.Time) + rightValue := right.Interface().(time.Time) - return reflect.ValueOf(lv.Before(rv)), nil + return reflect.ValueOf(leftValue.Before(rightValue)), nil } return reflect.ValueOf(nil), fmt.Errorf("can not use data type of %s in LT comparison", left.Kind().String()) @@ -625,79 +625,79 @@ func EvaluateGreaterThanEqual(left, right reflect.Value) (reflect.Value, error) left, right = GetValueElem(left), GetValueElem(right) switch left.Kind() { case reflect.String: - lv := left.String() + leftValue := left.String() switch right.Kind() { case reflect.String: - rv := right.String() + rightValue := right.String() - return reflect.ValueOf(lv >= rv), nil + return reflect.ValueOf(leftValue >= rightValue), nil default: return reflect.ValueOf(nil), fmt.Errorf("can not compare data type of string to %s in GTE comparison", right.Kind().String()) } case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: - lv := left.Int() + leftValue := left.Int() switch right.Kind() { case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: - rv := right.Int() + rightValue := right.Int() - return reflect.ValueOf(lv >= rv), nil + return reflect.ValueOf(leftValue >= rightValue), nil case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: - rv := right.Uint() + rightValue := right.Uint() - return reflect.ValueOf(lv >= int64(rv)), nil + return reflect.ValueOf(leftValue >= int64(rightValue)), nil case reflect.Float32, reflect.Float64: - rv := right.Float() + rightValue := right.Float() - return reflect.ValueOf(float64(lv) >= rv), nil + return reflect.ValueOf(float64(leftValue) >= rightValue), nil default: return reflect.ValueOf(nil), fmt.Errorf("can not use data type of %s in GTE comparison", right.Kind().String()) } case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: - lv := left.Uint() + leftValue := left.Uint() switch right.Kind() { case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: - rv := right.Int() + rightValue := right.Int() - return reflect.ValueOf(int64(lv) >= rv), nil + return reflect.ValueOf(int64(leftValue) >= rightValue), nil case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: - rv := right.Uint() + rightValue := right.Uint() - return reflect.ValueOf(lv >= rv), nil + return reflect.ValueOf(leftValue >= rightValue), nil case reflect.Float32, reflect.Float64: - rv := right.Float() + rightValue := right.Float() - return reflect.ValueOf(float64(lv) >= rv), nil + return reflect.ValueOf(float64(leftValue) >= rightValue), nil default: return reflect.ValueOf(nil), fmt.Errorf("can not use data type of %s in GTE comparison", right.Kind().String()) } case reflect.Float32, reflect.Float64: - lv := left.Float() + leftValue := left.Float() switch right.Kind() { case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: - rv := right.Int() + rightValue := right.Int() - return reflect.ValueOf(lv >= float64(rv)), nil + return reflect.ValueOf(leftValue >= float64(rightValue)), nil case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: - rv := right.Uint() + rightValue := right.Uint() - return reflect.ValueOf(lv >= float64(rv)), nil + return reflect.ValueOf(leftValue >= float64(rightValue)), nil case reflect.Float32, reflect.Float64: - rv := right.Float() + rightValue := right.Float() - return reflect.ValueOf(lv >= rv), nil + return reflect.ValueOf(leftValue >= rightValue), nil default: return reflect.ValueOf(nil), fmt.Errorf("can not use data type of %s in GTE comparison", right.Kind().String()) } default: if left.Type().String() == "time.Time" && right.Type().String() == "time.Time" { - lv := left.Interface().(time.Time) - rv := right.Interface().(time.Time) + leftValue := left.Interface().(time.Time) + rightValue := right.Interface().(time.Time) - return reflect.ValueOf(lv.After(rv) || lv == rv), nil + return reflect.ValueOf(leftValue.After(rightValue) || leftValue == rightValue), nil } return reflect.ValueOf(nil), fmt.Errorf("can not use data type of %s in GTE comparison", left.Kind().String()) @@ -709,79 +709,79 @@ func EvaluateLesserThanEqual(left, right reflect.Value) (reflect.Value, error) { left, right = GetValueElem(left), GetValueElem(right) switch left.Kind() { case reflect.String: - lv := left.String() + leftValue := left.String() switch right.Kind() { case reflect.String: - rv := right.String() + rightValue := right.String() - return reflect.ValueOf(lv <= rv), nil + return reflect.ValueOf(leftValue <= rightValue), nil default: return reflect.ValueOf(nil), fmt.Errorf("can not compare data type of string to %s in LTE comparison", right.Kind().String()) } case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: - lv := left.Int() + leftValue := left.Int() switch right.Kind() { case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: - rv := right.Int() + rightValue := right.Int() - return reflect.ValueOf(lv <= rv), nil + return reflect.ValueOf(leftValue <= rightValue), nil case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: - rv := right.Uint() + rightValue := right.Uint() - return reflect.ValueOf(lv <= int64(rv)), nil + return reflect.ValueOf(leftValue <= int64(rightValue)), nil case reflect.Float32, reflect.Float64: - rv := right.Float() + rightValue := right.Float() - return reflect.ValueOf(float64(lv) <= rv), nil + return reflect.ValueOf(float64(leftValue) <= rightValue), nil default: return reflect.ValueOf(nil), fmt.Errorf("can not use data type of %s in LTE comparison", right.Kind().String()) } case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: - lv := left.Uint() + leftValue := left.Uint() switch right.Kind() { case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: - rv := right.Int() + rightValue := right.Int() - return reflect.ValueOf(int64(lv) <= rv), nil + return reflect.ValueOf(int64(leftValue) <= rightValue), nil case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: - rv := right.Uint() + rightValue := right.Uint() - return reflect.ValueOf(lv <= rv), nil + return reflect.ValueOf(leftValue <= rightValue), nil case reflect.Float32, reflect.Float64: - rv := right.Float() + rightValue := right.Float() - return reflect.ValueOf(float64(lv) <= rv), nil + return reflect.ValueOf(float64(leftValue) <= rightValue), nil default: return reflect.ValueOf(nil), fmt.Errorf("can not use data type of %s in LTE comparison", right.Kind().String()) } case reflect.Float32, reflect.Float64: - lv := left.Float() + leftValue := left.Float() switch right.Kind() { case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: - rv := right.Int() + rightValue := right.Int() - return reflect.ValueOf(lv <= float64(rv)), nil + return reflect.ValueOf(leftValue <= float64(rightValue)), nil case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: - rv := right.Uint() + rightValue := right.Uint() - return reflect.ValueOf(lv <= float64(rv)), nil + return reflect.ValueOf(leftValue <= float64(rightValue)), nil case reflect.Float32, reflect.Float64: - rv := right.Float() + rightValue := right.Float() - return reflect.ValueOf(lv <= rv), nil + return reflect.ValueOf(leftValue <= rightValue), nil default: return reflect.ValueOf(nil), fmt.Errorf("can not use data type of %s in LTE comparison", right.Kind().String()) } default: if left.Type().String() == "time.Time" && right.Type().String() == "time.Time" { - lv := left.Interface().(time.Time) - rv := right.Interface().(time.Time) + leftValue := left.Interface().(time.Time) + rightValue := right.Interface().(time.Time) - return reflect.ValueOf(lv.Before(rv) || lv == rv), nil + return reflect.ValueOf(leftValue.Before(rightValue) || leftValue == rightValue), nil } return reflect.ValueOf(nil), fmt.Errorf("can not use data type of %s in LTE comparison", left.Kind().String()) @@ -793,86 +793,86 @@ func EvaluateEqual(left, right reflect.Value) (reflect.Value, error) { left, right = GetValueElem(left), GetValueElem(right) switch left.Kind() { case reflect.String: - lv := left.String() + leftValue := left.String() if right.Kind() == reflect.String { - rv := right.String() + rightValue := right.String() - return reflect.ValueOf(lv == rv), nil + return reflect.ValueOf(leftValue == rightValue), nil } return reflect.ValueOf(false), nil case reflect.Bool: - lv := left.Bool() + leftValue := left.Bool() if right.Kind() == reflect.Bool { - rv := right.Bool() + rightValue := right.Bool() - return reflect.ValueOf(lv == rv), nil + return reflect.ValueOf(leftValue == rightValue), nil } return reflect.ValueOf(false), nil case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: - lv := left.Int() + leftValue := left.Int() switch right.Kind() { case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: - rv := right.Int() + rightValue := right.Int() - return reflect.ValueOf(lv == rv), nil + return reflect.ValueOf(leftValue == rightValue), nil case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: - rv := right.Uint() + rightValue := right.Uint() - return reflect.ValueOf(lv == int64(rv)), nil + return reflect.ValueOf(leftValue == int64(rightValue)), nil case reflect.Float32, reflect.Float64: - rv := right.Float() + rightValue := right.Float() - return reflect.ValueOf(float64(lv) == rv), nil + return reflect.ValueOf(float64(leftValue) == rightValue), nil default: return reflect.ValueOf(nil), fmt.Errorf("can not use data type of %s in EQ comparison", right.Kind().String()) } case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: - lv := left.Uint() + leftValue := left.Uint() switch right.Kind() { case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: - rv := right.Int() + rightValue := right.Int() - return reflect.ValueOf(int64(lv) == rv), nil + return reflect.ValueOf(int64(leftValue) == rightValue), nil case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: - rv := right.Uint() + rightValue := right.Uint() - return reflect.ValueOf(lv == rv), nil + return reflect.ValueOf(leftValue == rightValue), nil case reflect.Float32, reflect.Float64: - rv := right.Float() + rightValue := right.Float() - return reflect.ValueOf(float64(lv) == rv), nil + return reflect.ValueOf(float64(leftValue) == rightValue), nil default: return reflect.ValueOf(nil), fmt.Errorf("can not use data type of %s in EQ comparison", right.Kind().String()) } case reflect.Float32, reflect.Float64: - lv := left.Float() + leftValue := left.Float() switch right.Kind() { case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: - rv := right.Int() + rightValue := right.Int() - return reflect.ValueOf(lv == float64(rv)), nil + return reflect.ValueOf(leftValue == float64(rightValue)), nil case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: - rv := right.Uint() + rightValue := right.Uint() - return reflect.ValueOf(lv == float64(rv)), nil + return reflect.ValueOf(leftValue == float64(rightValue)), nil case reflect.Float32, reflect.Float64: - rv := right.Float() + rightValue := right.Float() - return reflect.ValueOf(lv == rv), nil + return reflect.ValueOf(leftValue == rightValue), nil default: return reflect.ValueOf(nil), fmt.Errorf("can not use data type of %s in EQ comparison", right.Kind().String()) } default: if left.Type().String() == "time.Time" && right.Type().String() == "time.Time" { - lv := left.Interface().(time.Time) - rv := right.Interface().(time.Time) + leftValue := left.Interface().(time.Time) + rightValue := right.Interface().(time.Time) - return reflect.ValueOf(lv == rv), nil + return reflect.ValueOf(leftValue == rightValue), nil } return reflect.ValueOf(nil), fmt.Errorf("can not use data type of %s in EQ comparison", left.Kind().String()) @@ -884,86 +884,86 @@ func EvaluateNotEqual(left, right reflect.Value) (reflect.Value, error) { left, right = GetValueElem(left), GetValueElem(right) switch left.Kind() { case reflect.String: - lv := left.String() + leftValue := left.String() if right.Kind() == reflect.String { - rv := right.String() + rightValue := right.String() - return reflect.ValueOf(lv != rv), nil + return reflect.ValueOf(leftValue != rightValue), nil } return reflect.ValueOf(false), nil case reflect.Bool: - lv := left.Bool() + leftValue := left.Bool() if right.Kind() == reflect.Bool { - rv := right.Bool() + rightValue := right.Bool() - return reflect.ValueOf(lv != rv), nil + return reflect.ValueOf(leftValue != rightValue), nil } return reflect.ValueOf(false), nil case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: - lv := left.Int() + leftValue := left.Int() switch right.Kind() { case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: - rv := right.Int() + rightValue := right.Int() - return reflect.ValueOf(lv != rv), nil + return reflect.ValueOf(leftValue != rightValue), nil case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: - rv := right.Uint() + rightValue := right.Uint() - return reflect.ValueOf(lv != int64(rv)), nil + return reflect.ValueOf(leftValue != int64(rightValue)), nil case reflect.Float32, reflect.Float64: - rv := right.Float() + rightValue := right.Float() - return reflect.ValueOf(float64(lv) != rv), nil + return reflect.ValueOf(float64(leftValue) != rightValue), nil default: return reflect.ValueOf(nil), fmt.Errorf("can not use data type of %s in EQ comparison", right.Kind().String()) } case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: - lv := left.Uint() + leftValue := left.Uint() switch right.Kind() { case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: - rv := right.Int() + rightValue := right.Int() - return reflect.ValueOf(int64(lv) != rv), nil + return reflect.ValueOf(int64(leftValue) != rightValue), nil case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: - rv := right.Uint() + rightValue := right.Uint() - return reflect.ValueOf(lv != rv), nil + return reflect.ValueOf(leftValue != rightValue), nil case reflect.Float32, reflect.Float64: - rv := right.Float() + rightValue := right.Float() - return reflect.ValueOf(float64(lv) != rv), nil + return reflect.ValueOf(float64(leftValue) != rightValue), nil default: return reflect.ValueOf(nil), fmt.Errorf("can not use data type of %s in EQ comparison", right.Kind().String()) } case reflect.Float32, reflect.Float64: - lv := left.Float() + leftValue := left.Float() switch right.Kind() { case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: - rv := right.Int() + rightValue := right.Int() - return reflect.ValueOf(lv != float64(rv)), nil + return reflect.ValueOf(leftValue != float64(rightValue)), nil case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: - rv := right.Uint() + rightValue := right.Uint() - return reflect.ValueOf(lv != float64(rv)), nil + return reflect.ValueOf(leftValue != float64(rightValue)), nil case reflect.Float32, reflect.Float64: - rv := right.Float() + rightValue := right.Float() - return reflect.ValueOf(lv != rv), nil + return reflect.ValueOf(leftValue != rightValue), nil default: return reflect.ValueOf(nil), fmt.Errorf("can not use data type of %s in EQ comparison", right.Kind().String()) } default: if left.Type().String() == "time.Time" && right.Type().String() == "time.Time" { - lv := left.Interface().(time.Time) - rv := right.Interface().(time.Time) + leftValue := left.Interface().(time.Time) + rightValue := right.Interface().(time.Time) - return reflect.ValueOf(lv != rv), nil + return reflect.ValueOf(leftValue != rightValue), nil } return reflect.ValueOf(nil), fmt.Errorf("can not use data type of %s in EQ comparison", left.Kind().String()) @@ -974,10 +974,10 @@ func EvaluateNotEqual(left, right reflect.Value) (reflect.Value, error) { func EvaluateLogicAnd(left, right reflect.Value) (reflect.Value, error) { left, right = GetValueElem(left), GetValueElem(right) if left.Kind() == reflect.Bool && right.Kind() == reflect.Bool { - lv := left.Bool() - rv := right.Bool() + leftValue := left.Bool() + rightValue := right.Bool() - return reflect.ValueOf(lv && rv), nil + return reflect.ValueOf(leftValue && rightValue), nil } return reflect.ValueOf(nil), fmt.Errorf("can not use data type of %s in Logical AND comparison", left.Kind().String()) @@ -987,10 +987,10 @@ func EvaluateLogicAnd(left, right reflect.Value) (reflect.Value, error) { func EvaluateLogicOr(left, right reflect.Value) (reflect.Value, error) { left, right = GetValueElem(left), GetValueElem(right) if left.Kind() == reflect.Bool && right.Kind() == reflect.Bool { - lv := left.Bool() - rv := right.Bool() + leftValue := left.Bool() + rightValue := right.Bool() - return reflect.ValueOf(lv || rv), nil + return reflect.ValueOf(leftValue || rightValue), nil } return reflect.ValueOf(nil), fmt.Errorf("can not use data type of %s in Logical OR comparison", left.Kind().String()) @@ -1000,9 +1000,9 @@ func EvaluateLogicOr(left, right reflect.Value) (reflect.Value, error) { func EvaluateLogicSingle(left reflect.Value) (reflect.Value, error) { left = GetValueElem(left) if left.Kind() == reflect.Bool { - lv := left.Bool() + leftValue := left.Bool() - return reflect.ValueOf(lv), nil + return reflect.ValueOf(leftValue), nil } return reflect.ValueOf(nil), fmt.Errorf("can not use data type of %s in Logical AND comparison", left.Kind().String()) diff --git a/pkg/reflectools.go b/pkg/reflectools.go index 40007ce8..38ad47a1 100755 --- a/pkg/reflectools.go +++ b/pkg/reflectools.go @@ -141,61 +141,62 @@ func IsStruct(val reflect.Value) bool { // ValueToInterface will try to obtain an interface to a speciffic value. // it will detect the value's kind. -func ValueToInterface(v reflect.Value) interface{} { - if v.Type().Kind() == reflect.String { - return v.String() +func ValueToInterface(valueToConvert reflect.Value) interface{} { + if valueToConvert.Type().Kind() == reflect.String { + + return valueToConvert.String() } - switch v.Type().Kind() { + switch valueToConvert.Type().Kind() { case reflect.Int: - return int(v.Int()) + return int(valueToConvert.Int()) case reflect.Int8: - return int8(v.Int()) + return int8(valueToConvert.Int()) case reflect.Int16: - return int16(v.Int()) + return int16(valueToConvert.Int()) case reflect.Int32: - return int32(v.Int()) + return int32(valueToConvert.Int()) case reflect.Int64: - return v.Int() + return valueToConvert.Int() case reflect.Uint: - return uint(v.Uint()) + return uint(valueToConvert.Uint()) case reflect.Uint8: - return uint8(v.Uint()) + return uint8(valueToConvert.Uint()) case reflect.Uint16: - return uint16(v.Uint()) + return uint16(valueToConvert.Uint()) case reflect.Uint32: - return uint32(v.Uint()) + return uint32(valueToConvert.Uint()) case reflect.Uint64: - return v.Uint() + return valueToConvert.Uint() case reflect.Float32: - return float32(v.Float()) + return float32(valueToConvert.Float()) case reflect.Float64: - return v.Float() + return valueToConvert.Float() case reflect.Bool: - return v.Bool() + return valueToConvert.Bool() case reflect.Ptr: - newPtr := reflect.New(v.Elem().Type()) - newPtr.Elem().Set(v.Elem()) + newPtr := reflect.New(valueToConvert.Elem().Type()) + newPtr.Elem().Set(valueToConvert.Elem()) return newPtr.Interface() case reflect.Struct: - if v.CanInterface() { + if valueToConvert.CanInterface() { - return v.Interface() + return valueToConvert.Interface() } - logger.Log.Errorf("Can't interface value of struct %v", v) + logger.Log.Errorf("Can't interface value of struct %v", valueToConvert) return nil default: @@ -582,27 +583,27 @@ func IsAttributeNilOrZero(obj reflect.Value, fieldName string) (bool, error) { return false, nil } -func reflectIsZero(v reflect.Value) bool { - switch v.Kind() { +func reflectIsZero(value reflect.Value) bool { + switch value.Kind() { case reflect.Bool: - return !v.Bool() + return !value.Bool() case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: - return v.Int() == 0 + return value.Int() == 0 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: - return v.Uint() == 0 + return value.Uint() == 0 case reflect.Float32, reflect.Float64: - return math.Float64bits(v.Float()) == 0 + return math.Float64bits(value.Float()) == 0 case reflect.Complex64, reflect.Complex128: - c := v.Complex() + c := value.Complex() return math.Float64bits(real(c)) == 0 && math.Float64bits(imag(c)) == 0 case reflect.Array: - for i := 0; i < v.Len(); i++ { - if !reflectIsZero(v.Index(i)) { + for i := 0; i < value.Len(); i++ { + if !reflectIsZero(value.Index(i)) { return false } @@ -611,13 +612,13 @@ func reflectIsZero(v reflect.Value) bool { return true case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Ptr, reflect.Slice, reflect.UnsafePointer: - return v.IsNil() + return value.IsNil() case reflect.String: - return v.Len() == 0 + return value.Len() == 0 case reflect.Struct: - for i := 0; i < v.NumField(); i++ { - if !reflectIsZero(v.Field(i)) { + for i := 0; i < value.NumField(); i++ { + if !reflectIsZero(value.Field(i)) { return false } @@ -626,7 +627,7 @@ func reflectIsZero(v reflect.Value) bool { return true default: - panic(&reflect.ValueError{Method: "reflect.Value.IsZero", Kind: v.Kind()}) + panic(&reflect.ValueError{Method: "reflect.Value.IsZero", Kind: value.Kind()}) } } diff --git a/pkg/resource.go b/pkg/resource.go index 8268268d..3a4013d4 100644 --- a/pkg/resource.go +++ b/pkg/resource.go @@ -15,18 +15,24 @@ package pkg import ( + "context" "fmt" "gopkg.in/src-d/go-billy.v4" "io" "io/ioutil" "net/http" "path/filepath" + "time" "github.com/hyperjumptech/grule-rule-engine/logger" "github.com/bmatcuk/doublestar" ) +var ( + URLResourceTimeoutSecond = 1800 // 30 minutes +) + // ResourceBundle is a helper struct to help load multiple resource at once. type ResourceBundle interface { Load() ([]Resource, error) @@ -256,7 +262,10 @@ func (res *URLResource) Load() ([]byte, error) { return res.Bytes, nil } client := &http.Client{} - req, err := http.NewRequest(http.MethodGet, res.URL, nil) + + ctx, _ := context.WithTimeout(context.Background(), time.Duration(URLResourceTimeoutSecond)*time.Second) + req, err := http.NewRequestWithContext(ctx, http.MethodGet, res.URL, nil) + if len(res.Header) > 0 { req.Header = res.Header } @@ -316,9 +325,9 @@ type GITResourceBundle struct { PathPattern []string } -func (bundle *GITResourceBundle) loadPath(url, path string, fs billy.Filesystem) ([]Resource, error) { +func (bundle *GITResourceBundle) loadPath(url, path string, fileSyst billy.Filesystem) ([]Resource, error) { logger.Log.Tracef("Enter directory %s", path) - finfos, err := fs.ReadDir(path) + finfos, err := fileSyst.ReadDir(path) if err != nil { return nil, err @@ -330,7 +339,7 @@ func (bundle *GITResourceBundle) loadPath(url, path string, fs billy.Filesystem) fulPath = fmt.Sprintf("/%s", finfo.Name()) } if finfo.IsDir() { - gres, err := bundle.loadPath(url, fulPath, fs) + gres, err := bundle.loadPath(url, fulPath, fileSyst) if err != nil { return nil, err @@ -345,7 +354,7 @@ func (bundle *GITResourceBundle) loadPath(url, path string, fs billy.Filesystem) } if matched { logger.Log.Debugf("Loading git file %s", fulPath) - f, err := fs.Open(fulPath) + f, err := fileSyst.Open(fulPath) if err != nil { return nil, err