github.com/hyperledger/aries-framework-go@v0.3.2/pkg/doc/ld/ld.go (about) 1 /* 2 Copyright SecureKey Technologies Inc. All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package ld 8 9 import ( 10 jsonld "github.com/piprate/json-gold/ld" 11 12 ldcontext "github.com/hyperledger/aries-framework-go/component/models/ld/context" 13 "github.com/hyperledger/aries-framework-go/component/models/ld/documentloader" 14 ldstore "github.com/hyperledger/aries-framework-go/component/models/ld/store" 15 ) 16 17 // ErrContextNotFound is returned when JSON-LD context document is not found in the underlying storage. 18 var ErrContextNotFound = documentloader.ErrContextNotFound 19 20 // provider contains dependencies for the JSON-LD document loader. 21 type provider interface { 22 JSONLDContextStore() ldstore.ContextStore 23 JSONLDRemoteProviderStore() ldstore.RemoteProviderStore 24 } 25 26 // DocumentLoader is an implementation of ld.DocumentLoader backed by storage. 27 type DocumentLoader = documentloader.DocumentLoader 28 29 // NewDocumentLoader returns a new DocumentLoader instance. 30 // 31 // Embedded contexts (`ldcontext/embed/third_party`) are preloaded into the underlying storage. 32 // Additional contexts can be set using WithExtraContexts() option or provided by one or more remote providers. 33 // Use multiple WithRemoteProvider() options for setting up more than one remote JSON-LD context provider. 34 // 35 // By default, missing contexts are not fetched from the remote URL. Use WithRemoteDocumentLoader() option 36 // to specify a custom loader that can resolve context documents from the network. 37 func NewDocumentLoader(ctx provider, opts ...DocumentLoaderOpts) (*DocumentLoader, error) { 38 return documentloader.NewDocumentLoader(ctx, opts...) 39 } 40 41 // DocumentLoaderOpts configures DocumentLoader during creation. 42 type DocumentLoaderOpts = documentloader.Opts 43 44 // WithRemoteDocumentLoader specifies loader for fetching JSON-LD context documents from remote URLs. 45 // Documents are fetched with this loader only if they are not found in the underlying storage. 46 func WithRemoteDocumentLoader(loader jsonld.DocumentLoader) DocumentLoaderOpts { 47 return documentloader.WithRemoteDocumentLoader(loader) 48 } 49 50 // WithExtraContexts sets the extra contexts (in addition to embedded) for preloading into the underlying storage. 51 func WithExtraContexts(contexts ...ldcontext.Document) DocumentLoaderOpts { 52 return documentloader.WithExtraContexts(contexts...) 53 } 54 55 // RemoteProvider defines a remote JSON-LD context provider. 56 type RemoteProvider = documentloader.RemoteProvider 57 58 // WithRemoteProvider adds a remote JSON-LD context provider to the list of providers. 59 func WithRemoteProvider(provider RemoteProvider) DocumentLoaderOpts { 60 return documentloader.WithRemoteProvider(provider) 61 }