Copilot commented on code in PR #2866: URL: https://github.com/apache/dubbo-go/pull/2866#discussion_r2083690691
########## config_center/apollo/impl.go: ########## @@ -0,0 +1,170 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package apollo + +import ( + "fmt" + "regexp" + "strings" + "sync" + + "github.com/apolloconfig/agollo/v4" + "github.com/apolloconfig/agollo/v4/env/config" + gxset "github.com/dubbogo/gost/container/set" + + "dubbo.apache.org/dubbo-go/v3/common" + "dubbo.apache.org/dubbo-go/v3/common/constant" + perrors "github.com/pkg/errors" + + cc "dubbo.apache.org/dubbo-go/v3/config_center" + "dubbo.apache.org/dubbo-go/v3/config_center/parser" + "github.com/dubbogo/gost/log/logger" +) + +const ( + apolloProtocolPrefix = "http://" +) + +type apolloConfiguration struct { + cc.BaseDynamicConfiguration + url *common.URL + + listeners sync.Map + appConf *config.AppConfig + parser parser.ConfigurationParser + client agollo.Client +} + +func newApolloConfiguration(url *common.URL) (*apolloConfiguration, error) { + c := &apolloConfiguration{ + url: url, + } + c.appConf = &config.AppConfig{ + AppID: url.GetParam(constant.ConfigAppIDKey, ""), + Cluster: url.GetParam(constant.ConfigClusterKey, ""), + NamespaceName: url.GetParam(constant.ConfigNamespaceKey, cc.DefaultGroup), + IP: c.getAddressWithProtocolPrefix(url), + Secret: url.GetParam(constant.ConfigSecretKey, ""), + IsBackupConfig: url.GetParamBool(constant.ConfigBackupConfigKey, true), + BackupConfigPath: url.GetParam(constant.ConfigBackupConfigPathKey, ""), + } + logger.Infof("[Apollo ConfigCenter] New Apollo ConfigCenter with Configuration: %+v, url = %+v", c.appConf, c.url) + client, err := agollo.StartWithConfig(func() (*config.AppConfig, error) { + return c.appConf, nil + }) + if err != nil { + return nil, perrors.Wrap(err, "start apollo client failed") + } + c.client = client + return c, nil +} + +func (c *apolloConfiguration) AddListener(key string, listener cc.ConfigurationListener, opts ...cc.Option) { + tmpOpts := cc.NewOptions(opts...) + key = tmpOpts.Center.Group + key + al := newApolloListener() + l, exist := c.listeners.LoadOrStore(key, al) + if !exist { + c.client.AddChangeListener(al) + } + l.(*apolloListener).AddListener(listener) +} + +func (c *apolloConfiguration) RemoveListener(key string, listener cc.ConfigurationListener, opts ...cc.Option) { + tmpOpts := cc.NewOptions(opts...) + key = tmpOpts.Center.Group + key + l, ok := c.listeners.Load(key) + if ok { + l.(*apolloListener).RemoveListener(listener) + } +} + +func (c *apolloConfiguration) GetInternalProperty(key string, opts ...cc.Option) (string, error) { + if c.client == nil { + return "", perrors.New("apollo client is not initialized") + } + cache := c.client.GetConfigCache(c.appConf.NamespaceName) + if cache == nil { + return "", perrors.New(fmt.Sprintf("nothing in namespace:%s ", key)) + } + value, err := cache.Get(key) + if err != nil { + return "", perrors.Wrap(err, "get config value failed") + } + return value.(string), nil +} + +func (c *apolloConfiguration) GetRule(key string, opts ...cc.Option) (string, error) { + return c.GetInternalProperty(key, opts...) +} + +// PublishConfig will publish the config with the (key, group, value) pair +func (c *apolloConfiguration) PublishConfig(string, string, string) error { + return perrors.New("unsupport operation") +} + +// GetConfigKeysByGroup will return all keys with the group +func (c *apolloConfiguration) GetConfigKeysByGroup(group string) (*gxset.HashSet, error) { + return nil, perrors.New("unsupport operation") Review Comment: The error message uses 'unsupport operation'; consider replacing 'unsupport' with 'unsupported'. ```suggestion return perrors.New("unsupported operation") } // GetConfigKeysByGroup will return all keys with the group func (c *apolloConfiguration) GetConfigKeysByGroup(group string) (*gxset.HashSet, error) { return nil, perrors.New("unsupported operation") ``` ########## config_center/apollo/impl.go: ########## @@ -0,0 +1,170 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package apollo + +import ( + "fmt" + "regexp" + "strings" + "sync" + + "github.com/apolloconfig/agollo/v4" + "github.com/apolloconfig/agollo/v4/env/config" + gxset "github.com/dubbogo/gost/container/set" + + "dubbo.apache.org/dubbo-go/v3/common" + "dubbo.apache.org/dubbo-go/v3/common/constant" + perrors "github.com/pkg/errors" + + cc "dubbo.apache.org/dubbo-go/v3/config_center" + "dubbo.apache.org/dubbo-go/v3/config_center/parser" + "github.com/dubbogo/gost/log/logger" +) + +const ( + apolloProtocolPrefix = "http://" +) + +type apolloConfiguration struct { + cc.BaseDynamicConfiguration + url *common.URL + + listeners sync.Map + appConf *config.AppConfig + parser parser.ConfigurationParser + client agollo.Client +} + +func newApolloConfiguration(url *common.URL) (*apolloConfiguration, error) { + c := &apolloConfiguration{ + url: url, + } + c.appConf = &config.AppConfig{ + AppID: url.GetParam(constant.ConfigAppIDKey, ""), + Cluster: url.GetParam(constant.ConfigClusterKey, ""), + NamespaceName: url.GetParam(constant.ConfigNamespaceKey, cc.DefaultGroup), + IP: c.getAddressWithProtocolPrefix(url), + Secret: url.GetParam(constant.ConfigSecretKey, ""), + IsBackupConfig: url.GetParamBool(constant.ConfigBackupConfigKey, true), + BackupConfigPath: url.GetParam(constant.ConfigBackupConfigPathKey, ""), + } + logger.Infof("[Apollo ConfigCenter] New Apollo ConfigCenter with Configuration: %+v, url = %+v", c.appConf, c.url) + client, err := agollo.StartWithConfig(func() (*config.AppConfig, error) { + return c.appConf, nil + }) + if err != nil { + return nil, perrors.Wrap(err, "start apollo client failed") + } + c.client = client + return c, nil +} + +func (c *apolloConfiguration) AddListener(key string, listener cc.ConfigurationListener, opts ...cc.Option) { + tmpOpts := cc.NewOptions(opts...) + key = tmpOpts.Center.Group + key + al := newApolloListener() + l, exist := c.listeners.LoadOrStore(key, al) + if !exist { + c.client.AddChangeListener(al) + } + l.(*apolloListener).AddListener(listener) +} + +func (c *apolloConfiguration) RemoveListener(key string, listener cc.ConfigurationListener, opts ...cc.Option) { + tmpOpts := cc.NewOptions(opts...) + key = tmpOpts.Center.Group + key + l, ok := c.listeners.Load(key) + if ok { + l.(*apolloListener).RemoveListener(listener) + } +} + +func (c *apolloConfiguration) GetInternalProperty(key string, opts ...cc.Option) (string, error) { + if c.client == nil { + return "", perrors.New("apollo client is not initialized") + } + cache := c.client.GetConfigCache(c.appConf.NamespaceName) + if cache == nil { + return "", perrors.New(fmt.Sprintf("nothing in namespace:%s ", key)) + } + value, err := cache.Get(key) + if err != nil { + return "", perrors.Wrap(err, "get config value failed") + } + return value.(string), nil +} + +func (c *apolloConfiguration) GetRule(key string, opts ...cc.Option) (string, error) { + return c.GetInternalProperty(key, opts...) +} + +// PublishConfig will publish the config with the (key, group, value) pair +func (c *apolloConfiguration) PublishConfig(string, string, string) error { + return perrors.New("unsupport operation") +} + +// GetConfigKeysByGroup will return all keys with the group +func (c *apolloConfiguration) GetConfigKeysByGroup(group string) (*gxset.HashSet, error) { + return nil, perrors.New("unsupport operation") Review Comment: The error message uses 'unsupport operation'; consider replacing 'unsupport' with 'unsupported'. ```suggestion return perrors.New("unsupported operation") } // GetConfigKeysByGroup will return all keys with the group func (c *apolloConfiguration) GetConfigKeysByGroup(group string) (*gxset.HashSet, error) { return nil, perrors.New("unsupported operation") ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
