Pārlūkot izejas kodu

refactor(config): 将 FileSource 类型移动到新文件 file.go

- 在 config 目录下新建 file.go 文件,定义 FileSource 类型
- 从 source.go 文件中移除 FileSource 相关代码
- 优化项目结构,使代码更清晰、模块化
lihf 2 mēneši atpakaļ
vecāks
revīzija
a3e11ce4c1
2 mainītis faili ar 31 papildinājumiem un 28 dzēšanām
  1. 31 0
      config/file.go
  2. 0 28
      config/source.go

+ 31 - 0
config/file.go

@@ -0,0 +1,31 @@
+package config
+
+import (
+	"fmt"
+	"github.com/go-kratos/kratos/v2/config"
+	"github.com/go-kratos/kratos/v2/config/file"
+)
+
+type FileSource struct {
+	Format Format
+	Path   string
+}
+
+func (s *FileSource) NewSource() (config.Source, error) {
+	source := file.NewSource(s.Path)
+	return source, nil
+}
+
+func (s *FileSource) Validate() bool {
+	if !s.Format.Validate() {
+		return false
+	}
+	if s.Path == "" {
+		return false
+	}
+	return true
+}
+
+func (s *FileSource) String() string {
+	return fmt.Sprintf("file source format:%v, path:%v", s.Format, s.Path)
+}

+ 0 - 28
config/source.go

@@ -1,9 +1,7 @@
 package config
 
 import (
-	"fmt"
 	"github.com/go-kratos/kratos/v2/config"
-	"github.com/go-kratos/kratos/v2/config/file"
 )
 
 type Source interface {
@@ -30,29 +28,3 @@ func (f Format) Validate() bool {
 	_, ok := formatMap[f]
 	return ok
 }
-
-// =======================================
-
-type FileSource struct {
-	Format Format
-	Path   string
-}
-
-func (s *FileSource) NewSource() (config.Source, error) {
-	source := file.NewSource(s.Path)
-	return source, nil
-}
-
-func (s *FileSource) Validate() bool {
-	if !s.Format.Validate() {
-		return false
-	}
-	if s.Path == "" {
-		return false
-	}
-	return true
-}
-
-func (s *FileSource) String() string {
-	return fmt.Sprintf("file source format:%v, path:%v", s.Format, s.Path)
-}