dot icon indicating copy to clipboard operation
dot copied to clipboard

unknown path: after map[string]any -> struct

Open NV4RE opened this issue 2 months ago • 2 comments

Problem:

The code encounters an unknown path: Shelf.Orange.Qty error. This happens because the dot package might not be designed to handle nested maps where keys are object

package main

import (
	"fmt"

	"github.com/mowshon/dot"
)

type MyShopStock struct {
	Shelf     map[string]any
	Werehouse map[string]any
}

type Product struct {
	Price float64
	Name  string
	Qty   uint
}

type Inventory struct {
	Name string
	Qty  uint
}

func main() {
	data := &MyShopStock{
		Shelf: map[string]any{
			"Apple":  Product{Price: 1.99, Name: "apple", Qty: 10},
			"Banana": Product{Price: 0.99, Name: "banana", Qty: 5},
			"Orange": Product{},
		},
	}

	obj, err := dot.New(data)
	if err != nil {
		panic(err)
	}

	err = obj.Insert("Shelf.Orange.Qty", 5)
	if err != nil {
		panic(err)
	}

	fmt.Println(data.Shelf["Orange"])
}

https://go.dev/play/p/wBnrgKh67VU

Expected Behavior:

The Insert operation should successfully set the Qty for "Orange" to 5 without errors.

Observations:

It seems the dot package might not be handling nested maps as expected.

NV4RE avatar Apr 05 '24 17:04 NV4RE