// NewJSONSource returns an InputSourceContext suitable for retrieving
// config variables from raw JSON data.
962549
func NewJSONSource(data []byte) (InputSourceContext, error) {
	var deserialized map[string]interface{}
	if err := json.Unmarshal(data, &deserialized); err != nil {
		return nil, err
	}
	return &jsonSource{deserialized: deserialized}, nil
}
832120
func NewJsonReference(in interface{}, context *compiler.Context) (*JsonReference, error) {
	errors := make([]error, 0)
	x := &JsonReference{}
	m, ok := compiler.UnpackMap(in)
	if !ok {
		message := fmt.Sprintf("has unexpected value: %+v (%T)", in, in)
		errors = append(errors, compiler.NewError(context, message))
	} else {
		requiredKeys := []string{"$ref"}
		missingKeys := compiler.MissingKeysInMap(m, requiredKeys)
		if len(missingKeys) > 0 {
			message := fmt.Sprintf("is missing required %s: %+v", compiler.PluralProperties(len(missingKeys)), strings.Join(missingKeys, ", "))
			errors = append(errors, compiler.NewError(context, message))
		}
		allowedKeys := []string{"$ref", "description"}
		var allowedPatterns []*regexp.Regexp
		invalidKeys := compiler.InvalidKeysInMap(m, allowedKeys, allowedPatterns)
		if len(invalidKeys) > 0 {
			message := fmt.Sprintf("has invalid %s: %+v", compiler.PluralProperties(len(invalidKeys)), strings.Join(invalidKeys, ", "))
			errors = append(errors, compiler.NewError(context, message))
		}
		// string _ref = 1;
		v1 := compiler.MapValueForKey(m, "$ref")
		if v1 != nil {
			x.XRef, ok = v1.(string)
			if !ok {
				message := fmt.Sprintf("has unexpected value for $ref: %+v (%T)", v1, v1)
				errors = append(errors, compiler.NewError(context, message))
			}
		}
		// string description = 2;
		v2 := compiler.MapValueForKey(m, "description")
		if v2 != nil {
			x.Description, ok = v2.(string)
			if !ok {
				message := fmt.Sprintf("has unexpected value for description: %+v (%T)", v2, v2)
				errors = append(errors, compiler.NewError(context, message))
			}
		}
	}
	return x, compiler.NewErrorGroupOrNil(errors)
}
492043
func (s *SeesawEngine) ConfigSource(args *ipc.ConfigSource, oldSource *string) error {
	if args == nil {
		return errors.New("args is nil")
	}
	ctx := args.Ctx
	s.trace("ConfigSource", ctx)
	if ctx == nil {
		return errors.New("context is nil")
	}

	if !ctx.IsTrusted() {
		return errors.New("insufficient access")
	}

	if oldSource != nil {
		*oldSource = s.engine.notifier.Source().String()
	}
	newSource := args.Source
	if newSource == "" {
		return nil
	}
	source, err := config.SourceByName(newSource)
	if err != nil {
		return err
	}
	s.engine.notifier.SetSource(source)
	return nil
}
480866
func NewEnvSource(prefix string, env ...string) Source {
	vars := makeEnvVars(env)
	base := make([]string, 0, 10)

	if prefix != "" {
		base = append(base, prefix)
	}

	return SourceFunc(func(dst Map) (err error) {
		dst.Scan(func(path []string, item MapItem) {
			path = append(base, path...)
			path = append(path, item.Name)

			k := snakecaseUpper(strings.Join(path, "_"))

			if v, ok := vars[k]; ok {
				if e := item.Value.Set(v); e != nil {
					err = e
				}
			}
		})
		return
	})
}
1017615
func NewJSONSourceFromFile(f string) (InputSourceContext, error) {
	data, err := loadDataFrom(f)
	if err != nil {
		return nil, err
	}
	return NewJSONSource(data)
}
590035
func (s *Service) NewSource(w http.ResponseWriter, r *http.Request) {
	var src chronograf.Source
	if err := json.NewDecoder(r.Body).Decode(&src); err != nil {
		invalidJSON(w, s.Logger)
		return
	}

	ctx := r.Context()
	defaultOrg, err := s.Store.Organizations(ctx).DefaultOrganization(ctx)
	if err != nil {
		unknownErrorWithMessage(w, err, s.Logger)
		return
	}

	if err := ValidSourceRequest(&src, defaultOrg.ID); err != nil {
		invalidData(w, err, s.Logger)
		return
	}

	// By default the telegraf database will be telegraf
	if src.Telegraf == "" {
		src.Telegraf = "telegraf"
	}

	dbType, err := s.tsdbType(ctx, &src)
	if err != nil {
		Error(w, http.StatusBadRequest, "Error contacting source", s.Logger)
		return
	}

	src.Type = dbType
	if src, err = s.Store.Sources(ctx).Add(ctx, src); err != nil {
		msg := fmt.Errorf("error storing source %v: %v", src, err)
		unknownErrorWithMessage(w, msg, s.Logger)
		return
	}

	res := newSourceResponse(ctx, src)
	location(w, res.Links.Self)
	encodeJSON(w, http.StatusCreated, res, s.Logger)
}
891177
func NewSourceService() *SourceService {
	return &SourceService{
		DefaultSourceFn:  func(context.Context) (*platform.Source, error) { return nil, nil },
		FindSourceByIDFn: func(context.Context, platform.ID) (*platform.Source, error) { return nil, nil },
		CreateSourceFn:   func(context.Context, *platform.Source) error { return nil },
		UpdateSourceFn:   func(context.Context, platform.ID, platform.SourceUpdate) (*platform.Source, error) { return nil, nil },
		DeleteSourceFn:   func(context.Context, platform.ID) error { return nil },
		FindSourcesFn: func(context.Context, platform.FindOptions) ([]*platform.Source, int, error) {
			return nil, 0, nil
		},
	}
}
948435
func NewYamlSourceFromFile(file string) (InputSourceContext, error) {
	ysc := &yamlSourceContext{FilePath: file}
	var results map[interface{}]interface{}
	err := readCommandYaml(ysc.FilePath, &results)
	if err != nil {
		return nil, fmt.Errorf("Unable to load Yaml file '%s': inner error: \n'%v'", ysc.FilePath, err.Error())
	}

	return &MapInputSource{valueMap: results}, nil
}
401852
func (fsc *FetchSourceContext) Source() (interface{}, error) {
	if !fsc.fetchSource {
		return false, nil
	}
	if len(fsc.includes) == 0 && len(fsc.excludes) == 0 {
		return true, nil
	}
	src := make(map[string]interface{})
	if len(fsc.includes) > 0 {
		src["includes"] = fsc.includes
	}
	if len(fsc.excludes) > 0 {
		src["excludes"] = fsc.excludes
	}
	return src, nil
}
1052784
func (r *SearchRequest) FetchSourceContext(fsc *FetchSourceContext) *SearchRequest {
	r.searchSource = r.searchSource.FetchSourceContext(fsc)
	return r
}
201475
func (s *ScrollService) FetchSourceContext(fetchSourceContext *FetchSourceContext) *ScrollService {
	s.ss = s.ss.FetchSourceContext(fetchSourceContext)
	return s
}
887356
func (h *Client) DataSource(dataSource string) *Client {
	h.dataSource = dataSource
	h.dataSourceSet = true
	return h
}
654638
func NewURLDataSource(description, baseURL string, hostnameVerification utils.SSLHostnameVerification, priority int, requireSigned bool) DataSource {
	return &urlDataSource{
		description:          description,
		baseURL:              baseURL,
		hostnameVerification: hostnameVerification,
		priority:             priority,
		requireSigned:        requireSigned,
	}
}
867634
func (s *SourceService) CreateSource(ctx context.Context, b *platform.Source) error {
	u, err := newURL(s.Addr, sourcePath)
	if err != nil {
		return err
	}

	octets, err := json.Marshal(b)
	if err != nil {
		return err
	}

	req, err := http.NewRequest("POST", u.String(), bytes.NewReader(octets))
	if err != nil {
		return err
	}

	req.Header.Set("Content-Type", "application/json")
	SetToken(s.Token, req)

	hc := newClient(u.Scheme, s.InsecureSkipVerify)

	resp, err := hc.Do(req)
	if err != nil {
		return err
	}
	defer resp.Body.Close()

	// TODO(jsternberg): Should this check for a 201 explicitly?
	if err := CheckError(resp); err != nil {
		return err
	}

	if err := json.NewDecoder(resp.Body).Decode(b); err != nil {
		return err
	}

	return nil
}
305912
func NewConfigCenterSource(cc config.Client, dimInfo string,
	refreshMode, refreshInterval int) core.ConfigSource {

	if ConfigCenterConfig == nil {
		ConfigCenterConfig = new(Handler)
		ConfigCenterConfig.priority = configCenterSourcePriority
		ConfigCenterConfig.cc = cc
		ConfigCenterConfig.dimensionsInfo = dimInfo
		ConfigCenterConfig.initSuccess = true
		ConfigCenterConfig.RefreshMode = refreshMode
		ConfigCenterConfig.RefreshInterval = time.Second * time.Duration(refreshInterval)

	}
	return ConfigCenterConfig
}
307904
func NewRemoteConfigSource(source *apiv1.NodeConfigSource) (RemoteConfigSource, string, error) {
	// NOTE: Even though the API server validates the config, we check whether all *known* fields are
	// nil here, so that if a new API server allows a new config source type, old clients can send
	// an error message rather than crashing due to a nil pointer dereference.

	// Exactly one reference subfield of the config source must be non-nil.
	// Currently ConfigMap is the only reference subfield.
	if source.ConfigMap == nil {
		return nil, status.AllNilSubfieldsError, fmt.Errorf("%s, NodeConfigSource was: %#v", status.AllNilSubfieldsError, source)
	}
	return &remoteConfigMap{source}, "", nil
}
1371648
func NewLocalUpdateSource(path string, jsonPath string, log Log) LocalUpdateSource {
	return LocalUpdateSource{
		path:     path,
		jsonPath: jsonPath,
		log:      log,
	}
}