parseNestedDicts#224
Open
RagingRedRiot wants to merge 1 commit into
Open
Conversation
Contributor
Author
|
I have tested the code change to ensure it does not break shipping logs. I used the following unit test to ensure that the JSON parsing logic behaves as expected. package usp_o365
import (
"encoding/json"
"testing"
"github.com/refractionPOINT/usp-adapters/utils"
)
func TestMain(t *testing.T) {
// Test data that represents what we want to parse
var testData = `{
"String": "Not nested string value",
"Integer": 3,
"Data": "{\"nestedData\":\"This is my nested value\"}",
"Data2": "{\"nestedData2\":\"{\\\"Data3\\\":\\\"This is my double nested value\\\"}\"}"
}`
var dict utils.Dict // Represents the current JSON output
if err := json.Unmarshal([]byte(testData), &dict); err != nil {
t.Fatal("failed to unmarshal test data: %w", err)
}
updatedDict := parseNestedDicts(dict) // Represents the updated JSON output
// dict doesn't parse nested stringified JSON
// String - Expected to be parsed
_, ok := dict["String"].(string)
if !ok {
t.Fatalf("String is missing or is not a string; got: %#v", dict["String"])
}
// Integer - Expected to be parsed
rawInt, ok := dict["Integer"].(uint64)
if !ok {
t.Fatalf("Integer is missing or is not a uint64; got: %#v", dict["Integer"])
}
if rawInt != 3 {
t.Fatalf("Integer has wrong value; want 3, got %d", rawInt)
}
// Data - Does not parse beyond a string
_, ok = dict["Data"].(string)
if !ok {
t.Fatalf("Data is missing or is not a string; got: %#v", dict["Data"])
}
// Data2 - Does not parse beyond a string
_, ok = dict["Data2"].(string)
if !ok {
t.Fatalf("Data2 is missing or is not a string; got: %#v", dict["Data2"])
}
// updatedDict parses nested stringified JSON
// String - Same as dict
_, ok = updatedDict["String"].(string)
if !ok {
t.Fatalf("String is missing or is not a string; got: %#v", updatedDict["String"])
}
// Integer - Same as dict
rawInt, ok = updatedDict["Integer"].(uint64)
if !ok {
t.Fatalf("Integer is missing or is not a uint64; got: %#v", updatedDict["Integer"])
}
if rawInt != 3 {
t.Fatalf("Integer has wrong value; want 3, got %d", rawInt)
}
// Data → nestedData
dataDictUpdated, ok := updatedDict["Data"].(utils.Dict)
if !ok {
t.Fatalf("Data is missing or is not a utils.Dict; got: %#v", updatedDict["Data"])
}
nested, ok := dataDictUpdated["nestedData"].(string)
if !ok {
t.Fatalf("nestedData is missing or not a string; got: %#v", dataDictUpdated["nestedData"])
}
if nested != "This is my nested value" {
t.Errorf("nestedData has wrong value; want %q, got %q", "This is my nested value", nested)
}
// Data2 → nestedData2 → Data3
data2DictUpdated, ok := updatedDict["Data2"].(utils.Dict)
if !ok {
t.Fatalf("Data2 is missing or is not a utils.Dict; got: %#v", updatedDict["Data2"])
}
nested2Dict, ok := data2DictUpdated["nestedData2"].(utils.Dict)
if !ok {
t.Fatalf("nestedData2 is missing or is not a utils.Dict; got: %#v", data2DictUpdated["nestedData2"])
}
deepVal, ok := nested2Dict["Data3"].(string)
if !ok {
t.Fatalf("Data3 is missing or is not a string; got: %#v", nested2Dict["Data3"])
}
if deepVal != "This is my double nested value" {
t.Errorf("Data3 has wrong value; want %q, got %q", "This is my double nested value", deepVal)
}
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description of the change
Type of change
Related issues
https://github.com/BHISSOC/tracking/issues/302