Skip to content

Commit

Permalink
docs: update dataloader.go.gotmpl
Browse files Browse the repository at this point in the history
  • Loading branch information
NateScarlet committed Jun 12, 2020
1 parent b34e750 commit fd681b3
Showing 1 changed file with 15 additions and 26 deletions.
41 changes: 15 additions & 26 deletions templates/dataloader.go.gotmpl
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
{{- /*
Accept data:
Key: type without import path, can be slice or pointer.
KeyImport: key type import path,.
Value: type without import path, can be slice or pointer.
ValueImport: value type import path,.
*/ -}}
// Code generated by github.com/vektah/dataloaden, DO NOT EDIT.
// referenced https://github.com/vektah/dataloaden/blob/master/pkg/generator/template.go
package dataloader

{{- $key := .Key }}
{{- $value := .Value}}
Expand All @@ -27,11 +23,7 @@ Accept data:
{{- end }}
{{- if $isSlice }}
{{- $value = printf "[]%s" $value }}
{{- end -}}

// Code generated from {{ templateFiles }}, DO NOT EDIT.
// referenced https://github.com/vektah/dataloaden/blob/master/pkg/generator/template.go
package {{.Package}}
{{- end }}

import (
"sync"
Expand Down Expand Up @@ -165,29 +157,26 @@ func (l *{{.Name}}) LoadAllThunk(keys []{{ $key }}) func() ([]{{ $value }}, []er
}
}

// Set the cache with the provided key and value.
// It is the caller's responsibility to avoid pass same pointer from a loop.
func (l *{{.Name}}) Set(key {{ $key }}, value {{ $value }}) {
l.mu.Lock()
defer l.mu.Unlock()

l.unsafeSet(key, value)
}

// Prime the cache with the provided key and value. If the key already exists, no change is made
// and false is returned.
// (To forcefully prime the cache, clear the key first with loader.clear(key).prime(key, value).)
// It is the caller's responsibility to avoid pass same pointer from a loop.
// (To forcefully prime the cache, use Set.)
func (l *{{.Name}}) Prime(key {{ $key }}, value {{ $value }}) bool {
l.mu.Lock()
defer l.mu.Unlock()

var found bool
if _, found = l.cache[key]; !found {
{{- if $isPointer }}
// make a copy when writing to the cache, its easy to pass a pointer in from a loop var
// and end up with the whole cache pointing to the same value.
cpy := *value
l.unsafeSet(key, &cpy)
{{- else if $isSlice }}
// make a copy when writing to the cache, its easy to pass a pointer in from a loop var
// and end up with the whole cache pointing to the same value.
cpy := make({{ $value }}, len(value))
copy(cpy, value)
l.unsafeSet(key, cpy)
{{- else }}
l.unsafeSet(key, value)
{{- end }}
}

return !found
Expand Down

0 comments on commit fd681b3

Please sign in to comment.