
- No more BaseObject with Id and Type included, moved those into the nsUndefined namespace - Refactored the full parser funcs into two slightly distinct ones: - One that takes in raw bytes and one that takes in a map[string]any
115 lines
2.3 KiB
Go
115 lines
2.3 KiB
Go
package goap
|
|
|
|
import (
|
|
"fmt"
|
|
"slices"
|
|
|
|
"github.com/piprate/json-gold/ld"
|
|
)
|
|
|
|
var allInternalParsersExceptBase []UnmarshalFunc = []UnmarshalFunc{
|
|
ParseUDIdData,
|
|
ParseUDTypeData,
|
|
ParseASActorData,
|
|
ParseASAlsoKnownAsData,
|
|
ParseASAttachmentsData,
|
|
ParseASAttributedToData,
|
|
ParseASCCData,
|
|
ParseASContentData,
|
|
ParseASEndpointsData,
|
|
ParseASFollowersData,
|
|
ParseASFollowingData,
|
|
ParseASHrefData,
|
|
ParseASIconData,
|
|
ParseASImageData,
|
|
ParseASMediaTypeData,
|
|
ParseASNameData,
|
|
ParseASOutboxData,
|
|
ParseASObjectData,
|
|
ParseASPreferredNameData,
|
|
ParseASPublishedData,
|
|
ParseASRestrictedData,
|
|
ParseASRepliesData,
|
|
ParseASSharedInboxData,
|
|
ParseASSummaryData,
|
|
ParseASSensitiveData,
|
|
ParseASTagData,
|
|
ParseASToData,
|
|
ParseASUrlData,
|
|
ParseASUpdatedData,
|
|
|
|
ParseOstatusAtomUriData,
|
|
ParseOstatusConversationData,
|
|
|
|
ParseW3InboxData,
|
|
|
|
ParseW3VcardAddressData,
|
|
ParseW3VcardBirthdayData,
|
|
|
|
ParseW3SecurityOwnerData,
|
|
ParseW3SecurityPublicKeyPemData,
|
|
ParseW3SecurityPublicKeyData,
|
|
|
|
ParseMastoDevicesData,
|
|
ParseMastoDiscoverableData,
|
|
ParseMastoFeaturedData,
|
|
ParseMastoDiscoverableData,
|
|
ParseMastoIndexableData,
|
|
ParseMastoMemorialData,
|
|
|
|
ParseSchemaValueData,
|
|
|
|
ParseMKIsCatData,
|
|
ParseMKSummaryData,
|
|
|
|
ParseFFSpeakAsCatData,
|
|
}
|
|
|
|
func chainParse(
|
|
raw map[string]any,
|
|
start BaseApChain,
|
|
parsers ...UnmarshalFunc,
|
|
) (BaseApChain, []error) {
|
|
var current BaseApChain = start
|
|
errors := []error{}
|
|
|
|
for _, p := range parsers {
|
|
next, err := p(raw, current)
|
|
current = next
|
|
errors = append(errors, err)
|
|
}
|
|
|
|
return current, errors
|
|
}
|
|
|
|
func Unmarshal(
|
|
raw []byte,
|
|
ldOptions *ld.JsonLdOptions,
|
|
processor *ld.JsonLdProcessor,
|
|
extraParsers ...UnmarshalFunc,
|
|
) (BaseApChain, []error) {
|
|
if ldOptions == nil {
|
|
ldOptions = ld.NewJsonLdOptions("")
|
|
}
|
|
if processor == nil {
|
|
processor = ld.NewJsonLdProcessor()
|
|
}
|
|
rawData, err := processor.Expand(raw, ldOptions)
|
|
if err != nil {
|
|
return nil, []error{err}
|
|
}
|
|
data, ok := rawData[0].(map[string]any)
|
|
if !ok {
|
|
return nil, []error{fmt.Errorf("failed to cast expand result into map[string]any")}
|
|
}
|
|
return UnmarshalPreprocessed(data, extraParsers...)
|
|
}
|
|
|
|
func UnmarshalPreprocessed(
|
|
raw map[string]any,
|
|
extraParsers ...UnmarshalFunc,
|
|
) (BaseApChain, []error) {
|
|
base := EmptyBaseObject{}
|
|
allParsers := slices.Concat(allInternalParsersExceptBase, extraParsers)
|
|
return chainParse(raw, &base, allParsers...)
|
|
}
|