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