2024-07-07 19:12:21 +02:00
|
|
|
package db
|
|
|
|
|
|
|
|
import "testing"
|
|
|
|
|
|
|
|
func TestConfig_assignMapToStruct(t *testing.T) {
|
|
|
|
type Address struct {
|
|
|
|
Street string `json:"street"`
|
|
|
|
City string `json:"city"`
|
|
|
|
}
|
|
|
|
|
2024-07-08 20:56:59 +02:00
|
|
|
type Detail struct {
|
|
|
|
Value string `json:"value"`
|
|
|
|
Description string `json:"description"`
|
|
|
|
}
|
|
|
|
|
2024-07-07 19:12:21 +02:00
|
|
|
type User struct {
|
2024-07-08 20:56:59 +02:00
|
|
|
//Name string `json:"name"`
|
|
|
|
//Age int `json:"age"`
|
|
|
|
//Email string `json:"email"`
|
|
|
|
//Address Address `json:"address"`
|
|
|
|
Details map[string]Detail `json:"details"`
|
2024-07-07 19:12:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
johnData := map[string]interface{}{
|
2024-07-08 20:56:59 +02:00
|
|
|
//"name": "John Doe",
|
|
|
|
//"age": 30,
|
|
|
|
//"email": "john.doe@example.com",
|
|
|
|
//"address": map[string]interface{}{
|
|
|
|
// "street": "123 Main St",
|
|
|
|
// "city": "Anytown",
|
|
|
|
//},
|
|
|
|
"details": map[string]interface{}{
|
|
|
|
//"occupation": map[string]interface{}{
|
|
|
|
// "value": "engineer",
|
|
|
|
// "description": "Works with computers",
|
|
|
|
//},
|
|
|
|
//"hobby": map[string]interface{}{
|
|
|
|
// "value": "hiking",
|
|
|
|
// "description": "Enjoys the outdoors",
|
|
|
|
//},
|
|
|
|
"interests": map[string]interface{}{
|
|
|
|
"description": "Ho ho ho",
|
|
|
|
},
|
2024-07-07 19:12:21 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var john User
|
2024-07-08 20:56:59 +02:00
|
|
|
john.Details = make(map[string]Detail)
|
|
|
|
john.Details["interests"] = Detail{
|
|
|
|
Value: "politics",
|
|
|
|
Description: "Follows current events",
|
|
|
|
}
|
2024-07-07 19:12:21 +02:00
|
|
|
|
2024-07-08 20:56:59 +02:00
|
|
|
err := AssignMapToStruct(johnData, &john)
|
2024-07-07 19:12:21 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2024-07-08 11:45:18 +02:00
|
|
|
|
2024-07-08 20:56:59 +02:00
|
|
|
//if john.Name != "John Doe" {
|
|
|
|
// t.Errorf("Expected name to be John Doe but got %s", john.Name)
|
|
|
|
//}
|
|
|
|
|
|
|
|
if john.Details["interests"].Description != "Ho ho ho" {
|
|
|
|
t.Errorf("Expected interests description to be 'Ho ho ho' but got %s", john.Details["interests"].Description)
|
2024-07-08 11:45:18 +02:00
|
|
|
}
|
|
|
|
|
2024-07-08 20:56:59 +02:00
|
|
|
if john.Details["interests"].Value != "politics" {
|
|
|
|
t.Errorf("Expected interests to be politics but got '%s'", john.Details["interests"].Value)
|
2024-07-08 11:45:18 +02:00
|
|
|
}
|
2024-07-08 20:56:59 +02:00
|
|
|
|
|
|
|
//if john.Details["occupation"].Value != "engineer" {
|
|
|
|
// t.Errorf("Expected occupation to be engineer but got %s", john.Details["occupation"].Value)
|
|
|
|
//}
|
2024-07-07 19:12:21 +02:00
|
|
|
}
|