Skip to content
This repository has been archived by the owner on Sep 24, 2024. It is now read-only.

Feature: YAML to JSON #8

Merged
merged 4 commits into from
Jan 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,12 @@ List of features:

- [X] JSON to Yaml

- [X] Yaml to JSON

**Next**

- [ ] JWT encoding

- [ ] Yaml to JSON

- [ ] URL encoding

- [ ] URL decoding
Expand Down
3 changes: 2 additions & 1 deletion cmd/base64decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ package cmd
import (
"fmt"

internal "github.com/ahelmy/xdev/internal"
"github.com/spf13/cobra"

"github.com/ahelmy/xdev/internal"
)

// base64encoderCmd represents the base64encoder command
Expand Down
3 changes: 2 additions & 1 deletion cmd/base64encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ package cmd
import (
"fmt"

internal "github.com/ahelmy/xdev/internal"
"github.com/spf13/cobra"

"github.com/ahelmy/xdev/internal"
)

// base64encoderCmd represents the base64encoder command
Expand Down
3 changes: 2 additions & 1 deletion cmd/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ package cmd
import (
"fmt"

internal "github.com/ahelmy/xdev/internal"
"github.com/spf13/cobra"

"github.com/ahelmy/xdev/internal"
)

// jsonCmd represents the json command
Expand Down
9 changes: 5 additions & 4 deletions cmd/json2yaml.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
/*
Copyright © 2023 NAME HERE <EMAIL ADDRESS>

*/
package cmd

import (
"fmt"
internal "github.com/ahelmy/xdev/internal"

"github.com/spf13/cobra"

"github.com/ahelmy/xdev/internal"
)

