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

Inserting data into any type #3

Merged
merged 1 commit into from
Apr 22, 2024
Merged

Inserting data into any type #3

merged 1 commit into from
Apr 22, 2024

Conversation

mowshon
Copy link
Owner

@mowshon mowshon commented Apr 22, 2024

@NV4RE described the unusual behaviour of the dot package: #2

The problem is that if the data insertion path is of type interface{} then dot won't know how to determine the further path.

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
}

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)

	err = obj.Insert("Shelf.Orange.Qty", 5)
	fmt.Println(err)
}

After this PR, this code will return an error:

the type in Shelf.Orange is interface{} and it is impossible to further predict the path

Possible solution

If the final destination path is of type any, the value will be inserted without any problems.

err = obj.Insert("Shelf.Orange", Product{
	Qty: 5,
})

Copy link

@mowshon mowshon merged commit ecbf08c into main Apr 22, 2024
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant