diff --git a/clients/cache/disk_cache.go b/clients/cache/disk_cache.go index cec60ab3..bc51b15d 100644 --- a/clients/cache/disk_cache.go +++ b/clients/cache/disk_cache.go @@ -21,9 +21,12 @@ import ( "fmt" "io/ioutil" "os" + "runtime" "strconv" + "strings" "github.com/go-errors/errors" + "github.com/nacos-group/nacos-sdk-go/common/constant" "github.com/nacos-group/nacos-sdk-go/common/file" "github.com/nacos-group/nacos-sdk-go/common/logger" "github.com/nacos-group/nacos-sdk-go/model" @@ -31,6 +34,11 @@ import ( ) func GetFileName(cacheKey string, cacheDir string) string { + + if runtime.GOOS == constant.OS_WINDOWS { + cacheKey = strings.ReplaceAll(cacheKey, ":", constant.WINDOWS_LEGAL_NAME_SPLITER) + } + return cacheDir + string(os.PathSeparator) + cacheKey } diff --git a/clients/cache/disk_cache_test.go b/clients/cache/disk_cache_test.go new file mode 100644 index 00000000..bbbabee0 --- /dev/null +++ b/clients/cache/disk_cache_test.go @@ -0,0 +1,36 @@ +/* + * Copyright 1999-2020 Alibaba Group Holding Ltd. + * + * Licensed 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 cache + +import ( + "runtime" + "testing" + + "github.com/nacos-group/nacos-sdk-go/common/constant" + "github.com/stretchr/testify/assert" +) + +func TestGetFileName(t *testing.T) { + + name := GetFileName("nacos@@providers:org.apache.dubbo.UserProvider:hangzhou", "tmp") + + if runtime.GOOS == constant.OS_WINDOWS { + assert.Equal(t, name, "tmp\\nacos@@providers&&org.apache.dubbo.UserProvider&&hangzhou") + } else { + assert.Equal(t, name, "tmp/nacos@@providers:org.apache.dubbo.UserProvider:hangzhou") + } +} diff --git a/common/constant/const.go b/common/constant/const.go index e356ad3b..f7f50439 100644 --- a/common/constant/const.go +++ b/common/constant/const.go @@ -75,4 +75,6 @@ const ( NAMING_INSTANCE_ID_SPLITTER = "#" DefaultClientErrorCode = "SDK.NacosError" DEFAULT_SERVER_SCHEME = "http" + WINDOWS_LEGAL_NAME_SPLITER = "&&" + OS_WINDOWS = "windows" )