From ecd22017545f0e96cbbb87b740008f3da86d70da Mon Sep 17 00:00:00 2001 From: Reza Behzadan Date: Sun, 18 Feb 2024 15:04:40 +0330 Subject: [PATCH] Fix naming of functions for dict serialization --- internal/dict/dict_copy_test.go | 12 ++++++------ internal/dict/dict_serialization.go | 18 +++++++++--------- internal/dict/dict_serialization_test.go | 6 +++--- internal/dict/testutils_test.go | 9 +++++---- 4 files changed, 23 insertions(+), 22 deletions(-) diff --git a/internal/dict/dict_copy_test.go b/internal/dict/dict_copy_test.go index 7bef393..191b7da 100644 --- a/internal/dict/dict_copy_test.go +++ b/internal/dict/dict_copy_test.go @@ -6,9 +6,9 @@ import ( ) func TestCopy(t *testing.T) { - originalDict, err := loadYaml("test_data/dict2.yaml") + originalDict, err := LoadYaml("test_data/dict2.yaml") if err != nil { - t.Fatalf("loadYaml() returned an error: %v", err) + t.Fatalf("LoadYaml() returned an error: %v", err) } newDict1 := Dict{} @@ -31,9 +31,9 @@ func TestCopy(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 { - t.Fatalf("loadYaml() returned an error: %v", err) + t.Fatalf("LoadYaml() returned an error: %v", err) } newDict1 := Dict{} @@ -56,9 +56,9 @@ func TestCopyFrom(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 { - t.Fatalf("loadYaml() returned an error: %v", err) + t.Fatalf("LoadYaml() returned an error: %v", err) } newDict1 := Dict{} diff --git a/internal/dict/dict_serialization.go b/internal/dict/dict_serialization.go index bdc0370..a991fc1 100644 --- a/internal/dict/dict_serialization.go +++ b/internal/dict/dict_serialization.go @@ -7,7 +7,7 @@ import ( "gopkg.in/yaml.v3" ) -func loadYaml(fn string) (Dict, error) { +func LoadYaml(fn string) (Dict, error) { content, err := os.ReadFile(fn) if err != nil { 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 err := yaml.Unmarshal([]byte(yamlStr), &data) if err != nil { @@ -31,7 +31,7 @@ func loadYamlStr(yamlStr string) (Dict, error) { return data, nil } -func (d Dict) dumpYaml(fn string) error { +func (d Dict) DumpYaml(fn string) error { data, err := yaml.Marshal(d) if err != nil { return err @@ -48,7 +48,7 @@ func (d Dict) dumpYamlStr() (string, error) { } // loadJson loads the dictionary from a JSON file. -func loadJson(fn string) (Dict, error) { +func LoadJson(fn string) (Dict, error) { var data Dict content, err := os.ReadFile(fn) if err != nil { @@ -62,7 +62,7 @@ func loadJson(fn string) (Dict, error) { return data, nil } -func loadJsonStr(jsonStr string) (Dict, error) { +func LoadJsonStr(jsonStr string) (Dict, error) { var data Dict err := json.Unmarshal([]byte(jsonStr), &data) if err != nil { @@ -72,7 +72,7 @@ func loadJsonStr(jsonStr string) (Dict, error) { } // 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) if err != nil { 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. -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) if err != nil { return err @@ -90,7 +90,7 @@ func (d Dict) dumpJsonIndent(fn string, prefix, indent string) error { } // 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) if err != nil { return "", err @@ -99,7 +99,7 @@ func (d Dict) dumpJsonStr() (string, error) { } // 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) if err != nil { return "", err diff --git a/internal/dict/dict_serialization_test.go b/internal/dict/dict_serialization_test.go index d328ed7..84bffaf 100644 --- a/internal/dict/dict_serialization_test.go +++ b/internal/dict/dict_serialization_test.go @@ -31,7 +31,7 @@ key3: } // 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 { t.Fatalf("loadYaml failed: %v", err) } @@ -51,7 +51,7 @@ key3: // TestLoadYaml tests the loadYaml function. func TestLoadYaml(t *testing.T) { - dict, err := loadYaml("test_data/dict1.yaml") + dict, err := LoadYaml("test_data/dict1.yaml") if err != nil { t.Fatalf("loadYaml() returned an error: %v", err) } @@ -69,7 +69,7 @@ func TestLoadYaml(t *testing.T) { func TestDumpJsonStrIndent(t *testing.T) { d := Dict{"key": "value", "number": 1} - jsonStr, err := d.dumpJsonStrIndent("", " ") + jsonStr, err := d.DumpJsonStrIndent("", " ") if err != nil { t.Fatalf("dumpJsonStrIndent() returned an error: %v", err) } diff --git a/internal/dict/testutils_test.go b/internal/dict/testutils_test.go index 496fb5a..f9cabd3 100644 --- a/internal/dict/testutils_test.go +++ b/internal/dict/testutils_test.go @@ -14,10 +14,11 @@ type dictDictToDictTestCase struct { Result Dict `yaml:"result"` } -type dictStrToDictTestCase struct { +type dictStrToAnyTestCase struct { + Name string `yaml:"name"` Arg1 Dict `yaml:"arg1"` Arg2 string `yaml:"arg2"` - Result Dict `yaml:"result"` + Result any `yaml:"result"` } func loadYamlFile(fn string, out interface{}) { @@ -96,9 +97,9 @@ func sampleDict() Dict { } func TestSampleDict(t *testing.T) { - originalDict, err := loadYaml("test_data/dict2.yaml") + originalDict, err := LoadYaml("test_data/dict2.yaml") if err != nil { - t.Fatalf("loadYaml() returned an error: %v", err) + t.Fatalf("LoadYaml() returned an error: %v", err) } dict := sampleDict()