Fix naming of functions for dict serialization

This commit is contained in:
Reza Behzadan 2024-02-18 15:04:40 +03:30
parent d3e56f2eca
commit ecd2201754
4 changed files with 23 additions and 22 deletions

View File

@ -6,9 +6,9 @@ import (
) )
func TestCopy(t *testing.T) { func TestCopy(t *testing.T) {
originalDict, err := loadYaml("test_data/dict2.yaml") originalDict, err := LoadYaml("test_data/dict2.yaml")
if err != nil { if err != nil {
t.Fatalf("loadYaml() returned an error: %v", err) t.Fatalf("LoadYaml() returned an error: %v", err)
} }
newDict1 := Dict{} newDict1 := Dict{}
@ -31,9 +31,9 @@ func TestCopy(t *testing.T) {
} }
func TestCopyFrom(t *testing.T) { func TestCopyFrom(t *testing.T) {
originalDict, err := loadYaml("test_data/dict2.yaml") originalDict, err := LoadYaml("test_data/dict2.yaml")
if err != nil { if err != nil {
t.Fatalf("loadYaml() returned an error: %v", err) t.Fatalf("LoadYaml() returned an error: %v", err)
} }
newDict1 := Dict{} newDict1 := Dict{}
@ -56,9 +56,9 @@ func TestCopyFrom(t *testing.T) {
} }
func TestCopyTo(t *testing.T) { func TestCopyTo(t *testing.T) {
originalDict, err := loadYaml("test_data/dict2.yaml") originalDict, err := LoadYaml("test_data/dict2.yaml")
if err != nil { if err != nil {
t.Fatalf("loadYaml() returned an error: %v", err) t.Fatalf("LoadYaml() returned an error: %v", err)
} }
newDict1 := Dict{} newDict1 := Dict{}

View File

@ -7,7 +7,7 @@ import (
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
) )
func loadYaml(fn string) (Dict, error) { func LoadYaml(fn string) (Dict, error) {
content, err := os.ReadFile(fn) content, err := os.ReadFile(fn)
if err != nil { if err != nil {
return nil, err return nil, err
@ -22,7 +22,7 @@ func loadYaml(fn string) (Dict, error) {
} }
func loadYamlStr(yamlStr string) (Dict, error) { func LoadYamlStr(yamlStr string) (Dict, error) {
var data Dict var data Dict
err := yaml.Unmarshal([]byte(yamlStr), &data) err := yaml.Unmarshal([]byte(yamlStr), &data)
if err != nil { if err != nil {
@ -31,7 +31,7 @@ func loadYamlStr(yamlStr string) (Dict, error) {
return data, nil return data, nil
} }
func (d Dict) dumpYaml(fn string) error { func (d Dict) DumpYaml(fn string) error {
data, err := yaml.Marshal(d) data, err := yaml.Marshal(d)
if err != nil { if err != nil {
return err return err
@ -48,7 +48,7 @@ func (d Dict) dumpYamlStr() (string, error) {
} }
// loadJson loads the dictionary from a JSON file. // loadJson loads the dictionary from a JSON file.
func loadJson(fn string) (Dict, error) { func LoadJson(fn string) (Dict, error) {
var data Dict var data Dict
content, err := os.ReadFile(fn) content, err := os.ReadFile(fn)
if err != nil { if err != nil {
@ -62,7 +62,7 @@ func loadJson(fn string) (Dict, error) {
return data, nil return data, nil
} }
func loadJsonStr(jsonStr string) (Dict, error) { func LoadJsonStr(jsonStr string) (Dict, error) {
var data Dict var data Dict
err := json.Unmarshal([]byte(jsonStr), &data) err := json.Unmarshal([]byte(jsonStr), &data)
if err != nil { if err != nil {
@ -72,7 +72,7 @@ func loadJsonStr(jsonStr string) (Dict, error) {
} }
// dumpJson writes the dictionary to a JSON file. // dumpJson writes the dictionary to a JSON file.
func (d Dict) dumpJson(fn string) error { func (d Dict) DumpJson(fn string) error {
data, err := json.Marshal(d) data, err := json.Marshal(d)
if err != nil { if err != nil {
return err return err
@ -81,7 +81,7 @@ func (d Dict) dumpJson(fn string) error {
} }
// dumpJsonIndent writes the dictionary to a JSON file but applies indent first. // dumpJsonIndent writes the dictionary to a JSON file but applies indent first.
func (d Dict) dumpJsonIndent(fn string, prefix, indent string) error { func (d Dict) DumpJsonIndent(fn string, prefix, indent string) error {
data, err := json.MarshalIndent(d, prefix, indent) data, err := json.MarshalIndent(d, prefix, indent)
if err != nil { if err != nil {
return err return err
@ -90,7 +90,7 @@ func (d Dict) dumpJsonIndent(fn string, prefix, indent string) error {
} }
// dumpJsonStr returns the dictionary as a JSON string. // dumpJsonStr returns the dictionary as a JSON string.
func (d Dict) dumpJsonStr() (string, error) { func (d Dict) DumpJsonStr() (string, error) {
data, err := json.Marshal(d) data, err := json.Marshal(d)
if err != nil { if err != nil {
return "", err return "", err
@ -99,7 +99,7 @@ func (d Dict) dumpJsonStr() (string, error) {
} }
// dumpJsonStr returns the dictionary as a JSON string. // dumpJsonStr returns the dictionary as a JSON string.
func (d Dict) dumpJsonStrIndent(prefix, indent string) (string, error) { func (d Dict) DumpJsonStrIndent(prefix, indent string) (string, error) {
data, err := json.MarshalIndent(d, prefix, indent) data, err := json.MarshalIndent(d, prefix, indent)
if err != nil { if err != nil {
return "", err return "", err

View File

@ -31,7 +31,7 @@ key3:
} }
// Step 3: Use the loadYaml function to load this file into a Dict. // Step 3: Use the loadYaml function to load this file into a Dict.
dict, err := loadYaml(tmpFile.Name()) dict, err := LoadYaml(tmpFile.Name())
if err != nil { if err != nil {
t.Fatalf("loadYaml failed: %v", err) t.Fatalf("loadYaml failed: %v", err)
} }
@ -51,7 +51,7 @@ key3:
// TestLoadYaml tests the loadYaml function. // TestLoadYaml tests the loadYaml function.
func TestLoadYaml(t *testing.T) { func TestLoadYaml(t *testing.T) {
dict, err := loadYaml("test_data/dict1.yaml") dict, err := LoadYaml("test_data/dict1.yaml")
if err != nil { if err != nil {
t.Fatalf("loadYaml() returned an error: %v", err) t.Fatalf("loadYaml() returned an error: %v", err)
} }
@ -69,7 +69,7 @@ func TestLoadYaml(t *testing.T) {
func TestDumpJsonStrIndent(t *testing.T) { func TestDumpJsonStrIndent(t *testing.T) {
d := Dict{"key": "value", "number": 1} d := Dict{"key": "value", "number": 1}
jsonStr, err := d.dumpJsonStrIndent("", " ") jsonStr, err := d.DumpJsonStrIndent("", " ")
if err != nil { if err != nil {
t.Fatalf("dumpJsonStrIndent() returned an error: %v", err) t.Fatalf("dumpJsonStrIndent() returned an error: %v", err)
} }

View File

@ -14,10 +14,11 @@ type dictDictToDictTestCase struct {
Result Dict `yaml:"result"` Result Dict `yaml:"result"`
} }
type dictStrToDictTestCase struct { type dictStrToAnyTestCase struct {
Name string `yaml:"name"`
Arg1 Dict `yaml:"arg1"` Arg1 Dict `yaml:"arg1"`
Arg2 string `yaml:"arg2"` Arg2 string `yaml:"arg2"`
Result Dict `yaml:"result"` Result any `yaml:"result"`
} }
func loadYamlFile(fn string, out interface{}) { func loadYamlFile(fn string, out interface{}) {
@ -96,9 +97,9 @@ func sampleDict() Dict {
} }
func TestSampleDict(t *testing.T) { func TestSampleDict(t *testing.T) {
originalDict, err := loadYaml("test_data/dict2.yaml") originalDict, err := LoadYaml("test_data/dict2.yaml")
if err != nil { if err != nil {
t.Fatalf("loadYaml() returned an error: %v", err) t.Fatalf("LoadYaml() returned an error: %v", err)
} }
dict := sampleDict() dict := sampleDict()