-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjson_ld_reader.go
57 lines (42 loc) · 1.04 KB
/
json_ld_reader.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package jsonld_helper
import (
"encoding/json"
"reflect"
"github.com/piprate/json-gold/ld"
)
type JsonLDReader interface {
Value() any
Length() int
ReadKey(string) JsonLDReader
ReadIndex(int) JsonLDReader
Get() any
GetOrElse(any) any
GetOrThrow(error) (any, error)
StringOrElse(string) string
StringOrThrow(error) (string, error)
BoolOrElse(bool) bool
BoolOrThrow(error) (bool, error)
IntOrElse(int) int
IntOrThrow(error) (int, error)
FloatOrElse(float64) float64
FloatOrThrow(error) (float64, error)
}
func ParseJsonLD(value any, options *ld.JsonLdOptions) (JsonLDReader, error) {
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
}
return of(expanded), nil
}