2018-06-29 12:44:16 +03:00
|
|
|
package metadata
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
|
2018-06-29 15:03:25 +01:00
|
|
|
"github.com/coredns/coredns/request"
|
2018-06-29 12:44:16 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Provider interface needs to be implemented by each plugin willing to provide
|
|
|
|
|
// metadata information for other plugins.
|
|
|
|
|
// Note: this method should work quickly, because it is called for every request
|
|
|
|
|
// from the metadata plugin.
|
|
|
|
|
type Provider interface {
|
|
|
|
|
// List of variables which are provided by current Provider. Must remain constant.
|
|
|
|
|
MetadataVarNames() []string
|
|
|
|
|
// Metadata is expected to return a value with metadata information by the key
|
|
|
|
|
// from 4th argument. Value can be later retrieved from context by any other plugin.
|
|
|
|
|
// If value is not available by some reason returned boolean value should be false.
|
2018-06-29 15:03:25 +01:00
|
|
|
Metadata(ctx context.Context, state request.Request, variable string) (interface{}, bool)
|
2018-06-29 12:44:16 +03:00
|
|
|
}
|
|
|
|
|
|
2018-06-29 15:03:25 +01:00
|
|
|
// M is metadata information storage.
|
|
|
|
|
type M map[string]interface{}
|
2018-06-29 12:44:16 +03:00
|
|
|
|
2018-06-29 15:03:25 +01:00
|
|
|
// FromContext retrieves the metadata from the context.
|
|
|
|
|
func FromContext(ctx context.Context) (M, bool) {
|
2018-06-29 12:44:16 +03:00
|
|
|
if metadata := ctx.Value(metadataKey{}); metadata != nil {
|
2018-06-29 15:03:25 +01:00
|
|
|
if m, ok := metadata.(M); ok {
|
|
|
|
|
return m, true
|
2018-06-29 12:44:16 +03:00
|
|
|
}
|
|
|
|
|
}
|
2018-06-29 15:03:25 +01:00
|
|
|
return M{}, false
|
2018-06-29 12:44:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Value returns metadata value by key.
|
2018-06-29 15:03:25 +01:00
|
|
|
func (m M) Value(key string) (value interface{}, ok bool) {
|
2018-06-29 12:44:16 +03:00
|
|
|
value, ok = m[key]
|
|
|
|
|
return value, ok
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-29 15:03:25 +01:00
|
|
|
// SetValue sets the metadata value under key.
|
|
|
|
|
func (m M) SetValue(key string, val interface{}) {
|
2018-06-29 12:44:16 +03:00
|
|
|
m[key] = val
|
|
|
|
|
}
|
2018-06-29 15:03:25 +01:00
|
|
|
|
|
|
|
|
// metadataKey defines the type of key that is used to save metadata into the context.
|
|
|
|
|
type metadataKey struct{}
|