Skip to content

Commit

Permalink
receive jsonld is []byte and string fix error
Browse files Browse the repository at this point in the history
  • Loading branch information
juunini committed Oct 25, 2023
1 parent 66941d2 commit 4745ab3
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 17 deletions.
20 changes: 19 additions & 1 deletion json_ld_reader.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package jsonld_helper

import (
"encoding/json"
"reflect"

"github.com/piprate/json-gold/ld"
)

Expand Down Expand Up @@ -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
Expand Down
32 changes: 16 additions & 16 deletions json_ld_reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:[email protected]",
"name": "지상 최강의 개발자 쥬니니",
"attachment": []map[string]any{
"@id": "acct:[email protected]",
"name": "지상 최강의 개발자 쥬니니",
"attachment": [
{
"type": "PropertyValue",
"name": "GitHub",
"value": "juunini",
},
},
}
"type": "PropertyValue",
"name": "GitHub",
"value": "juunini"
}
]
}`
var readmeJsonLD = map[string]any{
"@context": []any{
"activitystreams.json",
Expand Down

0 comments on commit 4745ab3

Please sign in to comment.