From: komkom <info@komika.org>
Date: Sat, 18 Jun 2022 19:18:49 +0200
Subject: Fix failing tests on 32 bit architectures

Closes: #1004048
Origin: upstream, https://github.com/komkom/toml/pull/6
---
 internal/convert.go      |  8 +++---
 internal/convert_test.go |  4 +--
 internal/filter.go       | 14 +++++-----
 reader_test.go           | 67 ++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 80 insertions(+), 13 deletions(-)

diff --git a/internal/convert.go b/internal/convert.go
index 683e2d1..599d01f 100644
--- a/internal/convert.go
+++ b/internal/convert.go
@@ -53,7 +53,7 @@ func fromBin(r rune) (int, error) {
 	return 0, fmt.Errorf(`not valid bin value`)
 }
 
-func addNumber(r rune, total int, t NumberType) (int, error) {
+func addNumber(r rune, total int64, t NumberType) (int64, error) {
 
 	switch t {
 	case BinNumberType:
@@ -67,7 +67,7 @@ func addNumber(r rune, total int, t NumberType) (int, error) {
 	return 0, fmt.Errorf(`addRune type not supported`)
 }
 
-func add(r rune, total int, f from, base int) (int, error) {
+func add(r rune, total int64, f from, base int) (int64, error) {
 
 	v, err := f(r)
 	if err != nil {
@@ -78,7 +78,7 @@ func add(r rune, total int, f from, base int) (int, error) {
 		return 0, nil
 	}
 
-	total *= base
-	total += v
+	total *= int64(base)
+	total += int64(v)
 	return total, nil
 }
diff --git a/internal/convert_test.go b/internal/convert_test.go
index e971f95..0d6f45b 100644
--- a/internal/convert_test.go
+++ b/internal/convert_test.go
@@ -11,7 +11,7 @@ func TestConvert(t *testing.T) {
 
 	tests := []struct {
 		value    string
-		expected int
+		expected int64
 	}{
 		{
 			value:    `0x12AFE`,
@@ -46,7 +46,7 @@ func TestConvert(t *testing.T) {
 	var err error
 	for _, ts := range tests {
 
-		var total int
+		var total int64
 
 		for _, r := range ts.value[2:] {
 			if ts.value[:2] == `0x` {
diff --git a/internal/filter.go b/internal/filter.go
index 3da7e0c..48ff96b 100644
--- a/internal/filter.go
+++ b/internal/filter.go
@@ -381,7 +381,7 @@ type Scope struct {
 	state     ScopeState
 	key       []string
 	scopeType ScopeType
-	counter   int
+	counter   int64
 	lastToken Token
 	parseFunc ParseFunc
 }
@@ -827,7 +827,7 @@ func PrefixNumber(ranges []*unicode.RangeTable, numberType NumberType) ParseFunc
 				return parseError(state, `invalid character at number end`)
 			}
 			state.PopScope()
-			state.Buf.WriteString(strconv.Itoa(scope.counter))
+			state.Buf.WriteString(strconv.FormatInt(scope.counter, 10))
 			return ErrDontAdvance
 		}
 
@@ -908,7 +908,7 @@ func Float(firstState ScopeState, firstToken Token, counter int) ParseFunc {
 		if scope.counter == 0 {
 			scope.state = firstState
 			scope.lastToken = firstToken
-			scope.counter = counter
+			scope.counter = int64(counter)
 		}
 
 		if unicode.IsSpace(r) && scope.lastToken == DIGITT {
@@ -1054,7 +1054,7 @@ func Time(offset int, val []rune, uptoMinutes bool) ParseFunc {
 	return func(r rune, state *State, scope *Scope) error {
 
 		if scope.counter == 0 {
-			scope.counter += offset
+			scope.counter += int64(offset)
 			state.data = val
 			state.Buf.WriteString(string(val))
 		}
@@ -1161,7 +1161,7 @@ func Date(offset int, val []rune) ParseFunc {
 		}
 
 		if scope.counter == 0 {
-			scope.counter += offset
+			scope.counter += int64(offset)
 			state.data = val
 			state.Buf.WriteString(string(val))
 		}
@@ -1315,14 +1315,14 @@ func InlineArray(r rune, state *State, scope *Scope) error {
 func LiteralValue(value []rune) ParseFunc {
 	return func(r rune, state *State, scope *Scope) error {
 
-		if scope.counter < len(value) {
+		if int(scope.counter) < len(value) {
 			if value[scope.counter] != r {
 				return parseError(state, `invalid literal value`)
 			}
 		}
 
 		scope.counter++
-		if scope.counter >= len(value) {
+		if int(scope.counter) >= len(value) {
 			state.PopScope()
 			return nil
 		}
diff --git a/reader_test.go b/reader_test.go
index f8b62fd..ec43cd0 100644
--- a/reader_test.go
+++ b/reader_test.go
@@ -3,9 +3,11 @@ package toml
 import (
 	"bytes"
 	"encoding/json"
+	"fmt"
 	"io/ioutil"
 	"os"
 	"path/filepath"
+	"strconv"
 	"strings"
 	"testing"
 
@@ -268,3 +270,68 @@ func TestSpecs_invalid(t *testing.T) {
 		}
 	}
 }
+
+func unwrap(m map[string]interface{}) (interface{}, bool, error) {
+
+	var tagType string
+	var tagValue string
+	var counter, keyCounter int
+	for key, value := range m {
+
+		if counter > 2 {
+			break
+		}
+		counter++
+
+		if key == `type` {
+			keyCounter++
+			tagType = value.(string)
+		}
+
+		if key == `value` {
+			keyCounter++
+			tagValue = value.(string)
+		}
+	}
+
+	if keyCounter < 2 {
+		return nil, false, nil
+	}
+
+	switch tagType {
+	case `integer`:
+		v, err := strconv.ParseInt(tagValue, 10, 64)
+		if err != nil {
+			return nil, false, fmt.Errorf(`unwarp integer failed %w`, err)
+		}
+		return float64(v), true, nil
+	case `float`:
+
+		switch tagValue {
+		case `nan`, `-nan`, `+nan`, `inf`, `-inf`, `+inf`:
+			return tagValue, true, nil
+		}
+
+		f, err := strconv.ParseFloat(tagValue, 64)
+		if err != nil {
+			return nil, false, fmt.Errorf("unwrap float failed %w", err)
+		}
+		return f, true, nil
+
+	case `bool`:
+
+		switch tagValue {
+		case `true`:
+			return true, true, nil
+		case `false`:
+			return false, true, nil
+		}
+
+		return nil, false, fmt.Errorf("unwrap bool failed %v", tagValue)
+	case `string`, `datetime`, `date-local`, `time-local`, `datetime-local`:
+		return tagValue, true, nil
+
+	}
+
+	panic(`no such type ` + tagType)
+}
