mirror of
https://github.com/semaphoreui/semaphore.git
synced 2024-11-23 20:35:24 +01:00
43 lines
730 B
Go
43 lines
730 B
Go
|
package api
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"testing"
|
||
|
)
|
||
|
|
||
|
func TestStructToMap(t *testing.T) {
|
||
|
type Address struct {
|
||
|
City string `json:"city"`
|
||
|
State string `json:"state"`
|
||
|
}
|
||
|
|
||
|
type Person struct {
|
||
|
Name string `json:"name"`
|
||
|
Age int `json:"age"`
|
||
|
Email string `json:"email"`
|
||
|
Active bool `json:"active"`
|
||
|
Address Address `json:"address"`
|
||
|
}
|
||
|
|
||
|
// Create an instance of the struct
|
||
|
p := Person{
|
||
|
Name: "John Doe",
|
||
|
Age: 30,
|
||
|
Email: "johndoe@example.com",
|
||
|
Active: true,
|
||
|
Address: Address{
|
||
|
City: "New York",
|
||
|
State: "NY",
|
||
|
},
|
||
|
}
|
||
|
|
||
|
// Convert the struct to a flat map
|
||
|
flatMap := structToFlatMap(&p)
|
||
|
|
||
|
if flatMap["address.city"] != "New York" {
|
||
|
t.Fail()
|
||
|
}
|
||
|
// Print the map
|
||
|
fmt.Println(flatMap)
|
||
|
}
|