Skip to content

Commit

Permalink
Staticcheck: remove Yoda conditions (ST1017)
Browse files Browse the repository at this point in the history
  • Loading branch information
gotnospirit committed Sep 30, 2022
1 parent 89e2ff0 commit 1659d33
Show file tree
Hide file tree
Showing 12 changed files with 72 additions and 72 deletions.
6 changes: 3 additions & 3 deletions literal.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ func formatLiteral(expr Expression, ptr_output *bytes.Buffer, _ *map[string]inte
content := expr.([]string)

for _, c := range content {
if "" != c {
if c != "" {
ptr_output.WriteString(c)
} else if "" != pound {
} else if pound != "" {
ptr_output.WriteString(pound)
} else {
ptr_output.WriteRune(PoundChar)
Expand All @@ -30,7 +30,7 @@ func parseLiteral(start, end int, ptr_input *[]rune) []string {
for i := start; i < end; i++ {
c := input[i]

if EscapeChar == c {
if c == EscapeChar {
gap++
e++
escaped = true
Expand Down
10 changes: 5 additions & 5 deletions messageformat.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ type MessageFormat struct {

func (x *MessageFormat) SetCulture(name string) error {
fn, err := plural.GetFunc(name)
if nil != err {
if err != nil {
return err
}
x.plural = fn
return nil
}

func (x *MessageFormat) SetPluralFunction(fn pluralFunc) error {
if nil == fn {
if fn == nil {
return fmt.Errorf("PluralFunctionRequired")
}
x.plural = fn
Expand All @@ -40,14 +40,14 @@ func (x *MessageFormat) FormatMap(data map[string]interface{}) (string, error) {
var buf bytes.Buffer

err := x.root.format(&buf, &data, x, "")
if nil != err {
if err != nil {
return "", err
}
return buf.String(), nil
}

func (x *MessageFormat) getNamedKey(value interface{}, ordinal bool) (string, error) {
if nil == x.plural {
if x.plural == nil {
return "", fmt.Errorf("UndefinedPluralFunc")
}
return x.plural(value, ordinal), nil
Expand All @@ -57,7 +57,7 @@ func (x *MessageFormat) getFormatter(key string) (formatFunc, error) {
fn, ok := x.formatters[key]
if !ok {
return nil, fmt.Errorf("UnknownType: `%s`", key)
} else if nil == fn {
} else if fn == nil {
return nil, fmt.Errorf("UndefinedFormatFunc: `%s`", key)
}
return fn, nil
Expand Down
16 changes: 8 additions & 8 deletions messageformat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,19 @@ import (

func doParse(input string) (*MessageFormat, error) {
o, err := New()
if nil != err {
if err != nil {
return nil, err
}
mf, err := o.Parse(input)
if nil != err {
if err != nil {
return nil, err
}
return mf, nil
}

func TestSetPluralFunction(t *testing.T) {
mf, err := doParse(`{N,plural,one{1}other{2}}`)
if nil != err {
if err != nil {
t.Errorf("Unexpected parse failure: `%s`", err.Error())
} else {
// checks we can't unset the default plural function
Expand All @@ -44,24 +44,24 @@ func TestSetPluralFunction(t *testing.T) {

data := map[string]interface{}{"N": 1}
result, err := mf.FormatMap(data)
if nil != err {
if err != nil {
t.Errorf("Unexpected error : `%s`", err.Error())
} else if "1" != result {
} else if result != "1" {
t.Errorf("Unexpected format result : `%s`", result)
} else {
// checks we can set a new plural function and get a different result
err := mf.SetPluralFunction(func(interface{}, bool) string {
return "other"
})

if nil != err {
if err != nil {
t.Errorf("Unexpected error <%s>", err)
} else {
result, err := mf.FormatMap(data)

if nil != err {
if err != nil {
t.Errorf("Unexpected error : `%s`", err.Error())
} else if "2" != result {
} else if result != "2" {
t.Errorf("Unexpected format result : `%s`", result)
} else if testing.Verbose() {
fmt.Printf("- Got expected value <%s>\n", result)
Expand Down
4 changes: 2 additions & 2 deletions node.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ func (x *node) format(ptr_output *bytes.Buffer, data *map[string]interface{}, pt
ctype := child.ctype

fn, err := ptr_mf.getFormatter(ctype)
if nil != err {
if err != nil {
return err
}

err = fn(child.expr, ptr_output, data, ptr_mf, pound)
if nil != err {
if err != nil {
return err
}
}
Expand Down
8 changes: 4 additions & 4 deletions ordinal.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func formatOrdinal(expr Expression, ptr_output *bytes.Buffer, data *map[string]i
o := expr.(*selectExpr)

value, err := toString(*data, o.key)
if nil != err {
if err != nil {
return err
}

Expand All @@ -34,19 +34,19 @@ func formatOrdinal(expr Expression, ptr_output *bytes.Buffer, data *map[string]i

case string:
_, err := strconv.ParseFloat(v.(string), 64)
if nil != err {
if err != nil {
return err
}
}

key, err := ptr_mf.getNamedKey(v, true)
if nil != err {
if err != nil {
return err
}
choice = o.choices[key]
}

if nil == choice {
if choice == nil {
choice = o.choices["other"]
}
return choice.format(ptr_output, data, ptr_mf, value)
Expand Down
22 changes: 11 additions & 11 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ func (x *Parser) Parse(input string) (*MessageFormat, error) {
root := node{}
for pos < end {
i, level, err := x.parse(pos, end, &runes, &root)
if nil != err {
if err != nil {
return nil, parseError{err.Error(), i}
} else if 0 != level {
} else if level != 0 {
return nil, parseError{"UnbalancedBraces", i}
}

Expand All @@ -58,32 +58,32 @@ func (x *Parser) Register(key string, p parseFunc, f formatFunc) error {

func (x *Parser) parseExpression(start, end int, ptr_input *[]rune) (string, Expression, int, error) {
varname, char, pos, err := readVar(start, end, ptr_input)
if nil != err {
if err != nil {
return "", nil, pos, err
} else if "" == varname {
} else if varname == "" {
return "", nil, pos, fmt.Errorf("MissingVarName")
} else if CloseChar == char {
} else if char == CloseChar {
return "var", varname, pos, nil
}

ctype, char, pos, err := readVar(pos+1, end, ptr_input)
if nil != err {
if err != nil {
return "", nil, pos, err
}

fn, ok := x.parsers[ctype]
if !ok {
return "", nil, pos, fmt.Errorf("UnknownType: `%s`", ctype)
} else if nil == fn {
} else if fn == nil {
return "", nil, pos, fmt.Errorf("UndefinedParseFunc: `%s`", ctype)
}

expr, pos, err := fn(varname, x, char, pos, end, ptr_input)
if nil != err {
if err != nil {
return "", nil, pos, err
}

if pos >= end || CloseChar != (*ptr_input)[pos] {
if pos >= end || (*ptr_input)[pos] != CloseChar {
return "", nil, pos, fmt.Errorf("UnbalancedBraces")
}
return ctype, expr, pos, nil
Expand Down Expand Up @@ -125,7 +125,7 @@ loop:
}

ctype, child, i, err := x.parseExpression(pos+1, end, ptr_input)
if nil != err {
if err != nil {
return i, level, err
}

Expand All @@ -150,7 +150,7 @@ loop:

func NewWithCulture(name string) (*Parser, error) {
fn, err := plural.GetFunc(name)
if nil != err {
if err != nil {
return nil, err
}

Expand Down
14 changes: 7 additions & 7 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ func doTest(t *testing.T, data Test) {
} else {
mf, err := o.Parse(data.input)

if nil != err {
if err != nil {
t.Errorf("`%s` threw <%s>", data.input, err)
} else {
for _, ex := range data.expects {
result, err := mf.FormatMap(ex.data)
if nil != err {
if err != nil {
t.Errorf("`%s` threw <%s>", data.input, err)
} else if result != ex.output {
t.Errorf("Expecting <%v> but got <%v>", ex.output, result)
Expand All @@ -46,7 +46,7 @@ func doTestException(t *testing.T, input string, data map[string]interface{}, ex
} else {
mf, err := o.Parse(input)

if nil != err {
if err != nil {
doTestCompileError(t, input, expected, err)
} else {
_, err := mf.FormatMap(data)
Expand All @@ -65,7 +65,7 @@ func doTestParseException(t *testing.T, input, expected string) {
}

func doTestCompileError(t *testing.T, input, expected string, err error) {
if nil == err {
if err == nil {
t.Errorf("`%s` should threw <%s> but got none", input, expected)
} else if err.Error() != expected {
t.Errorf("`%s` should threw <%s> but got <%s>", input, expected, err.Error())
Expand All @@ -75,7 +75,7 @@ func doTestCompileError(t *testing.T, input, expected string, err error) {
}

func doTestError(t *testing.T, expected string, err error) {
if nil == err {
if err == nil {
t.Errorf("Expecting exception <%s> but got none", expected)
} else if err.Error() != expected {
t.Errorf("Expecting exception <%s> but got <%s>", expected, err.Error())
Expand Down Expand Up @@ -341,7 +341,7 @@ func TestMultiline(t *testing.T) {

func TestRegister(t *testing.T) {
o, err := New()
if nil != err {
if err != nil {
t.Errorf("Unexpected parse failure: `%s`", err.Error())
} else {
// checks default types can't be overloaded
Expand All @@ -356,7 +356,7 @@ func TestRegister(t *testing.T) {

// nil parseFunc and/or formatFunc are accepted (even if parsing will leads to an error!)
err = o.Register("noparse", nil, nil)
if nil != err {
if err != nil {
t.Errorf("Unexpected error: %s", err.Error())
}

Expand Down
Loading

0 comments on commit 1659d33

Please sign in to comment.