mirror of
https://github.com/aykhans/dodo.git
synced 2025-09-05 18:44:21 +00:00
🎉first commit
This commit is contained in:
33
readers/json.go
Normal file
33
readers/json.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package readers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
|
||||
"github.com/aykhans/dodo/config"
|
||||
"github.com/aykhans/dodo/custom_errors"
|
||||
)
|
||||
|
||||
func JSONConfigReader(filePath string) (*config.JSONConfig, error) {
|
||||
data, err := os.ReadFile(filePath)
|
||||
if err != nil {
|
||||
return nil, customerrors.OSErrorFormater(err)
|
||||
}
|
||||
jsonConf := &config.JSONConfig{}
|
||||
err = json.Unmarshal(data, &jsonConf)
|
||||
|
||||
if err != nil {
|
||||
switch err := err.(type) {
|
||||
case *json.UnmarshalTypeError:
|
||||
return nil,
|
||||
customerrors.NewTypeError(
|
||||
err.Type.String(),
|
||||
err.Value,
|
||||
err.Field,
|
||||
err,
|
||||
)
|
||||
}
|
||||
return nil, customerrors.NewInvalidFileError(filePath, err)
|
||||
}
|
||||
return jsonConf, nil
|
||||
}
|
Reference in New Issue
Block a user