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

Support generic map slice #422

Open
renom opened this issue Jan 31, 2024 · 1 comment
Open

Support generic map slice #422

renom opened this issue Jan 31, 2024 · 1 comment

Comments

@renom
Copy link

renom commented Jan 31, 2024

Example:

package main

import "fmt"

type MapItem[K, V any] struct {
	Key   K
	Value V
}

type SpecificMapValue struct {
	Field1 string
	Field2 int
	Field3 bool
}

func main() {
	var mapSlice []MapItem[string, SpecificMapValue]
	// next we should do unmarshalling:
	// yaml.Unmarshal([]byte("some yaml here"), &mapSlice)
	// but generic map slices aren't supported yet
	fmt.Println(mapSlice)
}
@renom
Copy link
Author

renom commented Jan 31, 2024

Would it be enough to make MapItem an interface here?
Something like that:

type MapItem interface {
	KeyToAny() interface{}
	ValueToAny() interface{}
}

Then the generic MapSlice implementation would be out of go-yaml's scope.
E.g. the following code could be used in the app:

type MapSlice[K, V any] []MapItem[K, V]

type MapItem[K, V any] struct {
	Key   K
	Value V
}

func (m MapItem[K, V]) KeyToAny() interface{} {
	return m.Key
}

func (m MapItem[K, V]) ValueToAny() interface{} {
	return m.Value
}

Or the latter code could be on the go-yaml's side too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant