Add NotEmptyChecker to overwrite default NotEmpty function

This commit is contained in:
Anthony Wang 2022-11-26 17:14:42 +00:00
parent 83b604a5ab
commit 81405e14ea
Signed by: a
GPG key ID: 42A5B952E6DD8D38

View file

@ -25,6 +25,9 @@ var ItemTyperFunc TyperFn = GetItemByType
// JSONItemUnmarshal can be set externally to populate a custom object based on its type
var JSONItemUnmarshal JSONUnmarshalerFn = nil
// NotEmptyChecker checks if an object is empty
var NotEmptyChecker NotEmptyCheckerFn = NotEmpty
// TyperFn is the type of the function which returns an Item struct instance
// for a specific ActivityVocabularyType
type TyperFn func(ActivityVocabularyType) (Item, error)
@ -33,6 +36,9 @@ type TyperFn func(ActivityVocabularyType) (Item, error)
// that the current package doesn't know about.
type JSONUnmarshalerFn func(ActivityVocabularyType, *fastjson.Value, Item) error
// NotEmptyCheckerFn is the type of a function that checks if an object is empty
type NotEmptyCheckerFn func(Item) bool
func JSONGetID(val *fastjson.Value) ID {
i := val.Get("id").GetStringBytes()
return ID(i)
@ -186,6 +192,7 @@ func JSONLoadItem(val *fastjson.Value) (Item, error) {
if err != nil || IsNil(i) {
return nil, nil
}
var empty = func(i Item) bool { return !NotEmptyChecker(i) }
switch typ {
case "":
@ -258,6 +265,9 @@ func JSONLoadItem(val *fastjson.Value) (Item, error) {
if err != nil {
return nil, err
}
if empty(i) {
return nil, nil
}
return i, nil
}