#Disappearing nested objects

26 messages · Page 1 of 1 (latest)

cyan kestrel
#
package main

import (
    "fmt"
)

type entity struct {
    id       int
    parentId *int
    children []entity
}

type response struct {
    data []entity
}

func main() {
    resp := response{}

    one := 1
    two := 2
    entities := map[int]entity{
        1: {id: 1, parentId: nil},
        2: {id: 2, parentId: &one},
        3: {id: 3, parentId: &two},
    }

    for _, entity := range entities {
        if entity.parentId != nil {
            fmt.Println("adding", *entity.parentId, "to", entity.id)
            parent := entities[*entity.parentId]
            parent.children = append(parent.children, entity)
            entities[*entity.parentId] = parent
        }
    }

    fmt.Println("entities[1].children len", len(entities[1].children)) // 1
    fmt.Println("entities[2].children len", len(entities[2].children)) // 1
    fmt.Println("entities[3].children len", len(entities[3].children)) // 0

    resp.data = append(resp.data, entities[1])

    fmt.Println("resp.data[0].id", resp.data[0].id)                                              // 1
    fmt.Println("resp.data[0].children len", len(resp.data[0].children))                         // 1
    fmt.Println("resp.data[0].children[0].id", resp.data[0].children[0].id)                      // 2
    fmt.Println("resp.data[0].children[0].children len", len(resp.data[0].children[0].children)) // expected 1, got 0, sometimes 1
}

the only difference between valid and invalid runs is the order in which entity parents were assigned, wat do

molten coyote
#

I'm not certain that I understood the question (what's the question?) but note that map iteration order is random, it can be 1, 2, 3 or 3, 1, 2, any combination is possible

cyan kestrel
#

the last line is essentially what puzzles me

#

i was writing an endpoint earlier and ran into this, noticed that sometimes i was getting an object in children, sometimes not

#

this is essentially a reproduction case of this

#

80-90% it's going to poof

#

and, uh, it should be there 👀

molten coyote
#

let's think about it another away

#

should this work? go entities := map[int]entity{ 3: {id: 3, parentId: &two}, 2: {id: 2, parentId: &one}, 1: {id: 1, parentId: nil}, }

grim pivot
#

this is the data that gets dumped

map[int]main.entity{
  1: main.entity{
    id: 1,
    children: []main.entity{
      {
        id: 2,
        parentId: &1,
      },
    },
  },
  2: main.entity{
    id: 2,
    parentId: &1,
    children: []main.entity{
      {
        id: 3,
        parentId: &2,
      },
    },
  },
  3: main.entity{
    id: 3,
    parentId: &2,
  },
}
#

note the structure when a for i=len; i-- loop get used

map[int]main.entity{
  1: main.entity{
    id: 1,
    children: []main.entity{
      {
        id: 2,
        parentId: &1,
        children: []main.entity{
          {
            id: 3,
            parentId: &2,
          },
        },
      },
    },
  },
  2: main.entity{
    id: 2,
    parentId: &1,
    children: []main.entity{
      {
        id: 3,
        parentId: &2,
      },
    },
  },
  3: main.entity{
    id: 3,
    parentId: &2,
  },
}
grim pivot
cyan kestrel
grim pivot
cyan kestrel
#

oic

#

it's getting lost in the first object, ugh

grim pivot
#

when you dont append it in order things get lost basically

cyan kestrel
#

welp, back to the drawing board

#

ty for pointing out my dumb dumb =P

grim pivot
#

what about just have *entity? have

parent *entity
children []*entity
```so order isnt as important
cyan kestrel
#

output struct might screw me over here, but thats what i essentially was expecting and should be using

#

oh yeah, seems to be working in the isolated case, change the map to hold references, and the output struct to hold references to children

#

which i should be able to port over to actual project

still ember
#

output order is sorted when printing, but is in fact random when ranging over them as pointed out by Thunder

cyan kestrel
#

well, note to future self, be careful with copies of nested objects ,_,

#

thank you again ❤️