// SplitYAML splits a YAML file into unstructured objects. Returns list of all unstructured objects
// found in the yaml. If any errors occurred, returns the first one
499686
func SplitYAML(out string) ([]*unstructured.Unstructured, error) {
	parts := diffSeparator.Split(out, -1)
	var objs []*unstructured.Unstructured
	var firstErr error
	for _, part := range parts {
		var objMap map[string]interface{}
		err := yaml.Unmarshal([]byte(part), &objMap)
		if err != nil {
			if firstErr == nil {
				firstErr = fmt.Errorf("Failed to unmarshal manifest: %v", err)
			}
			continue
		}
		if len(objMap) == 0 {
			// handles case where theres no content between `---`
			continue
		}
		var obj unstructured.Unstructured
		err = yaml.Unmarshal([]byte(part), &obj)
		if err != nil {
			if firstErr == nil {
				firstErr = fmt.Errorf("Failed to unmarshal manifest: %v", err)
			}
			continue
		}
		remObj, err := Remarshal(&obj)
		if err != nil {
			log.Debugf("Failed to remarshal oject: %v", err)
		} else {
			obj = *remObj
		}
		objs = append(objs, &obj)
	}
	return objs, firstErr
}
313780
func _yaml(v map[string]interface{}) string {
	data, err := yaml.Marshal(v)
	if err != nil {
		return err.Error()
	}
	return string(data)
}
645335
func writeYAML(w io.Writer, values interface{}) error {
	data, err := yaml.Marshal(values)
	if err != nil {
		return trace.Wrap(err)
	}
	_, err = w.Write(data)
	return trace.Wrap(err)
}
962523
func splitApplicationAndCharmConfigFromYAML(modelType state.ModelType, inYaml, appName string) (
	appCfg map[string]interface{},
	outYaml string,
	_ error,
) {
	var allSettings map[string]map[string]interface{}
	if err := goyaml.Unmarshal([]byte(inYaml), &allSettings); err != nil {
		return nil, "", errors.Annotate(err, "cannot parse settings data")
	}
	settings, ok := allSettings[appName]
	if !ok {
		return nil, "", errors.Errorf("no settings found for %q", appName)
	}

	providerSchema, _, err := applicationConfigSchema(modelType)
	if err != nil {
		return nil, "", errors.Trace(err)
	}
	appConfigKeys := application.KnownConfigKeys(providerSchema)

	appConfigAttrs := make(map[string]interface{})
	for k, v := range settings {
		if appConfigKeys.Contains(k) {
			appConfigAttrs[k] = v
			delete(settings, k)
		}
	}
	if len(settings) == 0 {
		return appConfigAttrs, "", nil
	}

	allSettings[appName] = settings
	charmConfig, err := goyaml.Marshal(allSettings)
	if err != nil {
		return nil, "", errors.Annotate(err, "cannot marshall charm settings")
	}
	return appConfigAttrs, string(charmConfig), nil
}
321915
func SplitMap(s, sep string, m func(p string) (string, bool)) []string {
	parts := strings.Split(s, sep)
	out := []string{}
	for _, part := range parts {
		if mp, ok := m(part); ok {
			out = append(out, mp)
		}
	}
	return out
}
444713
func saveYAML(stdout io.Writer, object saveObject, out string) error {
	buf, err := yaml.Marshal(object)
	if err != nil {
		return errors.Wrap(err, "marshalling yaml")
	}

	// to stdout
	if out == "-" {
		fmt.Fprintln(stdout, "---")
		fmt.Fprint(stdout, string(buf))
		return nil
	}

	// to a directory
	path, err := outputFile(stdout, object, out)
	if err != nil {
		return err
	}

	file, err := os.Create(path)
	if err != nil {
		return errors.Wrap(err, "creating yaml file")
	}
	defer file.Close()

	// We prepend a document separator, because it helps when files
	// are cat'd together, and is otherwise harmless.
	fmt.Fprintln(file, "---")
	if _, err := file.Write(buf); err != nil {
		return errors.Wrap(err, "writing yaml file")
	}

	return nil
}
709625
func (s *Secret) decodeYAML() (bool, error) {
	if !strings.HasPrefix(s.body, "---\n") && s.password != "---" {
		return false, nil
	}
	d := make(map[string]interface{})
	err := yaml.Unmarshal([]byte(s.body), &d)
	if err != nil {
		return true, err
	}
	s.data = d
	return true, nil
}
1317125
func SplitFirst(m Multiaddr) (*Component, Multiaddr) {
	// Shortcut if we already have a component
	if c, ok := m.(*Component); ok {
		return c, nil
	}

	b := m.Bytes()
	if len(b) == 0 {
		return nil, nil
	}
	n, c, err := readComponent(b)
	if err != nil {
		panic(err)
	}
	if len(b) == n {
		return &c, nil
	}
	return &c, &multiaddr{b[n:]}
}
1229175
func splitPath(path string) []string {
	path = strings.Trim(path, "/")
	if path == "" {
		return []string{}
	}
	return strings.Split(path, "/")
}
600758
func SplitWithNewlines(data []byte, atEOF bool) (advance int, token []byte, err error) {
	return split(true, data, atEOF)
}
377177
func Splitlines(s string) []string {
	s = strings.Replace(s, "\r", "\n", -1)
	s = strings.Replace(s, "\r\n", "\n", -1)
	return MapS(trimnewlines, FilterS(nonempty, strings.Split(s, "\n")))
}
724697
func SplitString(str string) []string {
	if len(str) == 0 {
		return []string{}
	}

	return regexp.MustCompile("[, ]+").Split(str, -1)
}
75235
func SplitAttrs(v []interface{}) ([]interface{}, *Attrs) {
	if len(v) == 0 {
		return v, nil
	}

	attrs, ok := v[len(v)-1].(Attrs)

	if !ok {
		return v, nil
	}

	v = v[:len(v)-1]
	return v, &attrs
}
1107387
func splitTerms(fieldSelector string) []string {
	if len(fieldSelector) == 0 {
		return nil
	}

	terms := make([]string, 0, 1)
	startIndex := 0
	inSlash := false
	for i, c := range fieldSelector {
		switch {
		case inSlash:
			inSlash = false
		case c == '\\':
			inSlash = true
		case c == ',':
			terms = append(terms, fieldSelector[startIndex:i])
			startIndex = i + 1
		}
	}

	terms = append(terms, fieldSelector[startIndex:])

	return terms
}
924560
func split(data []byte) [][]byte {
	chunks := [][]byte{}

	start := 0
	dirDepth := 0
	for i, b := range data {
		switch b {
		case dirStartMark:
			dirDepth++
		case dirEndMark:
			dirDepth--
		case dirElementSep:
			if dirDepth == 0 {
				chunks = append(chunks, data[start:i+1])
				start = i + 1
			}
		}
	}
	chunks = append(chunks, data[start:])

	return chunks
}
795078
func SplitLast(m Multiaddr) (Multiaddr, *Component) {
	// Shortcut if we already have a component
	if c, ok := m.(*Component); ok {
		return nil, c
	}

	b := m.Bytes()
	if len(b) == 0 {
		return nil, nil
	}

	var (
		c      Component
		err    error
		offset int
	)
	for {
		var n int
		n, c, err = readComponent(b[offset:])
		if err != nil {
			panic(err)
		}
		if len(b) == n+offset {
			// Reached end
			if offset == 0 {
				// Only one component
				return nil, &c
			}
			return &multiaddr{b[:offset]}, &c
		}
		offset += n
	}
}
395344
func split(s string) (a []rune) {
	for _, ch := range s {
		a = append(a, ch)
	}
	return
}