You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by al...@apache.org on 2025/08/31 14:16:12 UTC

(dubbo-go) branch main updated: fix(test): register custom parser with concurrent safety for unit test (#2999)

This is an automated email from the ASF dual-hosted git repository.

alexstocks pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/dubbo-go.git


The following commit(s) were added to refs/heads/main by this push:
     new 5b3c17933 fix(test): register custom parser with concurrent safety for unit test (#2999)
5b3c17933 is described below

commit 5b3c17933790003d83d3d41502edc3692d400482
Author: Rinai <ri...@g-rinai.cn>
AuthorDate: Sun Aug 31 22:16:06 2025 +0800

    fix(test): register custom parser with concurrent safety for unit test (#2999)
---
 config_center/apollo/impl_test.go | 45 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 45 insertions(+)

diff --git a/config_center/apollo/impl_test.go b/config_center/apollo/impl_test.go
index a9ac778f8..a3fee2de2 100644
--- a/config_center/apollo/impl_test.go
+++ b/config_center/apollo/impl_test.go
@@ -18,6 +18,7 @@
 package apollo
 
 import (
+	"bytes"
 	"fmt"
 	"net/http"
 	"net/http/httptest"
@@ -27,11 +28,16 @@ import (
 )
 
 import (
+	"github.com/apolloconfig/agollo/v4/constant"
+	"github.com/apolloconfig/agollo/v4/extension"
+
 	"github.com/knadh/koanf"
 	"github.com/knadh/koanf/parsers/json"
 	"github.com/knadh/koanf/parsers/yaml"
 	"github.com/knadh/koanf/providers/rawbytes"
 
+	"github.com/spf13/viper"
+
 	"github.com/stretchr/testify/assert"
 )
 
@@ -172,6 +178,9 @@ func TestGetConfigItem(t *testing.T) {
 }
 
 func initMockApollo(t *testing.T) *apolloConfiguration {
+	// Register the YAML format parser with concurrent safety.
+	extension.AddFormatParser(constant.YAML, &Parser{})
+	extension.AddFormatParser(constant.YML, &Parser{})
 	c := &config.RootConfig{ConfigCenter: &config.CenterConfig{
 		Protocol:  "apollo",
 		Address:   "localhost:8080",
@@ -238,3 +247,39 @@ func (l *apolloDataListener) Process(configType *config_center.ConfigChangeEvent
 	l.count++
 	l.event = configType.Key
 }
+
+// custom parser with concurrent safety
+type Parser struct {
+}
+
+func (d *Parser) Parse(configContent any) (map[string]any, error) {
+	content, ok := configContent.(string)
+	if !ok {
+		return nil, nil
+	}
+	if content == "" {
+		return nil, nil
+	}
+	vp := viper.New()
+	vp.SetConfigType("yaml")
+	buffer := bytes.NewBufferString(content)
+	// use viper to parse
+	err := vp.ReadConfig(buffer)
+	if err != nil {
+		return nil, err
+	}
+
+	return convertToMap(vp), nil
+}
+
+func convertToMap(vp *viper.Viper) map[string]any {
+	if vp == nil {
+		return nil
+	}
+
+	m := make(map[string]any)
+	for _, key := range vp.AllKeys() {
+		m[key] = vp.Get(key)
+	}
+	return m
+}