map2struct_test.go 735 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package parser
  2. import "testing"
  3. func TestMapToStruct(t *testing.T) {
  4. m := map[string]any{
  5. "result": "result",
  6. "inherit": map[string]any{
  7. "inherit_result": "result",
  8. },
  9. "object": map[string]any{
  10. "a": 1,
  11. },
  12. }
  13. type p struct {
  14. Inherit struct {
  15. InheritResult string `json:"inherit_result"`
  16. }
  17. }
  18. type s struct {
  19. p
  20. Result string `json:"result"`
  21. Object struct {
  22. A int `json:"a"`
  23. } `json:"object"`
  24. }
  25. result, err := MapToStruct[s](m)
  26. if err != nil {
  27. t.Error(err)
  28. }
  29. if result.Result != "result" {
  30. t.Error("result should be result")
  31. }
  32. if result.Inherit.InheritResult != "result" {
  33. t.Error("inherit_result should be result")
  34. }
  35. if result.Object.A != 1 {
  36. t.Error("a should be 1")
  37. }
  38. }