// Copyright 2024 Matthew Rich . All rights reserved. package resource import ( "errors" "encoding/json" "gopkg.in/yaml.v3" ) var ( ErrInvalidOnErrorStrategy = errors.New("Invalid OnError strategy") ) type OnError string const ( OnErrorStop = "stop" OnErrorFail = "fail" OnErrorSkip = "skip" ) func NewOnError() OnError { return OnErrorFail } func (o OnError) Strategy() string { switch o { case OnErrorStop, OnErrorFail, OnErrorSkip: return string(o) } return "" } 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) }