Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
NV4RE opened this issue Apr 5, 2024 · 2 comments
Open

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

NV4RE opened this issue Apr 5, 2024 · 2 comments
Assignees
Labels
bug Something isn't working

Comments

@NV4RE
Copy link

NV4RE commented Apr 5, 2024

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.

@mowshon
Copy link
Owner

mowshon commented Apr 22, 2024

@NV4RE Yeah, that's right. Type any can cause a lot of headaches.

The thing is, you insert Shelf.Orange.Qty, but what is Orange if the type is any ? Anything.

The dot package looks at the type of the field from the structure, but it doesn't know what type was specified to the field during the initialisation of the structure.

But, I see your point. I'll try to find a solution.

@mowshon mowshon added the bug Something isn't working label Apr 22, 2024
@mowshon mowshon self-assigned this Apr 22, 2024
@mowshon
Copy link
Owner

mowshon commented Apr 22, 2024

Created a PR and published a new version v1.0.1 where some issues with any type have been fixed.

PR: #3
Release: https://github.com/mowshon/dot/releases/tag/v1.0.1

It is not possible to completely solve your problem at the moment. But thank you for informing me that this problem exists.

@NV4RE, thank you for your contribution to the development of open source projects.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants