Home > database >  golang why is value overwritten while implementing hierarchy tree?
golang why is value overwritten while implementing hierarchy tree?

Time:12-28

package main

import (
    "encoding/json"
    "fmt"
)

type employee struct {
    Name      string      `json:"name"`
    Id        int         `json:"id,omitempty"`
    ManagerId int         `json:"managerid"`
    Reporters []employee `json:"reporters,omitempty"`
}

// type employees struct {
//  employees []employee `json:"employee"`
// }

type EmployeeList struct {
    employees map[int]employee
    root  employee
}

func NewEmployeeList() *EmployeeList {
    var el EmployeeList
    el.employees = make(map[int]employee)
    return &el
}


func (el *EmployeeList) CreateMap(employeesInfo []employee) {    
    for _ , emp := range employeesInfo {
        e := employee{
            Name : emp.Name,
            Id: emp.Id,
            ManagerId: emp.ManagerId,
        }
        el.employees[emp.Id] = e
        if(emp.ManagerId == 0){
            el.root = e
            fmt.Println("CreateMap",el.root)
        }
    }
    fmt.Println("CreateMap2",el.root,el.employees)
}

func (el *EmployeeList) getReportersById(empId int) []employee {     
    reporters := []employee{}
    for _ , employee := range el.employees {
        if(employee.ManagerId == empId){
            reporters = append(reporters, employee)
        }
    }
    return reporters
}

func (el *EmployeeList) maketree(e *employee) {  
    //e := root
    e.Reporters = el.getReportersById(e.Id)
    if(true){
        fmt.Println("maketree",e.Id,e.Name,e.Reporters)
    }
    // e.Reporters = reporters
    if(len(e.Reporters) == 0){
        return
    }
    for _ , reporterEmployee := range e.Reporters {
        el.maketree(&reporterEmployee);
    }
}

func (el EmployeeList) print(root employee, level int) {     
    for i:= 0; i<level;i   {
        fmt.Print("\t");
    }
    fmt.Println(root.Name);
    for _, reporter := range root.Reporters {
        el.print(reporter, level   1)
    }
}

func main() {
    //1. Read JSON File
    myJsonString := `[{    "name": "Rob",    "id": 7,    "managerid": 3 }, {    "name": "Rex",    "id": 6,    "managerid": 2 }, {    "name": "Jake",    "id": 5,    "managerid": 2 }, {    "name": "Paul",    "id": 4,    "managerid": 1 }, {    "name": "Oliver",    "id": 3,    "managerid": 1 }, {    "name": "John",    "id": 2,    "managerid": 1 }, {    "name": "Britney",    "id": 1,    "managerid": 0 }]`
    //2. Create class and sent file data
    emplist := NewEmployeeList()
    rawEmployeesInfo := []employee{}
    
    _ = json.Unmarshal([]byte(myJsonString),&rawEmployeesInfo);
    //fmt.Println(rawEmployeesInfo);
    emplist.CreateMap(rawEmployeesInfo);
    //fmt.Println(emplist.employees,emplist.root);
    fmt.Println("Main1",emplist.root)
    emplist.maketree(&emplist.root);
    //fmt.Println(emplist.root)
    fmt.Println("Main2",emplist.root)
    emplist.print(emplist.root,0)
}

I am trying to create a tree from a json to store employee list. The problem is that while creating the tree values are stored correctly but somehow they lose value in the main function when I want to print the hierarchy tree. Can someone please help me why the value is not stored? I have added logs also to check.

CodePudding user response:

Your CreateMap method:

func (el EmployeeList) CreateMap(employeesInfo []employee) {
    ...
}

should be:

func (el *EmployeeList) CreateMap(employeesInfo []employee) {
    //---^
    ....
}

so that el is a pointer. Otherwise you're operating on a copy of emplist when you say:

emplist.CreateMap(rawEmployeesInfo);

so you do a bunch of work to create your map and then throw it all away.

  • Related