From 4745ab3f17b49236b92053c2b7dc749124e3c473 Mon Sep 17 00:00:00 2001 From: Juunini Date: Wed, 25 Oct 2023 19:40:17 +0900 Subject: [PATCH] receive jsonld is `[]byte` and `string` fix error --- json_ld_reader.go | 20 +++++++++++++++++++- json_ld_reader_test.go | 32 ++++++++++++++++---------------- 2 files changed, 35 insertions(+), 17 deletions(-) diff --git a/json_ld_reader.go b/json_ld_reader.go index 9b6916e..0431286 100644 --- a/json_ld_reader.go +++ b/json_ld_reader.go @@ -1,6 +1,9 @@ package jsonld_helper import ( + "encoding/json" + "reflect" + "github.com/piprate/json-gold/ld" ) @@ -29,7 +32,22 @@ type JsonLDReader interface { } func ParseJsonLD(value any, options *ld.JsonLdOptions) (JsonLDReader, error) { - expanded, err := ld.NewJsonLdProcessor().Expand(value, options) + origin := value + reflectType := reflect.TypeOf(value).String() + + if reflectType == "[]uint8" { + if err := json.Unmarshal(value.([]byte), &origin); err != nil { + return nil, err + } + } + + if reflectType == "string" { + if err := json.Unmarshal([]byte(value.(string)), &origin); err != nil { + return nil, err + } + } + + expanded, err := ld.NewJsonLdProcessor().Expand(origin, options) if err != nil { return nil, err diff --git a/json_ld_reader_test.go b/json_ld_reader_test.go index ecda083..763ddc5 100644 --- a/json_ld_reader_test.go +++ b/json_ld_reader_test.go @@ -188,26 +188,26 @@ func Test_READMEJsonLDTest(t *testing.T) { } var jsonld, _ = jsonld_helper.ParseJsonLD(givenJsonLD, nil) -var givenJsonLD = map[string]any{ - "@context": []any{ +var givenJsonLD = `{ + "@context": [ "activitystreams.json", - map[string]any{ - "schema": "http://schema.org#", + { + "schema": "http://schema.org#", "PropertyValue": "schema:PropertyValue", - "value": "schema:value", - }, - }, + "value": "schema:value" + } + ], "as:type": "Person", - "@id": "acct:juunini@snippet.cloudmt.co.kr", - "name": "지상 최강의 개발자 쥬니니", - "attachment": []map[string]any{ + "@id": "acct:juunini@snippet.cloudmt.co.kr", + "name": "지상 최강의 개발자 쥬니니", + "attachment": [ { - "type": "PropertyValue", - "name": "GitHub", - "value": "juunini", - }, - }, -} + "type": "PropertyValue", + "name": "GitHub", + "value": "juunini" + } + ] +}` var readmeJsonLD = map[string]any{ "@context": []any{ "activitystreams.json",