// Copyright 2024 Matthew Rich . All rights reserved. package folio import ( "errors" "encoding/json" "gopkg.in/yaml.v3" "log/slog" ) var ( ErrInvalidOnErrorStrategy = errors.New("Invalid OnError strategy") ) type OnError string const ( OnErrorStop = "stop" OnErrorFail = "fail" // set error value and return the error OnErrorSkip = "skip" // set error value and continue processing resources ) func NewOnError() OnError { return OnErrorFail } func (o OnError) Strategy() string { switch o { case OnErrorStop, OnErrorFail, OnErrorSkip: return string(o) } return "" } func (o OnError) GetStrategy() OnError { switch o { case OnErrorStop, OnErrorFail, OnErrorSkip: return o default: slog.Warn("OnError.GetStrategy() - invalid value, defaulting to OnErrorFail", "strategy", o) return OnErrorFail } } func (o OnError) Validate() error { switch o { case OnErrorStop, OnErrorFail, OnErrorSkip: return nil default: return ErrInvalidOnErrorStrategy } } func (o *OnError) UnmarshalValue(value string) (err error) { if err = OnError(value).Validate(); err == nil { *o = OnError(value) } return } func (o *OnError) UnmarshalJSON(jsonData []byte) error { var s string if unmarshalErr := json.Unmarshal(jsonData, &s); unmarshalErr != nil { return unmarshalErr } return o.UnmarshalValue(s) } func (o *OnError) UnmarshalYAML(value *yaml.Node) error { var s string if err := value.Decode(&s); err != nil { return err } return o.UnmarshalValue(s) }