// json2yamlCmd represents the json2yaml command
var json2yamlCmd = &cobra.Command{
Use: "json2yaml",
Use: "json2yaml",
Aliases: []string{"j2y"},
Short: "Convert JSON to YAML. Alias: j2y",
Short: "Convert JSON to YAML. Alias: j2y",
Long: `Convert JSON to YAML. For example:

j2y '{"name":"ahmed","age":30}'
Expand Down
3 changes: 2 additions & 1 deletion cmd/jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ package cmd
import (
"fmt"

internal "github.com/ahelmy/xdev/internal"
"github.com/spf13/cobra"

"github.com/ahelmy/xdev/internal"
)

// jwtdecodeCmd represents the jwtdecode command
Expand Down
3 changes: 2 additions & 1 deletion cmd/pasword.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import (
"fmt"
"strconv"

internal "github.com/ahelmy/xdev/internal"
"github.com/spf13/cobra"

"github.com/ahelmy/xdev/internal"
)

// paswordCmd represents the pasword command
Expand Down
3 changes: 2 additions & 1 deletion cmd/uuid.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ package cmd
import (
"fmt"

internal "github.com/ahelmy/xdev/internal"
"github.com/spf13/cobra"

"github.com/ahelmy/xdev/internal"
)

// guidCmd represents the guid command
Expand Down
48 changes: 48 additions & 0 deletions cmd/yaml2json.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package cmd

import (
"fmt"

"github.com/spf13/cobra"

"github.com/ahelmy/xdev/internal"
)

// yaml2jsonCmd represents the yaml2json command
var yaml2jsonCmd = &cobra.Command{
Use: "yaml2json",
Aliases: []string{"y2j"},
Short: "Convert YAML to JSON. Alias: y2j",
Long: `Convert YAML to JSON. For example:

y2j 'name: ahmed
age: 30'
Output:
{"name":"ahmed","age":30}`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) < 1 {
fmt.Println("Please provide a YAML string.")
return
}
jsonString, err := internal.Yaml2Json(args[0])
if err != nil {
fmt.Println(err)
return
}
fmt.Println(jsonString)
},
}

func init() {
rootCmd.AddCommand(yaml2jsonCmd)

// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// json2yamlCmd.PersistentFlags().String("foo", "", "A help for foo")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// json2yamlCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
19 changes: 11 additions & 8 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@ module github.com/ahelmy/xdev
go 1.20

require (
github.com/boumenot/gocover-cobertura v1.2.0 // indirect
github.com/golang-jwt/jwt v3.2.2+incompatible // indirect
github.com/golang-jwt/jwt/v5 v5.2.0 // indirect
github.com/google/uuid v1.5.0 // indirect
github.com/golang-jwt/jwt/v5 v5.2.0
github.com/google/uuid v1.5.0
github.com/itchyny/json2yaml v0.1.4
github.com/spf13/cobra v1.8.0
github.com/stretchr/testify v1.8.4
gopkg.in/yaml.v3 v3.0.1
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/itchyny/json2yaml v0.1.4 // indirect
github.com/spf13/cobra v1.8.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/tools v0.0.0-20200526224456-8b020aee10d2 // indirect
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 // indirect
)
34 changes: 7 additions & 27 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
github.com/boumenot/gocover-cobertura v1.2.0 h1:g+VROIASoEHBrEilIyaCmgo7HGm+AV5yKEPLk0qIY+s=
github.com/boumenot/gocover-cobertura v1.2.0/go.mod h1:fz7ly8dslE42VRR5ZWLt2OHGDHjkTiA2oNvKgJEjLT0=
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/golang-jwt/jwt v3.2.2+incompatible h1:IfV12K8xAKAnZqdXVzCZ+TOjboZ2keLg81eXfW3O+oY=
github.com/golang-jwt/jwt v3.2.2+incompatible/go.mod h1:8pz2t5EyA70fFQQSrl6XZXzqecmYZeUEB8OUGHkxJ+I=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/golang-jwt/jwt/v5 v5.2.0 h1:d/ix8ftRUorsN+5eMIlF4T6J8CAt9rch3My2winC1Jw=
github.com/golang-jwt/jwt/v5 v5.2.0/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk=
github.com/google/uuid v1.5.0 h1:1p67kYwdtXjb0gL0BPiP1Av9wiZPo5A8z2cWkTZ+eyU=
Expand All @@ -12,33 +9,16 @@ github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/itchyny/json2yaml v0.1.4 h1:/pErVOXGG5iTyXHi/QKR4y3uzhLjGTEmmJIy97YT+k8=
github.com/itchyny/json2yaml v0.1.4/go.mod h1:6iudhBZdarpjLFRNj+clWLAkGft+9uCcjAZYXUH9eGI=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0=
github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20200526224456-8b020aee10d2 h1:21BqcH/onxtGHn1A2GDOJjZnbt4Nlez629S3eaR+eYs=
golang.org/x/tools v0.0.0-20200526224456-8b020aee10d2/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
22 changes: 22 additions & 0 deletions internal/yaml.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package internal

import (
"encoding/json"

"gopkg.in/yaml.v3"
)

func Yaml2Json(yamlString string) (string, error) {
t := make(map[string]any)
err := yaml.Unmarshal([]byte(yamlString), &t)
if err != nil {
return "{}", err
}

output, err := json.Marshal(t)
if err != nil {
return "{}", err
}

return string(output), nil
}
43 changes: 43 additions & 0 deletions internal/yaml_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package internal

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestYaml2JSON(t *testing.T) {
tests := []struct {
name string
yamlString string
expectedJSON string
expectError bool
}{
{
name: "Valid YAML",
yamlString: "key: value\nnested:\n subkey: subvalue",
expectedJSON: `{"key":"value","nested":{"subkey":"subvalue"}}`,
expectError: false,
},
{
name: "Invalid YAML",
yamlString: "invalid: yaml: string",
expectedJSON: "{}",
expectError: true,
},
// Add more test cases as needed
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result, err := Yaml2Json(tt.yamlString)

if (err != nil) != tt.expectError {
t.Errorf("Unexpected error status. Got error: %v, expected error: %v", err, tt.expectError)
}

// Use assert.JSONEq to compare JSON strings while ignoring key order
assert.JSONEq(t, tt.expectedJSON, result)
})
}
}
Loading