github.com/altipla-consulting/ravendb-go-client@v0.1.3/lazy_multi_loader_with_include.go (about)

     1  package ravendb
     2  
     3  // Note: ILazyLoaderWithInclude is LazyMultiLoaderWithInclude
     4  
     5  // LazyMultiLoaderWithInclude is for lazily loading one or more objects with includes
     6  type LazyMultiLoaderWithInclude struct {
     7  	session  *DocumentSession
     8  	includes []string
     9  }
    10  
    11  // NewLazyMultiLoaderWithInclude creates a lazy multi loader with includes
    12  func NewLazyMultiLoaderWithInclude(session *DocumentSession) *LazyMultiLoaderWithInclude {
    13  	return &LazyMultiLoaderWithInclude{
    14  		session: session,
    15  	}
    16  }
    17  
    18  // Include adds ids of objects to add in a request
    19  func (l *LazyMultiLoaderWithInclude) Include(path string) *LazyMultiLoaderWithInclude {
    20  	l.includes = append(l.includes, path)
    21  	return l
    22  }
    23  
    24  // LoadMulti lazily loads multiple values of a given type with given ids
    25  func (l *LazyMultiLoaderWithInclude) LoadMulti(ids []string) (*Lazy, error) {
    26  	if len(ids) == 0 {
    27  		return nil, newIllegalArgumentError("ids cannot be empty array")
    28  	}
    29  	return l.session.lazyLoadInternal(ids, l.includes, nil, nil), nil
    30  }
    31  
    32  // Load lazy loads a value with a given id into result
    33  func (l *LazyMultiLoaderWithInclude) Load(id string) (*Lazy, error) {
    34  	if id == "" {
    35  		return nil, newIllegalArgumentError("id cannot be empty string")
    36  	}
    37  	ids := []string{id}
    38  	// result should be **Foo, make map[string]*Foo
    39  
    40  	lazy := l.session.lazyLoadInternal(ids, l.includes, nil, nil)
    41  	valueFactory := func(result interface{}) error {
    42  		return lazy.GetValue(result)
    43  	}
    44  	return newLazy(valueFactory), nil
    45  }