-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsonprocessor.go
30 lines (29 loc) · 998 Bytes
/
jsonprocessor.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
package dynamictags
// Create processor to process 'json' tag.
// This processor replace structure field with 'json' tag
// by value get from json. Example usage:
// jsonFile, err := os.Open("serverconfiguration.json")
// if err != nil
// return err
// defer jsonFile.close()
// val, _ := ioutil.ReadAll(jsonFile)
// var res any
// err = json.Unmarshal([]byte(val), &res)
// if err != nil {
// return err
// }
// processor := NewJsonProcessor(res, "$.database")
// processor.Process(&databaseConfiguration, nil)
// Returns:
// - Json tag processor if success.
// - error if error occured during processor creation
func NewJsonProcessor(content any, rootPath string) (*DynamicTagProcessor, error) {
converter, err := NewJsonTagConverter(content, rootPath)
if err != nil || converter == nil {
return nil, err
}
processor := DynamicTagProcessor{}
processor.InitProcessor()
processor.AddTagConverter(converter)
return &processor, nil
}