...

Source file src/dario.cat/mergo/issue138_test.go

Documentation: dario.cat/mergo

     1  package mergo_test
     2  
     3  import (
     4  	"encoding/json"
     5  	"testing"
     6  
     7  	"dario.cat/mergo"
     8  )
     9  
    10  const issue138configuration string = `
    11  {
    12  	"Port": 80
    13  }
    14  `
    15  
    16  func TestIssue138(t *testing.T) {
    17  	type config struct {
    18  		Port uint16
    19  	}
    20  	type compatibleConfig struct {
    21  		Port float64
    22  	}
    23  
    24  	foo := make(map[string]interface{})
    25  	// encoding/json unmarshals numbers as float64
    26  	// https://golang.org/pkg/encoding/json/#Unmarshal
    27  	json.Unmarshal([]byte(issue138configuration), &foo)
    28  
    29  	err := mergo.Map(&config{}, foo)
    30  	if err == nil {
    31  		t.Error("expected type mismatch error, got nil")
    32  	} else {
    33  		if err.Error() != "type mismatch on Port field: found float64, expected uint16" {
    34  			t.Errorf("expected type mismatch error, got %q", err)
    35  		}
    36  	}
    37  
    38  	c := compatibleConfig{}
    39  	if err := mergo.Map(&c, foo); err != nil {
    40  		t.Error(err)
    41  	}
    42  }
    43  

View as plain text