MCPcopy Index your code
hub / github.com/httprunner/httprunner / LoadFile

Function LoadFile

hrp/internal/builtin/utils.go:239–270  ·  view source on GitHub ↗

LoadFile loads file content with file extension and assigns to structObj

(path string, structObj interface{})

Source from the content-addressed store, hash-verified

237
238// LoadFile loads file content with file extension and assigns to structObj
239func LoadFile(path string, structObj interface{}) (err error) {
240 log.Info().Str("path", path).Msg("load file")
241 file, err := ReadFile(path)
242 if err != nil {
243 return errors.Wrap(err, "read file failed")
244 }
245 // remove BOM at the beginning of file
246 file = bytes.TrimLeft(file, "\xef\xbb\xbf")
247 ext := filepath.Ext(path)
248 switch ext {
249 case ".json", ".har":
250 decoder := json.NewDecoder(bytes.NewReader(file))
251 decoder.UseNumber()
252 err = decoder.Decode(structObj)
253 if err != nil {
254 err = errors.Wrap(code.LoadJSONError, err.Error())
255 }
256 case ".yaml", ".yml":
257 err = yaml.Unmarshal(file, structObj)
258 if err != nil {
259 err = errors.Wrap(code.LoadYAMLError, err.Error())
260 }
261 case ".env":
262 err = parseEnvContent(file, structObj)
263 if err != nil {
264 err = errors.Wrap(code.LoadEnvError, err.Error())
265 }
266 default:
267 err = code.UnsupportedFileExtension
268 }
269 return err
270}
271
272func parseEnvContent(file []byte, obj interface{}) error {
273 envMap := obj.(map[string]string)

Callers 12

ToTestCaseMethod · 0.92
toTestCaseMethod · 0.92
TestLoadCaseFunction · 0.92
ToAPIMethod · 0.92
loadCasePostmanFunction · 0.92
LoadJSONCaseFunction · 0.92
LoadYAMLCaseFunction · 0.92
loadCaseHARFunction · 0.92
LoadSwaggerCaseFunction · 0.92
overrideWithProfileMethod · 0.92
boom.goFile · 0.92
makeHRPBoomerFunction · 0.92

Calls 3

ReadFileFunction · 0.85
parseEnvContentFunction · 0.85
UnmarshalMethod · 0.65

Tested by 1

TestLoadCaseFunction · 0.74