<commit_before>package yml

import (
	"bytes"
	"github.com/spf13/viper"
	"github.com/zouyx/agollo/v3/constant"
	"github.com/zouyx/agollo/v3/extension"
	"github.com/zouyx/agollo/v3/utils"
)

var vp = viper.New()

func init() {
	parser := &Parser{}
	extension.AddFormatParser(constant.YML, parser)
	extension.AddFormatParser(constant.YAML, parser)
	vp.SetConfigType("yml")
}

// Parser properties转换器
type Parser struct {
}

// Parse 内存内容=>yml文件转换器
func (d *Parser) Parse(configContent interface{}) (map[string]interface{}, error) {
	content, ok := configContent.(string)
	if !ok {
		return nil, nil
	}
	if utils.Empty == content{
		return nil, nil
	}

	buffer := bytes.NewBufferString(content)
	// 使用viper解析
	err := vp.ReadConfig(buffer)
	if err != nil {
		return nil, err
	}

	return convertToMap(vp), nil
}

func convertToMap(vp *viper.Viper) map[string]interface{}{
	if vp == nil {
		return nil
	}

	m := make(map[string]interface{})
	for _, key := range vp.AllKeys() {
		m[key] = vp.Get(key)
	}
	return m
}<commit_msg>delete useless code<commit_after>package yml

import (
	"bytes"
	"github.com/spf13/viper"
	"github.com/zouyx/agollo/v3/constant"
	"github.com/zouyx/agollo/v3/extension"
	"github.com/zouyx/agollo/v3/utils"
)

var vp = viper.New()

func init() {
	parser := &Parser{}
	extension.AddFormatParser(constant.YML, parser)
	vp.SetConfigType("yml")
}

// Parser properties转换器
type Parser struct {
}

// Parse 内存内容=>yml文件转换器
func (d *Parser) Parse(configContent interface{}) (map[string]interface{}, error) {
	content, ok := configContent.(string)
	if !ok {
		return nil, nil
	}
	if utils.Empty == content {
		return nil, nil
	}

	buffer := bytes.NewBufferString(content)
	// 使用viper解析
	err := vp.ReadConfig(buffer)
	if err != nil {
		return nil, err
	}

	return convertToMap(vp), nil
}

func convertToMap(vp *viper.Viper) map[string]interface{} {
	if vp == nil {
		return nil
	}

	m := make(map[string]interface{})
	for _, key := range vp.AllKeys() {
		m[key] = vp.Get(key)
	}
	return m
}
{'fraction_non_alphanumeric': 0.15465465465465467, 'fraction_numerical': 0.005005005005005005, 'mean_word_length': 4.125641025641026, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4119617', 'n_tokens_mistral': 836, 'n_tokens_neox': 807, 'n_words': 162}