// ------------------------------------------------------------------------ // Project atila // Active Thing (activething.com) git.activething.com/go // // File name f.go // Created by DEV // Modified 29/01/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 packs import ( "strings" "time" ) const ( FlagOmitEmpty Flags = 1 << iota FlagQuote FlagScapeHTML FlagBinary64 Flags = 0b0000100000000000 FlagBinary32 Flags = 0b0000010000000000 FlagDateRFC822 Flags = 0b0001000000000000 FlagDateUnix Flags = 0b0010000000000000 FlagDateANSIC Flags = 0b0100000000000000 FlagDateRFC850 Flags = 0b1000000000000000 binMask = 0b0000110000000000 dateMask = 0b1111000000000000 flagMaskBin Flags = 0b1111001111111111 flagMaskDate Flags = 0b0000111111111111 ) type Flags uint16 func (f Flags) OmitEmpty () bool { return f&FlagOmitEmpty != 0 } func (f Flags) Quote () bool { return f&FlagQuote != 0 } func (f Flags) EscapeHTML () bool { return f&FlagScapeHTML != 0 } func (f Flags) Binary () bool { return f&(FlagBinary64|FlagBinary32) != 0 } func (f Flags) Bin64 () bool { return f&FlagBinary64 != 0 } func (f Flags) Bin32 () bool { return f&FlagBinary32 != 0 } func (f Flags) SetWithName(flag string) Flags { switch strings.ToLower(flag) { case "o","omitempty": return f | FlagOmitEmpty case "q","quote" : return f | FlagQuote case "h","html" : return f | FlagScapeHTML // formato binario case "b64","bin64" : return (f & flagMaskBin) | FlagBinary64 case "b32","bin32" : return (f & flagMaskBin) | FlagBinary32 // formatos de fechas case "unix" : return (f & flagMaskDate) | FlagDateUnix case "rfc822" : return (f & flagMaskDate) | FlagDateRFC822 case "ansic" : return (f & flagMaskDate) | FlagDateANSIC case "rfc850" : return (f & flagMaskDate) | FlagDateRFC850 case "rfc822z": return f & flagMaskDate default: return f } } func (f Flags) SetWithNames(flags ... string) Flags { nf := f for _,n := range flags { switch strings.ToLower(n) { case "o","omitempty": nf |= FlagOmitEmpty case "q","quote" : nf |= FlagQuote case "h","html" : nf |= FlagScapeHTML // formatos binarios case "b32","bin32" : nf = (nf & flagMaskBin) | FlagBinary32 case "b64","bin64" : nf = (nf & flagMaskBin) | FlagBinary64 // formatos de fechas case "unix" : nf = (nf & flagMaskDate) | FlagDateUnix case "rfc822" : nf = (nf & flagMaskDate) | FlagDateRFC822 case "ansic" : nf = (nf & flagMaskDate) | FlagDateANSIC case "rfc850" : nf = (nf & flagMaskDate) | FlagDateRFC850 case "rfc822z": nf &= flagMaskDate } } return nf } func (f Flags) SetWithString (flags string) Flags { if len(flags) == 0 { return f } nf := f for flags != "" { var op string op,flags,_ = strings.Cut(flags,",") nf |= f.SetWithName(op) } return nf } func (f Flags) Names () string { var ls []string if f&FlagOmitEmpty != 0 { ls = append(ls,"omitempty")} if f&FlagQuote != 0 { ls = append(ls,"quote")} if f&FlagScapeHTML != 0 { ls = append(ls,"html")} if f&flagMaskBin != 0 { ls = append(ls,f.binName()) } if f&dateMask != 0 { ls = append(ls,f.dateName()) } if len(ls) > 0 { return strings.Join(ls,",") } return "" } func (f Flags) binName () string { switch f & binMask { case FlagBinary64 : return "b64" case FlagBinary32 : return "b32" } return "" } func (f Flags) dateName() string { switch f & dateMask { case FlagDateUnix : return "unix" case FlagDateRFC822: return "rfc822" case FlagDateANSIC : return "ansic" case FlagDateRFC850: return "rfc8550" default: return time.RFC822Z } } func (f Flags) DateFormat () string { switch f & dateMask { case FlagDateUnix : return time.UnixDate case FlagDateRFC822: return time.RFC822 case FlagDateANSIC : return time.ANSIC case FlagDateRFC850: return time.RFC850 default: return time.RFC822Z } }