I have a struct where one of the fields is a map with the key being another struct. I am using Go version 16.1 and the map created that way is supposedly supported in this version (albeit unsure when field is a map). Here is the relevant code and test that fails with unsupported type error.
package model
import (
"encoding/json"
slog "github.com/go-eden/slf4go"
)
type User struct{
ID string `json:"id" bson:"_id,omitempty"`
FName string `json:"fName"`
MName string `json:"mName"`
LName string `json:"lName"`
Jobs map[*Job]float64 `json:"jobs,omitempty"`
Password string `json:"password"`
IsAdmin bool `json:"isAdmin"`
}
func (u *User)IsModel()bool{
return true
}
func(u * User)ToJSON()string{
//var jsonUser string;
b,err := json.Marshal(u)
if err !=nil {
slog.Info(err)
return err.Error()
}
return string(b)
}
func (u * User)FromJSON(jsonString string)bool{
err := json.Unmarshal([]byte(jsonString),u)
if err != nil{
return false
}
return true
}
and the struct job is:
package model
import "encoding/json"
type Job struct{
ID string `json:"id" bson:"_id,omitempty"`
Name string `json:"name"`
}
func(j *Job)IsModel()bool{
return true
}
func(j *Job) ToJSON()string{
b,err := json.Marshal(j)
if err !=nil {
return err.Error()
}
return string(b)
}
func(j *Job) FromJSON(jsonString string)bool{
err := json.Unmarshal([]byte(jsonString),j)
if err != nil{
return false
}
return true
}
Finally the test that fails is the following (marshalling fails after I add the job to the jobs map):
package model
import (
"fmt"
"reflect"
"testing"
)
func TestUser_ToJSON(t *testing.T) {
user := &User{
ID: "",
FName: "Mike",
MName: "",
LName: "Clovis",
Jobs: make(map[*Job]float64),
Password: "xyz",
IsAdmin: true,
}
newUser := &User{}
fmt.Println(user.ToJSON())
job := &Job{
ID: "1",
Name: "Cashier",
}
user.Jobs[job] = 12.99
fmt.Println("User as json")
fmt.Println(user.ToJSON())
newUser.FromJSON(user.ToJSON())
fmt.Println(newUser)
if reflect.DeepEqual(user,newUser) {
fmt.Println(newUser)
}else{
fmt.Println(user)
fmt.Println(newUser)
t.Error("Cannot marshal and unmarshal User struct")
}
dUser := *user
fmt.Println(dUser)
}
By my reading of the documentation this should work! If anyone has a suggestion or workaround I would appreciate it. As an FYI I have tried making the map with the key being both a job and as a pointer to the job with the same results.
Related
I have two yaml files and i want to show the difference between the two. The yaml files are below. I want to display difference between the two yamls.
Like under deletion it should be name:"first" and under updates it should be a full path something like profiles:names:description:"second yaml".
The code below gives me the full object in case of a deeply nested object. How do i get the full path?
YAML1
`
id: 5
name: "first"
repo: "some repo"
profiles:
id: 3
name: default
description: "first yaml"
data:
logs: []
config:
hosts:
url.com: minikube
`
YAML2
`
id: 5
repo: "some repo"
profiles:
id: 3
name: default
description: "second yaml"
data:
logs: []
config:
hosts:
url.com: minikube
`
`
package main
import (
"fmt"
"io/ioutil"
"log"
"reflect"
"gopkg.in/yaml.v3"
)
type Modifications struct {
addition map[string]interface{}
deletion map[string]interface{}
update map[string]interface{}
}
func NewModifications() *Modifications {
var m Modifications
m.addition = make(map[string]interface{})
m.deletion = make(map[string]interface{})
m.update = make(map[string]interface{})
return &m
}
func compareYaml(modifications *Modifications) {
// read file
yfile1, err := ioutil.ReadFile("./old.yaml")
if err != nil {
fmt.Println(err)
log.Fatal(err)
}
yfile2, err := ioutil.ReadFile("./new.yaml")
if err != nil {
log.Fatal(err)
//return
}
// unmarshal ymal
data1 := make(map[string]interface{})
if err := yaml.Unmarshal(yfile1, &data1); err != nil {
fmt.Println(err)
log.Fatal(err)
//return
}
data2 := make(map[string]interface{})
if err := yaml.Unmarshal(yfile2, &data2); err != nil {
fmt.Println(err)
log.Fatal(err)
//return
}
// from this we can iterate the key in data2 then check whether it exists in data1
// if so then we can update the value in data2
// iterate key1 in data2
for key1, value1 := range data1 {
// check whether key1 exists in data2
if _, exists := data2[key1]; exists {
// see if it is an update
if reflect.DeepEqual(data2[key1], value1) {
delete(data2, key1)
} else {
modifications.update[key1] = data2[key1]
delete(data2, key1)
}
} else {
modifications.deletion[key1] = value1
}
}
for k, v := range data2 {
modifications.addition[k] = v
}
}
func main() {
modifications := NewModifications()
compareYaml(modifications)
fmt.Println("************")
fmt.Println("adddition")
fmt.Println(modifications.addition)
fmt.Println("************")
fmt.Println("deletion")
fmt.Println(modifications.deletion)
fmt.Println("************")
fmt.Println("update")
fmt.Println(modifications.update)
fmt.Println("************")
}
`
Playground
You may want to use the compare package:
https://github.com/google/go-cmp
There is a cmp.Diff that allows you to compare two objects and will return the diff in string format.
I would like to directly navigate to a value in a map. Lets be more specific with the following go code example which should give me the value of "Walter" directly: (https://play.golang.org/p/tYJsvp39hn)
type Signature struct{
Name string
Signed bool
}
path := "Document.Signatures.1.Name"
map := map[string]interface{}{
"Document": map[string]interface{}{
"Signatures": []interface{}{
Signature{ Name: "Hugo", Signed: false },
Signature{ Name: "Walter", Signed: false },
},
"Otherstuff": "asadwa",
},
"AlsoOtherStuff": "adwaw",
}
// map.giveMe(path)
// even better (if possible:) map.change(path,"ToThisNewValue")
I have searched for solutions, but I can't find any on the internet. Maybe one of you knows how to do this or knows a library to use for me.
Thank you so much in advance!
Quite a lot of reflect calls will be needed if there is no predefined struct.
That being said, you can do it by iterating through the map with type checking on every iteration and handling cases accordingly.
// Splitting the path into keys
keys := strings.Split(path, ".")
var value interface{} = map1
for _, key := range keys {
if value, err = Get(key, value); err != nil {
break
}
}
if err == nil {
fmt.Println("Value:", value)
} else {
fmt.Println("Error:", err)
}
func Get(key string, s interface{}) (v interface{}, err error) {
var (
i int64
ok bool
)
switch s.(type) {
case map[string]interface{}:
if v, ok = s.(map[string]interface{})[key]; !ok {
err = fmt.Errorf("Key not present. [Key:%s]", key)
}
case []interface{}:
if i, err = strconv.ParseInt(key, 10, 64); err == nil {
array := s.([]interface{})
if int(i) < len(array) {
v = array[i]
} else {
err = fmt.Errorf("Index out of bounds. [Index:%d] [Array:%v]", i, array)
}
}
case Signature:
r := reflect.ValueOf(s)
v = reflect.Indirect(r).FieldByName(key)
}
//fmt.Println("Value:", v, " Key:", key, "Error:", err)
return v, err
}
Playground code
For example if I have an interface{} value that originally a map[string]map[int64][]int64 or any other kind of map, how to get the key type of the map? or more precise, how to convert it to map[theKeyType]interface{}?
func Transverse(any interface{}) string {
res := ``
switch any.(type) {
case string:
return ``
case []byte:
return ``
case int, int64, int32:
return ``
case float32, float64:
return ``
case bool:
return ``
case map[int64]interface{}:
return ``
case map[string]interface{}:
return ``
case []interface{}:
return ``
default:
kind := reflect.TypeOf(any).Kind()
switch kind {
case reflect.Map:
// how to convert it to map[keyType]interface{} ?
}
return `` // handle other type
}
return ``
}
Getting the Key type is easy:
reflect.TypeOf(any).Key()
To make the entire conversion, you need to create a map value of type map[keyType]interface{} and then copy the values over. Below is a working example how this can be done:
package main
import (
"errors"
"fmt"
"reflect"
)
func InterfaceMap(i interface{}) (interface{}, error) {
// Get type
t := reflect.TypeOf(i)
switch t.Kind() {
case reflect.Map:
// Get the value of the provided map
v := reflect.ValueOf(i)
// The "only" way of making a reflect.Type with interface{}
it := reflect.TypeOf((*interface{})(nil)).Elem()
// Create the map of the specific type. Key type is t.Key(), and element type is it
m := reflect.MakeMap(reflect.MapOf(t.Key(), it))
// Copy values to new map
for _, mk := range v.MapKeys() {
m.SetMapIndex(mk, v.MapIndex(mk))
}
return m.Interface(), nil
}
return nil, errors.New("Unsupported type")
}
func main() {
foo := make(map[string]int)
foo["anisus"] = 42
bar, err := InterfaceMap(foo)
if err != nil {
panic(err)
}
fmt.Printf("%#v\n", bar.(map[string]interface{}))
}
Output:
map[string]interface {}{"anisus":42}
Playground: http://play.golang.org/p/tJTapGAs2b
How do I retrieve the []byte value of this interface?
package main
import (
"reflect"
)
func byteInterface() interface{} {
return []byte("foo")
}
func main() {
//var b []byte
i := byteInterface()
switch {
case reflect.TypeOf(i).Kind() == reflect.Slice && (reflect.TypeOf(i) == reflect.TypeOf([]byte(nil))):
default:
panic("should have bytes")
}
}
You can use a type assertion for this; no need to use the reflect package:
package main
func byteInterface() interface{} {
return []byte("foo")
}
func main() {
i := byteInterface()
if b, ok := i.([]byte); ok {
// use b as []byte
println(len(b))
} else {
panic("should have bytes")
}
}
I have Go program that has a function defined. I also have a map that should have a key for each function. How can I do that?
I have tried this, but this doesn't work.
func a(param string) {
}
m := map[string] func {
'a_func': a,
}
for key, value := range m {
if key == 'a_func' {
value(param)
}
}
Are you trying to do something like this? I've revised the example to use varying types and numbers of function parameters.
package main
import "fmt"
func f(p string) {
fmt.Println("function f parameter:", p)
}
func g(p string, q int) {
fmt.Println("function g parameters:", p, q)
}
func main() {
m := map[string]interface{}{
"f": f,
"g": g,
}
for k, v := range m {
switch k {
case "f":
v.(func(string))("astring")
case "g":
v.(func(string, int))("astring", 42)
}
}
}
m := map[string]func(string, string)
Works if you know the signature (and all the funcs have the same signature)
I think this is cleaner/safer than using interface{}
You can define a type if functions are same interface.
package main
import "log"
type fn func (string)
func foo(msg string) {
log.Printf("foo! Message is %s", msg)
}
func bar(msg string) {
log.Printf("bar! Message is %s", msg)
}
func main() {
m := map[string] fn {
"f": foo,
"b": bar,
}
log.Printf("map is %v", m)
m["f"]("Hello")
m["b"]("World")
}
#Seth Hoenig's answer helped me best, but I just wanted to add that Go accepts functions with defined return value as well:
package main
func main() {
m := map[string]func(string) string{
"foo": func(s string) string { return s + "nurf" },
}
m["foo"]("baz") // "baznurf"
}
If you think it's ugly, you could always use a type (see #smagch's answer).
I used a map[string]func (a type, b *type) I passed a string to search the map and a pointer to modify the slice.
Hope that helps!
var Exceptions map[string]func(step string, item *structs.Item)
func SetExceptions() {
Exceptions = map[string]func(a string, i *structs.Item){
"step1": step1,
}
}
func RunExceptions(state string, item *structs.Item) {
method, methBool := Exceptions[state]
if methBool {
method(state, item)
}
}
func step1(step string, item *structs.Item) {
item.Title = "Modified"
}
Here is the way I made it work in my case:
package main
import (
"fmt"
)
var routes map[string]func() string
func main() {
routes = map[string]func() string{
"GET /": homePage,
"GET /about": aboutPage,
}
fmt.Println("GET /", pageContent("GET /"))
fmt.Println("GET /about", pageContent("GET /about"))
fmt.Println("GET /unknown", pageContent("GET /unknown"))
// Output:
// GET / Home page
// GET /about About page
// GET /unknown 404: Page Not Found
}
func pageContent(route string) string {
page, ok := routes[route]
if ok {
return page()
} else {
return notFoundPage()
}
}
func homePage() string {
return "Home page"
}
func aboutPage() string {
return "About page"
}
func notFoundPage() string {
return "404: Page Not Found"
}
https://play.golang.org/p/8_g6Di1OKZS
Hope this works for you(you can use interface{} instead any)
package main
import (
"fmt"
)
func toon(v any) {
fmt.Println(v)
}
func main() {
names := map[string]any{
"Function": toon,
}
names["Function"].(func(any))("a")
}