Exemple #1
0
func (m Matcher) Process(tree *gabs.Container) error {
	return Repeat(5, func() error {
		return ProcessAll(tree, func(ktype string, output *gabs.Container, value interface{}, key interface{}) error {
			if ktype == "map" {
				skey, err := templater.MatchTemplate(key.(string), tree)
				if err != nil {
					return err
				}

				parts := strings.SplitN(skey, "?", 2)
				if valmap, ok := value.(map[string]interface{}); ok && len(parts) > 1 {
					matchValue := strings.Trim(strings.TrimSpace(parts[1]), "\"'")
					newKey := strings.TrimSpace(parts[0])

					output.Delete(key.(string))
					output.Delete(newKey)

					if v, ok := valmap[matchValue]; ok {
						output.Set(v, newKey)
						return nil
					}

					for k, v := range valmap {
						if k == "*" {
							continue
						}

						re, err := regexp.Compile("^" + strings.Trim(k, "^$") + "$")
						if err != nil {
							return err
						}

						matches := re.FindStringSubmatch(matchValue)
						groups := re.SubexpNames()

						if len(matches) > 0 {
							for i, group := range groups {
								if group != "" {
									tree.Set(matches[i], "match", group)
								}
							}

							output.Set(v, newKey)
							return nil
						}
					}

					if v, ok := valmap["*"]; ok {
						output.Set(v, newKey)
					}
				}
			}

			return nil
		})
	})
}
Exemple #2
0
func makePluginPair(plugin string, data *gabs.Container) PluginData {
	if s, ok := data.Data().(string); ok {
		obj := gabs.New()
		ns := strings.Split(plugin, ".")
		obj.Set(s, ns[len(ns)-1])
		data = obj
	} else {
		var cpy interface{}
		bs, _ := json.Marshal(data.Data())
		json.Unmarshal(bs, &cpy)
		data.Set(cpy)
	}

	return PluginData{plugin, PluginRegestry.Get(plugin), Manifest{data}}
}
Exemple #3
0
func includeFile(file string, tree *gabs.Container) error {
	loaded, err := loader.LoadFile(file)
	if err != nil {
		return err
	}

	log.Println("include:", file)

	merged, err := mergemap.Merge(loaded.Data().(map[string]interface{}), tree.Data().(map[string]interface{}))
	if err != nil {
		return err
	}

	_, err = tree.Set(merged)
	return err
}