-
Notifications
You must be signed in to change notification settings - Fork 0
/
JSONParser.go
59 lines (50 loc) · 1.51 KB
/
JSONParser.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
58
59
package jsonparser
import (
"encoding/json"
"errors"
"log"
"reflect"
)
type JSONParser struct {
JSONSchema any
ConvertFunction func(any, string) any
ValidatorFunction func(any) error
Output any
Name string
}
func (parser *JSONParser) Parse(data []byte, url string) (any, error) {
// Check if schema and output are pointers
if reflect.ValueOf(parser.JSONSchema).Kind() != reflect.Ptr {
return nil, errors.New("JSONSchema must be a pointer")
}
if reflect.ValueOf(parser.Output).Kind() != reflect.Ptr {
return nil, errors.New("output must be a pointer")
}
// Check if schema and output are structs
if reflect.ValueOf(parser.JSONSchema).Elem().Kind() != reflect.Struct {
return nil, errors.New("JSONSchema must be a pointer to a struct")
}
if reflect.ValueOf(parser.Output).Elem().Kind() != reflect.Struct {
return nil, errors.New("output must be a pointer to a struct")
}
// Create a new instance of the schema and output struct
schema := reflect.New(reflect.ValueOf(parser.JSONSchema).Elem().Type()).Interface()
// Unmarshal the data into the schema
err := json.Unmarshal(data, schema)
if err != nil {
return nil, err
}
// Check if the schema is valid
err = parser.ValidatorFunction(schema)
if err != nil {
// Log failing url
log.Println(url)
return nil, err
}
// Use the convert function if it is set
if parser.ConvertFunction == nil {
return nil, errors.New("ConvertFunction is not set")
}
output := parser.ConvertFunction(schema, url)
return output, nil
}