// ------------------------------------------------------------------------ // Project active2 // Active Thing (activething.com) git.activething.com/go // // File name field_extractor.go // Created by DEV // Modified 31/03/2024 // // Copyright 2024 activething.com // ------------------------------------------------------------------------ // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // ------------------------------------------------------------------------ package errs import "time" type ( FormatExtractor interface { Extract (builder FormatBuilder, err error) bool } FormatExtractorFnc func (builder FormatBuilder, err error) bool FormatExtractors []FormatExtractor ) func (f FormatExtractorFnc) Extract (builder FormatBuilder, err error) bool { return f(builder,err) } func (l FormatExtractors) Extract (builder FormatBuilder, err error) bool { if err == nil || len(l) == 0 { return true } for _,f := range l { if f != nil { if !f.Extract(builder,err) { return false } } } return true } func (l FormatExtractors) Append (exf ...FormatExtractor) FormatExtractors { return append(l,exf...) } func DefaultsExtractors () FormatExtractors { return []FormatExtractor{ FormatExtractorFnc(func (b FormatBuilder, e error) bool { if ms,is := e.(interface { Code() string }); is { b.AddFieldString("code",ms.Code()) } return true }), FormatExtractorFnc(func (b FormatBuilder, e error) bool { if ms,is := e.(interface { Message() string }); is { b.AddFieldString("message",ms.Message()) } return true }), FormatExtractorFnc(func (b FormatBuilder, e error) bool { if in,is := e.(interface { Info () string }); is { b.AddFieldString("info",in.Info()) } return true }), FormatExtractorFnc(func (b FormatBuilder, e error) bool { if tm,is := e.(interface { Time() time.Time }); is { b.AddFieldTime("time",tm.Time()) } return true }), FormatExtractorFnc(func (b FormatBuilder, e error) bool { if tm,is := e.(interface { Date() time.Time }); is { b.AddFieldDate("date",tm.Date()) } return true }), } }