Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

7 allow to take and return mapstringany #8

Merged
merged 2 commits into from
Feb 28, 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
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

## Candidate functions :

| Func Name | Prototype | Description | Comments |
|:---------:|:--------------------------------------------------------------------------------:|:------------------------------------------:|:--------:|
| Marshal | Marshal(content map\[string]string \| []map\[string]string) (string, error) {} | Returns the JSON encoding of a map | N/A |
| Unmarshal | Unmarshal(content string) (map\[string]string \| []map\[string]string, error) {} | Returns the map representation of the JSON | N/A |
| Func Name | Prototype | Description | Comments |
|:---------:|:--------------------------------------------------------------------------:|:------------------------------------------:|:--------:|
| Marshal | Marshal(content map\[string]any \| []map\[string]any) (string, error) {} | Returns the JSON encoding of a map | N/A |
| Unmarshal | Unmarshal(content string) (map\[string]any \| []map\[string]any, error) {} | Returns the map representation of the JSON | N/A |
6 changes: 3 additions & 3 deletions json.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
)

// Marshal returns the JSON encoding of a map
func Marshal[JsonType map[string]string | []map[string]string](content JsonType) (string, error) {
func Marshal[JsonType map[string]any | []map[string]any](content JsonType) (string, error) {
contentByte, err := json.Marshal(content)
if err != nil {
return "", err
Expand All @@ -16,14 +16,14 @@ func Marshal[JsonType map[string]string | []map[string]string](content JsonType)
// Unmarshal returns the map representation of the JSON
func Unmarshal(content string) (any, error) {
if content[0] == '[' {
var v []map[string]string
var v []map[string]any
err := json.Unmarshal([]byte(content), &v)
if err != nil {
return v, err
}
return v, nil
}
var v map[string]string
var v map[string]any
err := json.Unmarshal([]byte(content), &v)
if err != nil {
return v, err
Expand Down
28 changes: 24 additions & 4 deletions json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@ package json

import (
"encoding/json"
"fmt"
"io"
"math"
"os"
"reflect"
"testing"
)

func TestMarshal(t *testing.T) {
// Marshal map
maps := map[string]string{}
maps := map[string]any{}
file, err := os.Open("unit_test_files/test.json")
if err != nil {
t.Errorf("Error: %v", err)
Expand All @@ -32,7 +34,7 @@ func TestMarshal(t *testing.T) {
}

// Marshal map array
var maps2 []map[string]string
var maps2 []map[string]any
file2, err := os.Open("unit_test_files/test2.json")
if err != nil {
t.Errorf("Error: %v", err)
Expand All @@ -55,11 +57,19 @@ func TestMarshal(t *testing.T) {
if string(expected) != actual {
t.Errorf("Expected %v, got %v", expected, actual)
}

// Marshal crash test
invalidMap := map[string]any{"invalid": math.Inf(-1)}
res, err := Marshal(invalidMap)
fmt.Println(res)
if err == nil {
t.Errorf("Expected error, got %v", err)
}
}

func TestUnmarshal(t *testing.T) {
// Unmarshal Json file
var expected map[string]string
var expected map[string]any
file, _ := os.Open("unit_test_files/test.json")
defer file.Close()
fileContent, _ := io.ReadAll(file)
Expand All @@ -73,7 +83,7 @@ func TestUnmarshal(t *testing.T) {
}

// Unmarshal Json Array File
var expect []map[string]string
var expect []map[string]any
file2, _ := os.Open("unit_test_files/test2.json")
defer file2.Close()
fileContent2, _ := io.ReadAll(file2)
Expand All @@ -85,4 +95,14 @@ func TestUnmarshal(t *testing.T) {
if !reflect.DeepEqual(expect, actual) {
t.Errorf("Expected %v, got %v", expect, actual)
}

// Unmarshal crash test
_, err = Unmarshal("invalid json")
if err == nil {
t.Errorf("Expected error, got nil")
}
_, err = Unmarshal("[invalid json")
if err == nil {
t.Errorf("Expected error, got nil")
}
}
5 changes: 3 additions & 2 deletions unit_test_files/test.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"name": "Ecla",
"members": "7",
"language": "Golang"
"members": 7,
"language": "Golang",
"check": true
}
10 changes: 6 additions & 4 deletions unit_test_files/test2.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
[
{
"name": "Ecla",
"members": "7",
"language": "Golang"
"members": 7,
"language": "Golang",
"check": true
},
{
"name": "Test",
"members": "12",
"language": "Python"
"members": 12,
"language": "Python",
"check": true
}
]
Loading