Skip to content

Commit

Permalink
fix:修复windows文件名称问题 (#205)
Browse files Browse the repository at this point in the history
* fix:修复windows文件名称问题
Co-authored-by: sanxun0325 <[email protected]>
  • Loading branch information
zhaoyunxing92 authored Apr 8, 2021
1 parent 54f875c commit c9952e6
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
8 changes: 8 additions & 0 deletions clients/cache/disk_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,24 @@ 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"
"github.com/nacos-group/nacos-sdk-go/util"
)

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
}

Expand Down
36 changes: 36 additions & 0 deletions clients/cache/disk_cache_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
}
2 changes: 2 additions & 0 deletions common/constant/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,6 @@ const (
NAMING_INSTANCE_ID_SPLITTER = "#"
DefaultClientErrorCode = "SDK.NacosError"
DEFAULT_SERVER_SCHEME = "http"
WINDOWS_LEGAL_NAME_SPLITER = "&&"
OS_WINDOWS = "windows"
)

0 comments on commit c9952e6

Please sign in to comment.