...

Source file src/github.com/pelletier/go-toml/v2/internal/testsuite/add.go

Documentation: github.com/pelletier/go-toml/v2/internal/testsuite

     1  package testsuite
     2  
     3  import (
     4  	"fmt"
     5  	"math"
     6  	"time"
     7  
     8  	"github.com/pelletier/go-toml/v2"
     9  )
    10  
    11  // addTag adds JSON tags to a data structure as expected by toml-test.
    12  func addTag(key string, tomlData interface{}) interface{} {
    13  	// Switch on the data type.
    14  	switch orig := tomlData.(type) {
    15  	default:
    16  		//return map[string]interface{}{}
    17  		panic(fmt.Sprintf("Unknown type: %T", tomlData))
    18  
    19  	// A table: we don't need to add any tags, just recurse for every table
    20  	// entry.
    21  	case map[string]interface{}:
    22  		typed := make(map[string]interface{}, len(orig))
    23  		for k, v := range orig {
    24  			typed[k] = addTag(k, v)
    25  		}
    26  		return typed
    27  
    28  	// An array: we don't need to add any tags, just recurse for every table
    29  	// entry.
    30  	case []map[string]interface{}:
    31  		typed := make([]map[string]interface{}, len(orig))
    32  		for i, v := range orig {
    33  			typed[i] = addTag("", v).(map[string]interface{})
    34  		}
    35  		return typed
    36  	case []interface{}:
    37  		typed := make([]interface{}, len(orig))
    38  		for i, v := range orig {
    39  			typed[i] = addTag("", v)
    40  		}
    41  		return typed
    42  
    43  	// Datetime: tag as datetime.
    44  	case toml.LocalTime:
    45  		return tag("time-local", orig.String())
    46  	case toml.LocalDate:
    47  		return tag("date-local", orig.String())
    48  	case toml.LocalDateTime:
    49  		return tag("datetime-local", orig.String())
    50  	case time.Time:
    51  		return tag("datetime", orig.Format("2006-01-02T15:04:05.999999999Z07:00"))
    52  
    53  	// Tag primitive values: bool, string, int, and float64.
    54  	case bool:
    55  		return tag("bool", fmt.Sprintf("%v", orig))
    56  	case string:
    57  		return tag("string", orig)
    58  	case int64:
    59  		return tag("integer", fmt.Sprintf("%d", orig))
    60  	case float64:
    61  		// Special case for nan since NaN == NaN is false.
    62  		if math.IsNaN(orig) {
    63  			return tag("float", "nan")
    64  		}
    65  		return tag("float", fmt.Sprintf("%v", orig))
    66  	}
    67  }
    68  
    69  func tag(typeName string, data interface{}) map[string]interface{} {
    70  	return map[string]interface{}{
    71  		"type":  typeName,
    72  		"value": data,
    73  	}
    74  }
    75  

View as plain text