mirror of
https://github.com/aykhans/dodo.git
synced 2025-04-22 19:42:14 +00:00
34 lines
665 B
Go
34 lines
665 B
Go
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
|
|
}
|