Skip to content

Commit 4916b02

Browse files
committed
chore: coverage json
1 parent 25003fb commit 4916b02

File tree

3 files changed

+89
-1
lines changed

3 files changed

+89
-1
lines changed

internal/define/define.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@ type HostConfig struct {
55
Notes string `yaml:"Notes,omitempty"`
66
Config map[string]string
77
}
8+
9+
type HostConfigForJSON map[string]string

internal/parser/json.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,16 @@ import (
55
Fn "github.com/soulteary/ssh-yaml/internal/fn"
66
)
77

8-
func ConvertToJSON(hostConfigs []Define.HostConfig) []byte {
8+
func ConvertToJSON(input []Define.HostConfig) []byte {
9+
hostConfigs := make([]Define.HostConfigForJSON, 0)
10+
for _, hostConfig := range input {
11+
config := Define.HostConfigForJSON{}
12+
13+
orderMaps := Fn.GetOrderMaps(hostConfig.Config)
14+
for _, field := range orderMaps.Keys {
15+
config[field] = orderMaps.Data[field]
16+
}
17+
hostConfigs = append(hostConfigs, config)
18+
}
919
return Fn.GetJSONBytes(hostConfigs)
1020
}

internal/parser/json_test.go

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package parser_test
2+
3+
import (
4+
"encoding/json"
5+
"reflect"
6+
"testing"
7+
8+
Define "github.com/soulteary/ssh-yaml/internal/define"
9+
Parser "github.com/soulteary/ssh-yaml/internal/parser"
10+
)
11+
12+
func TestConvertToJSON(t *testing.T) {
13+
testCases := []struct {
14+
name string
15+
input []Define.HostConfig
16+
expected []byte
17+
}{
18+
{
19+
name: "Empty slice",
20+
input: []Define.HostConfig{},
21+
expected: []byte("[]"),
22+
},
23+
{
24+
name: "Single host config",
25+
input: []Define.HostConfig{
26+
{
27+
Config: map[string]string{
28+
"Host": "example.com",
29+
"Port": "22",
30+
"Username": "user",
31+
"Password": "pass",
32+
},
33+
},
34+
},
35+
expected: []byte(`[{"Host":"example.com","Password":"pass","Port":"22","Username":"user"}]`),
36+
},
37+
{
38+
name: "Multiple host configs",
39+
input: []Define.HostConfig{
40+
{
41+
Config: map[string]string{
42+
"Host": "example1.com",
43+
"Port": "22",
44+
"Username": "user1",
45+
"Password": "pass1",
46+
},
47+
},
48+
{
49+
Config: map[string]string{
50+
"Host": "example2.com",
51+
"Port": "2222",
52+
"Username": "user2",
53+
"Password": "pass2",
54+
},
55+
},
56+
},
57+
expected: []byte(`[{"Host":"example1.com","Password":"pass1","Port":"22","Username":"user1"},{"Host":"example2.com","Password":"pass2","Port":"2222","Username":"user2"}]`),
58+
},
59+
}
60+
61+
for _, tc := range testCases {
62+
t.Run(tc.name, func(t *testing.T) {
63+
result := Parser.ConvertToJSON(tc.input)
64+
65+
var js interface{}
66+
err := json.Unmarshal(result, &js)
67+
if err != nil {
68+
t.Errorf("ConvertToJSON produced invalid JSON: %v", err)
69+
}
70+
71+
if !reflect.DeepEqual(result, tc.expected) {
72+
t.Errorf("ConvertToJSON() = %s, want %s", result, tc.expected)
73+
}
74+
})
75+
}
76+
}

0 commit comments

Comments
 (0)