Created
June 22, 2021 00:22
-
-
Save JamesAtIntegratnIO/845e908162a7798432cb0963c1c3c386 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "encoding/json" | |
| "fmt" | |
| "io/ioutil" | |
| "log" | |
| "strconv" | |
| ) | |
| type Migration struct { | |
| Id int64 | |
| Migration string | |
| Created string | |
| } | |
| type Migrations struct { | |
| Migrations []Migration | |
| } | |
| type DTO struct { | |
| Data [][]string `json:"rows"` | |
| } | |
| func read_json() []byte { | |
| // read file | |
| data, err := ioutil.ReadFile("./data.json") | |
| if err != nil { | |
| fmt.Print(err) | |
| } | |
| return data | |
| } | |
| func (d DTO) UnmarshalJson(b []byte) error { | |
| if err := json.Unmarshal(b, &d); err != nil { | |
| return err | |
| } | |
| return nil | |
| } | |
| func (m *Migrations) populateMigrations(dto DTO) { | |
| var err error | |
| var migration Migration | |
| for i := 0; i < len(dto.Data); i++ { | |
| migration.Id, err = strconv.ParseInt(dto.Data[i][0], 10, 64) | |
| if err != nil { | |
| log.Fatal(err) | |
| } | |
| migration.Migration = dto.Data[i][1] | |
| migration.Created = dto.Data[i][2] | |
| m.Migrations = append(m.Migrations, migration) | |
| } | |
| } | |
| func main() { | |
| var dto DTO | |
| if err := json.Unmarshal(read_json(), &dto); err != nil { | |
| log.Fatal(err) | |
| } | |
| var m Migrations | |
| m.populateMigrations(dto) | |
| for _, v := range m.Migrations { | |
| fmt.Println(v.Id) | |
| fmt.Println(v.Migration) | |
| fmt.Println(v.Created) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment