gitee.com/sy_183/go-common@v1.0.5-0.20231205030221-958cfe129b47/config/file-parser.go (about) 1 package config 2 3 import ( 4 "fmt" 5 "gitee.com/sy_183/go-common/unit" 6 "os" 7 ) 8 9 const MaxConfigFileSize = 32 * 1024 * 1024 10 11 var ConfigSizeTooLargeError = fmt.Errorf("配置文件大小过大,超过限定大小(%s)", unit.Size(MaxConfigFileSize)) 12 13 type fileParser struct { 14 path string 15 typ Type 16 } 17 18 func (p *fileParser) Unmarshal(c interface{}) error { 19 info, err := os.Stat(p.path) 20 if err != nil { 21 return err 22 } 23 if info.Size() > MaxConfigFileSize { 24 return ConfigSizeTooLargeError 25 } 26 data, err := os.ReadFile(p.path) 27 if err != nil { 28 return err 29 } 30 bp := &bytesParser{bytes: data, typ: p.typ} 31 return bp.Unmarshal(c) 32 }