wk8 / go-ordered-map

Optimal implementation of ordered maps for Golang - ie maps that remember the order in which keys were inserted.
Apache License 2.0
523 stars 40 forks source link

Yaml deserialization - map ordering not working #34

Closed rr-jino-jose closed 1 year ago

rr-jino-jose commented 1 year ago

hello

I am trying to get my map ordered and I don't see its giving me correct results. Can anyone give me a hand?? The below is my main file

package main

import (
    "fmt"
    "io/ioutil"

    orderedmap "github.com/wk8/go-ordered-map/v2"
    "gopkg.in/yaml.v3"
)

func main() {
    filename := "./file.yaml"
    err := updateYAMLFile(filename, "env")
    if err != nil {
        fmt.Println(err)
    }
}

func updateYAMLFile(filename string, userKey string) error {
    // Read the existing YAML file
    content, err := ioutil.ReadFile(filename)
    if err != nil {
        return err
    }

    // Parse the YAML content
    //var data map[string]interface{}
    data := orderedmap.New[string, any]()
    if err := yaml.Unmarshal(content, &data); err != nil {
        return err
    }
    // If the user provided key exists
    fmt.Println(data)
    //yamlBytes, _ := yaml.Marshal(&data)
    //fmt.Println(string(yamlBytes))

    return err

}

I have a yaml file like this

name: CI

on:
  push:
    branches:
      - master
      - "feature/**"
      - "hotfix/**"
      - "chore/**"

  pull_request:
    branches:
      - master
    types: [opened, reopened]
    paths-ignore:
      - '*.md'
      - 'ci/**'
      - 'ops/**'
      - '.gitignore'

permissions:
  checks: write
  contents: read
  statuses: write

# ensures only a single CI build of the branch will run at a time
# see https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions#concurrency
concurrency: 
  group: "ci-${{ github.ref }}"
  cancel-in-progress: true

When I run the main file its giving me wrong order.. wonder what am I missing

go run main.go
&{map[concurrency:0xc000074ed0 jobs:0xc000075530 name:0xc000074ba0 on:0xc000074d50 permissions:0xc000074e10] 0xc0000749f0}

I was expecting it to be "name,on,permissions,concurrency" format

wk8 commented 1 year ago

Can't repro. And confused as to why your print would show a &{map ... when your code is supposed to be printing an ordered map. You sure you ran that exact snippet?

wk8 commented 1 year ago

Closing, please submit a go playground example if you'd like to reopen. Thanks!