Compare commits
	
		
			13 Commits
		
	
	
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| d2bb362135 | |||
| 9dd81f6bd5 | |||
| d2c04afcd5 | |||
| 62980e1489 | |||
| 59963adf74 | |||
| 194ea4ace5 | |||
| 73b80a66bc | |||
| d8b2d01274 | |||
| bfa8457e95 | |||
| 70106733d9 | |||
| ce7837b9ef | |||
| d0d72167eb | |||
| a55ee1a6ce | 
| @@ -1,94 +1,14 @@ | |||||||
| package exerr | package exerr | ||||||
|  |  | ||||||
| import ( | type Method string | ||||||
| 	"gogs.mikescher.com/BlackForestBytes/goext/dataext" |  | ||||||
| 	"gogs.mikescher.com/BlackForestBytes/goext/langext" | const ( | ||||||
|  | 	MethodOutput Method = "OUTPUT" | ||||||
|  | 	MethodPrint  Method = "PRINT" | ||||||
|  | 	MethodBuild  Method = "BUILD" | ||||||
|  | 	MethodFatal  Method = "FATAL" | ||||||
| ) | ) | ||||||
|  |  | ||||||
| type ErrorCategory struct{ Category string } |  | ||||||
|  |  | ||||||
| var ( |  | ||||||
| 	CatWrap    = ErrorCategory{"Wrap"}    // The error is simply wrapping another error (e.g. when a grpc call returns an error) |  | ||||||
| 	CatSystem  = ErrorCategory{"System"}  // An internal system error (e.g. connection to db failed) |  | ||||||
| 	CatUser    = ErrorCategory{"User"}    // The user (the API caller) did something wrong (e.g. he has no permissions to do this) |  | ||||||
| 	CatForeign = ErrorCategory{"Foreign"} // A foreign error that some component threw (e.g. an unknown mongodb error), happens if we call Wrap(..) on an non-bmerror value |  | ||||||
| ) |  | ||||||
|  |  | ||||||
| //goland:noinspection GoUnusedGlobalVariable |  | ||||||
| var AllCategories = []ErrorCategory{CatWrap, CatSystem, CatUser, CatForeign} |  | ||||||
|  |  | ||||||
| type ErrorSeverity struct{ Severity string } |  | ||||||
|  |  | ||||||
| var ( |  | ||||||
| 	SevTrace = ErrorSeverity{"Trace"} |  | ||||||
| 	SevDebug = ErrorSeverity{"Debug"} |  | ||||||
| 	SevInfo  = ErrorSeverity{"Info"} |  | ||||||
| 	SevWarn  = ErrorSeverity{"Warn"} |  | ||||||
| 	SevErr   = ErrorSeverity{"Err"} |  | ||||||
| 	SevFatal = ErrorSeverity{"Fatal"} |  | ||||||
| ) |  | ||||||
|  |  | ||||||
| //goland:noinspection GoUnusedGlobalVariable |  | ||||||
| var AllSeverities = []ErrorSeverity{SevTrace, SevDebug, SevInfo, SevWarn, SevErr, SevFatal} |  | ||||||
|  |  | ||||||
| type ErrorType struct { |  | ||||||
| 	Key               string |  | ||||||
| 	DefaultStatusCode *int |  | ||||||
| } |  | ||||||
|  |  | ||||||
| //goland:noinspection GoUnusedGlobalVariable |  | ||||||
| var ( |  | ||||||
| 	TypeInternal       = NewType("INTERNAL_ERROR", langext.Ptr(500)) |  | ||||||
| 	TypePanic          = NewType("PANIC", langext.Ptr(500)) |  | ||||||
| 	TypeNotImplemented = NewType("NOT_IMPLEMENTED", langext.Ptr(500)) |  | ||||||
|  |  | ||||||
| 	TypeMongoQuery        = NewType("MONGO_QUERY", langext.Ptr(500)) |  | ||||||
| 	TypeCursorTokenDecode = NewType("CURSOR_TOKEN_DECODE", langext.Ptr(500)) |  | ||||||
| 	TypeMongoFilter       = NewType("MONGO_FILTER", langext.Ptr(500)) |  | ||||||
| 	TypeMongoReflection   = NewType("MONGO_REFLECTION", langext.Ptr(500)) |  | ||||||
| 	TypeMongoInvalidOpt   = NewType("MONGO_INVALIDOPT", langext.Ptr(500)) |  | ||||||
|  |  | ||||||
| 	TypeSQLQuery  = NewType("SQL_QUERY", langext.Ptr(500)) |  | ||||||
| 	TypeSQLBuild  = NewType("SQL_BUILD", langext.Ptr(500)) |  | ||||||
| 	TypeSQLDecode = NewType("SQL_DECODE", langext.Ptr(500)) |  | ||||||
|  |  | ||||||
| 	TypeWrap = NewType("Wrap", nil) |  | ||||||
|  |  | ||||||
| 	TypeBindFailURI      = NewType("BINDFAIL_URI", langext.Ptr(400)) |  | ||||||
| 	TypeBindFailQuery    = NewType("BINDFAIL_QUERY", langext.Ptr(400)) |  | ||||||
| 	TypeBindFailJSON     = NewType("BINDFAIL_JSON", langext.Ptr(400)) |  | ||||||
| 	TypeBindFailFormData = NewType("BINDFAIL_FORMDATA", langext.Ptr(400)) |  | ||||||
| 	TypeBindFailHeader   = NewType("BINDFAIL_HEADER", langext.Ptr(400)) |  | ||||||
|  |  | ||||||
| 	TypeMarshalEntityID = NewType("MARSHAL_ENTITY_ID", langext.Ptr(400)) |  | ||||||
| 	TypeInvalidCSID     = NewType("INVALID_CSID", langext.Ptr(400)) |  | ||||||
|  |  | ||||||
| 	TypeGoogleStatuscode = NewType("GOOGLE_STATUSCODE", langext.Ptr(400)) |  | ||||||
| 	TypeGoogleResponse   = NewType("GOOGLE_RESPONSE", langext.Ptr(400)) |  | ||||||
|  |  | ||||||
| 	TypeUnauthorized = NewType("UNAUTHORIZED", langext.Ptr(401)) |  | ||||||
| 	TypeAuthFailed   = NewType("AUTH_FAILED", langext.Ptr(401)) |  | ||||||
|  |  | ||||||
| 	TypeInvalidImage    = NewType("IMAGEEXT_INVALID_IMAGE", langext.Ptr(400)) |  | ||||||
| 	TypeInvalidMimeType = NewType("IMAGEEXT_INVALID_MIMETYPE", langext.Ptr(400)) |  | ||||||
|  |  | ||||||
| 	// other values come from the downstream application that uses goext |  | ||||||
| ) |  | ||||||
|  |  | ||||||
| var registeredTypes = dataext.SyncMap[string, ErrorType]{} |  | ||||||
|  |  | ||||||
| func NewType(key string, defStatusCode *int) ErrorType { |  | ||||||
| 	et := ErrorType{key, defStatusCode} |  | ||||||
|  |  | ||||||
| 	registeredTypes.Set(key, et) |  | ||||||
|  |  | ||||||
| 	return et |  | ||||||
| } |  | ||||||
|  |  | ||||||
| func ListRegisteredTypes() []ErrorType { |  | ||||||
| 	return registeredTypes.GetAllValues() |  | ||||||
| } |  | ||||||
|  |  | ||||||
| type LogPrintLevel string | type LogPrintLevel string | ||||||
|  |  | ||||||
| const ( | const ( | ||||||
|   | |||||||
							
								
								
									
										89
									
								
								exerr/dataCategory.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										89
									
								
								exerr/dataCategory.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,89 @@ | |||||||
|  | package exerr | ||||||
|  |  | ||||||
|  | import ( | ||||||
|  | 	"encoding/json" | ||||||
|  | 	"errors" | ||||||
|  | 	"fmt" | ||||||
|  | 	"go.mongodb.org/mongo-driver/bson" | ||||||
|  | 	"go.mongodb.org/mongo-driver/bson/bsoncodec" | ||||||
|  | 	"go.mongodb.org/mongo-driver/bson/bsonrw" | ||||||
|  | 	"go.mongodb.org/mongo-driver/bson/bsontype" | ||||||
|  | 	"reflect" | ||||||
|  | ) | ||||||
|  |  | ||||||
|  | type ErrorCategory struct{ Category string } | ||||||
|  |  | ||||||
|  | var ( | ||||||
|  | 	CatWrap    = ErrorCategory{"Wrap"}    // The error is simply wrapping another error (e.g. when a grpc call returns an error) | ||||||
|  | 	CatSystem  = ErrorCategory{"System"}  // An internal system error (e.g. connection to db failed) | ||||||
|  | 	CatUser    = ErrorCategory{"User"}    // The user (the API caller) did something wrong (e.g. he has no permissions to do this) | ||||||
|  | 	CatForeign = ErrorCategory{"Foreign"} // A foreign error that some component threw (e.g. an unknown mongodb error), happens if we call Wrap(..) on an non-bmerror value | ||||||
|  | ) | ||||||
|  |  | ||||||
|  | func (e *ErrorCategory) UnmarshalJSON(bytes []byte) error { | ||||||
|  | 	return json.Unmarshal(bytes, &e.Category) | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func (e ErrorCategory) MarshalJSON() ([]byte, error) { | ||||||
|  | 	return json.Marshal(e.Category) | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func (e *ErrorCategory) UnmarshalBSONValue(bt bsontype.Type, data []byte) error { | ||||||
|  | 	if bt == bson.TypeNull { | ||||||
|  | 		// we can't set nil in UnmarshalBSONValue (so we use default(struct)) | ||||||
|  | 		// Use mongoext.CreateGoExtBsonRegistry if you need to unmarsh pointer values | ||||||
|  | 		// https://stackoverflow.com/questions/75167597 | ||||||
|  | 		// https://jira.mongodb.org/browse/GODRIVER-2252 | ||||||
|  | 		*e = ErrorCategory{} | ||||||
|  | 		return nil | ||||||
|  | 	} | ||||||
|  | 	if bt != bson.TypeString { | ||||||
|  | 		return errors.New(fmt.Sprintf("cannot unmarshal %v into String", bt)) | ||||||
|  | 	} | ||||||
|  | 	var tt string | ||||||
|  | 	err := bson.RawValue{Type: bt, Value: data}.Unmarshal(&tt) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return err | ||||||
|  | 	} | ||||||
|  | 	*e = ErrorCategory{tt} | ||||||
|  | 	return nil | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func (e ErrorCategory) MarshalBSONValue() (bsontype.Type, []byte, error) { | ||||||
|  | 	return bson.MarshalValue(e.Category) | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func (e ErrorCategory) DecodeValue(dc bsoncodec.DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error { | ||||||
|  | 	if val.Kind() == reflect.Ptr && val.IsNil() { | ||||||
|  | 		if !val.CanSet() { | ||||||
|  | 			return errors.New("ValueUnmarshalerDecodeValue") | ||||||
|  | 		} | ||||||
|  | 		val.Set(reflect.New(val.Type().Elem())) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	tp, src, err := bsonrw.Copier{}.CopyValueToBytes(vr) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return err | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	if val.Kind() == reflect.Ptr && len(src) == 0 { | ||||||
|  | 		val.Set(reflect.Zero(val.Type())) | ||||||
|  | 		return nil | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	err = e.UnmarshalBSONValue(tp, src) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return err | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	if val.Kind() == reflect.Ptr { | ||||||
|  | 		val.Set(reflect.ValueOf(&e)) | ||||||
|  | 	} else { | ||||||
|  | 		val.Set(reflect.ValueOf(e)) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	return nil | ||||||
|  | } | ||||||
|  |  | ||||||
|  | //goland:noinspection GoUnusedGlobalVariable | ||||||
|  | var AllCategories = []ErrorCategory{CatWrap, CatSystem, CatUser, CatForeign} | ||||||
							
								
								
									
										91
									
								
								exerr/dataSeverity.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										91
									
								
								exerr/dataSeverity.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,91 @@ | |||||||
|  | package exerr | ||||||
|  |  | ||||||
|  | import ( | ||||||
|  | 	"encoding/json" | ||||||
|  | 	"errors" | ||||||
|  | 	"fmt" | ||||||
|  | 	"go.mongodb.org/mongo-driver/bson" | ||||||
|  | 	"go.mongodb.org/mongo-driver/bson/bsoncodec" | ||||||
|  | 	"go.mongodb.org/mongo-driver/bson/bsonrw" | ||||||
|  | 	"go.mongodb.org/mongo-driver/bson/bsontype" | ||||||
|  | 	"reflect" | ||||||
|  | ) | ||||||
|  |  | ||||||
|  | type ErrorSeverity struct{ Severity string } | ||||||
|  |  | ||||||
|  | var ( | ||||||
|  | 	SevTrace = ErrorSeverity{"Trace"} | ||||||
|  | 	SevDebug = ErrorSeverity{"Debug"} | ||||||
|  | 	SevInfo  = ErrorSeverity{"Info"} | ||||||
|  | 	SevWarn  = ErrorSeverity{"Warn"} | ||||||
|  | 	SevErr   = ErrorSeverity{"Err"} | ||||||
|  | 	SevFatal = ErrorSeverity{"Fatal"} | ||||||
|  | ) | ||||||
|  |  | ||||||
|  | func (e *ErrorSeverity) UnmarshalJSON(bytes []byte) error { | ||||||
|  | 	return json.Unmarshal(bytes, &e.Severity) | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func (e ErrorSeverity) MarshalJSON() ([]byte, error) { | ||||||
|  | 	return json.Marshal(e.Severity) | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func (e *ErrorSeverity) UnmarshalBSONValue(bt bsontype.Type, data []byte) error { | ||||||
|  | 	if bt == bson.TypeNull { | ||||||
|  | 		// we can't set nil in UnmarshalBSONValue (so we use default(struct)) | ||||||
|  | 		// Use mongoext.CreateGoExtBsonRegistry if you need to unmarsh pointer values | ||||||
|  | 		// https://stackoverflow.com/questions/75167597 | ||||||
|  | 		// https://jira.mongodb.org/browse/GODRIVER-2252 | ||||||
|  | 		*e = ErrorSeverity{} | ||||||
|  | 		return nil | ||||||
|  | 	} | ||||||
|  | 	if bt != bson.TypeString { | ||||||
|  | 		return errors.New(fmt.Sprintf("cannot unmarshal %v into String", bt)) | ||||||
|  | 	} | ||||||
|  | 	var tt string | ||||||
|  | 	err := bson.RawValue{Type: bt, Value: data}.Unmarshal(&tt) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return err | ||||||
|  | 	} | ||||||
|  | 	*e = ErrorSeverity{tt} | ||||||
|  | 	return nil | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func (e ErrorSeverity) MarshalBSONValue() (bsontype.Type, []byte, error) { | ||||||
|  | 	return bson.MarshalValue(e.Severity) | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func (e ErrorSeverity) DecodeValue(dc bsoncodec.DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error { | ||||||
|  | 	if val.Kind() == reflect.Ptr && val.IsNil() { | ||||||
|  | 		if !val.CanSet() { | ||||||
|  | 			return errors.New("ValueUnmarshalerDecodeValue") | ||||||
|  | 		} | ||||||
|  | 		val.Set(reflect.New(val.Type().Elem())) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	tp, src, err := bsonrw.Copier{}.CopyValueToBytes(vr) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return err | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	if val.Kind() == reflect.Ptr && len(src) == 0 { | ||||||
|  | 		val.Set(reflect.Zero(val.Type())) | ||||||
|  | 		return nil | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	err = e.UnmarshalBSONValue(tp, src) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return err | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	if val.Kind() == reflect.Ptr { | ||||||
|  | 		val.Set(reflect.ValueOf(&e)) | ||||||
|  | 	} else { | ||||||
|  | 		val.Set(reflect.ValueOf(e)) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	return nil | ||||||
|  | } | ||||||
|  |  | ||||||
|  | //goland:noinspection GoUnusedGlobalVariable | ||||||
|  | var AllSeverities = []ErrorSeverity{SevTrace, SevDebug, SevInfo, SevWarn, SevErr, SevFatal} | ||||||
							
								
								
									
										155
									
								
								exerr/dataType.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										155
									
								
								exerr/dataType.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,155 @@ | |||||||
|  | package exerr | ||||||
|  |  | ||||||
|  | import ( | ||||||
|  | 	"encoding/json" | ||||||
|  | 	"errors" | ||||||
|  | 	"fmt" | ||||||
|  | 	"go.mongodb.org/mongo-driver/bson" | ||||||
|  | 	"go.mongodb.org/mongo-driver/bson/bsoncodec" | ||||||
|  | 	"go.mongodb.org/mongo-driver/bson/bsonrw" | ||||||
|  | 	"go.mongodb.org/mongo-driver/bson/bsontype" | ||||||
|  | 	"gogs.mikescher.com/BlackForestBytes/goext/dataext" | ||||||
|  | 	"gogs.mikescher.com/BlackForestBytes/goext/langext" | ||||||
|  | 	"reflect" | ||||||
|  | ) | ||||||
|  |  | ||||||
|  | type ErrorType struct { | ||||||
|  | 	Key               string | ||||||
|  | 	DefaultStatusCode *int | ||||||
|  | } | ||||||
|  |  | ||||||
|  | //goland:noinspection GoUnusedGlobalVariable | ||||||
|  | var ( | ||||||
|  | 	TypeInternal       = NewType("INTERNAL_ERROR", langext.Ptr(500)) | ||||||
|  | 	TypePanic          = NewType("PANIC", langext.Ptr(500)) | ||||||
|  | 	TypeNotImplemented = NewType("NOT_IMPLEMENTED", langext.Ptr(500)) | ||||||
|  |  | ||||||
|  | 	TypeMongoQuery        = NewType("MONGO_QUERY", langext.Ptr(500)) | ||||||
|  | 	TypeCursorTokenDecode = NewType("CURSOR_TOKEN_DECODE", langext.Ptr(500)) | ||||||
|  | 	TypeMongoFilter       = NewType("MONGO_FILTER", langext.Ptr(500)) | ||||||
|  | 	TypeMongoReflection   = NewType("MONGO_REFLECTION", langext.Ptr(500)) | ||||||
|  | 	TypeMongoInvalidOpt   = NewType("MONGO_INVALIDOPT", langext.Ptr(500)) | ||||||
|  |  | ||||||
|  | 	TypeSQLQuery  = NewType("SQL_QUERY", langext.Ptr(500)) | ||||||
|  | 	TypeSQLBuild  = NewType("SQL_BUILD", langext.Ptr(500)) | ||||||
|  | 	TypeSQLDecode = NewType("SQL_DECODE", langext.Ptr(500)) | ||||||
|  |  | ||||||
|  | 	TypeWrap = NewType("Wrap", nil) | ||||||
|  |  | ||||||
|  | 	TypeBindFailURI      = NewType("BINDFAIL_URI", langext.Ptr(400)) | ||||||
|  | 	TypeBindFailQuery    = NewType("BINDFAIL_QUERY", langext.Ptr(400)) | ||||||
|  | 	TypeBindFailJSON     = NewType("BINDFAIL_JSON", langext.Ptr(400)) | ||||||
|  | 	TypeBindFailFormData = NewType("BINDFAIL_FORMDATA", langext.Ptr(400)) | ||||||
|  | 	TypeBindFailHeader   = NewType("BINDFAIL_HEADER", langext.Ptr(400)) | ||||||
|  |  | ||||||
|  | 	TypeMarshalEntityID = NewType("MARSHAL_ENTITY_ID", langext.Ptr(400)) | ||||||
|  | 	TypeInvalidCSID     = NewType("INVALID_CSID", langext.Ptr(400)) | ||||||
|  |  | ||||||
|  | 	TypeGoogleStatuscode = NewType("GOOGLE_STATUSCODE", langext.Ptr(400)) | ||||||
|  | 	TypeGoogleResponse   = NewType("GOOGLE_RESPONSE", langext.Ptr(400)) | ||||||
|  |  | ||||||
|  | 	TypeUnauthorized = NewType("UNAUTHORIZED", langext.Ptr(401)) | ||||||
|  | 	TypeAuthFailed   = NewType("AUTH_FAILED", langext.Ptr(401)) | ||||||
|  |  | ||||||
|  | 	TypeInvalidImage    = NewType("IMAGEEXT_INVALID_IMAGE", langext.Ptr(400)) | ||||||
|  | 	TypeInvalidMimeType = NewType("IMAGEEXT_INVALID_MIMETYPE", langext.Ptr(400)) | ||||||
|  |  | ||||||
|  | 	// other values come from the downstream application that uses goext | ||||||
|  | ) | ||||||
|  |  | ||||||
|  | func (e *ErrorType) UnmarshalJSON(bytes []byte) error { | ||||||
|  | 	var k string | ||||||
|  | 	err := json.Unmarshal(bytes, &k) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return err | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	if d, ok := registeredTypes.Get(k); ok { | ||||||
|  | 		*e = d | ||||||
|  | 		return nil | ||||||
|  | 	} else { | ||||||
|  | 		*e = ErrorType{k, nil} | ||||||
|  | 		return nil | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func (e ErrorType) MarshalJSON() ([]byte, error) { | ||||||
|  | 	return json.Marshal(e.Key) | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func (e *ErrorType) UnmarshalBSONValue(bt bsontype.Type, data []byte) error { | ||||||
|  | 	if bt == bson.TypeNull { | ||||||
|  | 		// we can't set nil in UnmarshalBSONValue (so we use default(struct)) | ||||||
|  | 		// Use mongoext.CreateGoExtBsonRegistry if you need to unmarsh pointer values | ||||||
|  | 		// https://stackoverflow.com/questions/75167597 | ||||||
|  | 		// https://jira.mongodb.org/browse/GODRIVER-2252 | ||||||
|  | 		*e = ErrorType{} | ||||||
|  | 		return nil | ||||||
|  | 	} | ||||||
|  | 	if bt != bson.TypeString { | ||||||
|  | 		return errors.New(fmt.Sprintf("cannot unmarshal %v into String", bt)) | ||||||
|  | 	} | ||||||
|  | 	var tt string | ||||||
|  | 	err := bson.RawValue{Type: bt, Value: data}.Unmarshal(&tt) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return err | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	if d, ok := registeredTypes.Get(tt); ok { | ||||||
|  | 		*e = d | ||||||
|  | 		return nil | ||||||
|  | 	} else { | ||||||
|  | 		*e = ErrorType{tt, nil} | ||||||
|  | 		return nil | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func (e ErrorType) MarshalBSONValue() (bsontype.Type, []byte, error) { | ||||||
|  | 	return bson.MarshalValue(e.Key) | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func (e ErrorType) DecodeValue(dc bsoncodec.DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error { | ||||||
|  | 	if val.Kind() == reflect.Ptr && val.IsNil() { | ||||||
|  | 		if !val.CanSet() { | ||||||
|  | 			return errors.New("ValueUnmarshalerDecodeValue") | ||||||
|  | 		} | ||||||
|  | 		val.Set(reflect.New(val.Type().Elem())) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	tp, src, err := bsonrw.Copier{}.CopyValueToBytes(vr) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return err | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	if val.Kind() == reflect.Ptr && len(src) == 0 { | ||||||
|  | 		val.Set(reflect.Zero(val.Type())) | ||||||
|  | 		return nil | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	err = e.UnmarshalBSONValue(tp, src) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return err | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	if val.Kind() == reflect.Ptr { | ||||||
|  | 		val.Set(reflect.ValueOf(&e)) | ||||||
|  | 	} else { | ||||||
|  | 		val.Set(reflect.ValueOf(e)) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	return nil | ||||||
|  | } | ||||||
|  |  | ||||||
|  | var registeredTypes = dataext.SyncMap[string, ErrorType]{} | ||||||
|  |  | ||||||
|  | func NewType(key string, defStatusCode *int) ErrorType { | ||||||
|  | 	et := ErrorType{key, defStatusCode} | ||||||
|  |  | ||||||
|  | 	registeredTypes.Set(key, et) | ||||||
|  |  | ||||||
|  | 	return et | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func ListRegisteredTypes() []ErrorType { | ||||||
|  | 	return registeredTypes.GetAllValues() | ||||||
|  | } | ||||||
							
								
								
									
										153
									
								
								exerr/data_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										153
									
								
								exerr/data_test.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,153 @@ | |||||||
|  | package exerr | ||||||
|  |  | ||||||
|  | import ( | ||||||
|  | 	"context" | ||||||
|  | 	"encoding/json" | ||||||
|  | 	"go.mongodb.org/mongo-driver/bson" | ||||||
|  | 	"go.mongodb.org/mongo-driver/bson/primitive" | ||||||
|  | 	"go.mongodb.org/mongo-driver/mongo" | ||||||
|  | 	"gogs.mikescher.com/BlackForestBytes/goext/tst" | ||||||
|  | 	"testing" | ||||||
|  | 	"time" | ||||||
|  | ) | ||||||
|  |  | ||||||
|  | func TestJSONMarshalErrorCategory(t *testing.T) { | ||||||
|  |  | ||||||
|  | 	c1 := CatSystem | ||||||
|  |  | ||||||
|  | 	jsonbin := tst.Must(json.Marshal(c1))(t) | ||||||
|  |  | ||||||
|  | 	var c2 ErrorCategory | ||||||
|  | 	tst.AssertNoErr(t, json.Unmarshal(jsonbin, &c2)) | ||||||
|  |  | ||||||
|  | 	tst.AssertEqual(t, c1, c2) | ||||||
|  |  | ||||||
|  | 	tst.AssertEqual(t, string(jsonbin), "\"System\"") | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func TestJSONMarshalErrorSeverity(t *testing.T) { | ||||||
|  |  | ||||||
|  | 	c1 := SevErr | ||||||
|  |  | ||||||
|  | 	jsonbin := tst.Must(json.Marshal(c1))(t) | ||||||
|  |  | ||||||
|  | 	var c2 ErrorSeverity | ||||||
|  | 	tst.AssertNoErr(t, json.Unmarshal(jsonbin, &c2)) | ||||||
|  |  | ||||||
|  | 	tst.AssertEqual(t, c1, c2) | ||||||
|  |  | ||||||
|  | 	tst.AssertEqual(t, string(jsonbin), "\"Err\"") | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func TestJSONMarshalErrorType(t *testing.T) { | ||||||
|  |  | ||||||
|  | 	c1 := TypeNotImplemented | ||||||
|  |  | ||||||
|  | 	jsonbin := tst.Must(json.Marshal(c1))(t) | ||||||
|  |  | ||||||
|  | 	var c2 ErrorType | ||||||
|  | 	tst.AssertNoErr(t, json.Unmarshal(jsonbin, &c2)) | ||||||
|  |  | ||||||
|  | 	tst.AssertEqual(t, c1, c2) | ||||||
|  |  | ||||||
|  | 	tst.AssertEqual(t, string(jsonbin), "\"NOT_IMPLEMENTED\"") | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func TestBSONMarshalErrorCategory(t *testing.T) { | ||||||
|  | 	ctx, cancel := context.WithTimeout(context.Background(), 350*time.Millisecond) | ||||||
|  | 	defer cancel() | ||||||
|  |  | ||||||
|  | 	client, err := mongo.Connect(ctx) | ||||||
|  | 	if err != nil { | ||||||
|  | 		t.Skip("Skip test - no local mongo found") | ||||||
|  | 		return | ||||||
|  | 	} | ||||||
|  | 	err = client.Ping(ctx, nil) | ||||||
|  | 	if err != nil { | ||||||
|  | 		t.Skip("Skip test - no local mongo found") | ||||||
|  | 		return | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	primimd := primitive.NewObjectID() | ||||||
|  |  | ||||||
|  | 	_, err = client.Database("_test").Collection("goext-cicd").InsertOne(ctx, bson.M{"_id": primimd, "val": CatSystem}) | ||||||
|  | 	tst.AssertNoErr(t, err) | ||||||
|  |  | ||||||
|  | 	cursor := client.Database("_test").Collection("goext-cicd").FindOne(ctx, bson.M{"_id": primimd, "val": bson.M{"$type": "string"}}) | ||||||
|  |  | ||||||
|  | 	var c1 struct { | ||||||
|  | 		ID  primitive.ObjectID `bson:"_id"` | ||||||
|  | 		Val ErrorCategory      `bson:"val"` | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	err = cursor.Decode(&c1) | ||||||
|  | 	tst.AssertNoErr(t, err) | ||||||
|  |  | ||||||
|  | 	tst.AssertEqual(t, c1.Val, CatSystem) | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func TestBSONMarshalErrorSeverity(t *testing.T) { | ||||||
|  | 	ctx, cancel := context.WithTimeout(context.Background(), 350*time.Millisecond) | ||||||
|  | 	defer cancel() | ||||||
|  |  | ||||||
|  | 	client, err := mongo.Connect(ctx) | ||||||
|  | 	if err != nil { | ||||||
|  | 		t.Skip("Skip test - no local mongo found") | ||||||
|  | 		return | ||||||
|  | 	} | ||||||
|  | 	err = client.Ping(ctx, nil) | ||||||
|  | 	if err != nil { | ||||||
|  | 		t.Skip("Skip test - no local mongo found") | ||||||
|  | 		return | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	primimd := primitive.NewObjectID() | ||||||
|  |  | ||||||
|  | 	_, err = client.Database("_test").Collection("goext-cicd").InsertOne(ctx, bson.M{"_id": primimd, "val": SevErr}) | ||||||
|  | 	tst.AssertNoErr(t, err) | ||||||
|  |  | ||||||
|  | 	cursor := client.Database("_test").Collection("goext-cicd").FindOne(ctx, bson.M{"_id": primimd, "val": bson.M{"$type": "string"}}) | ||||||
|  |  | ||||||
|  | 	var c1 struct { | ||||||
|  | 		ID  primitive.ObjectID `bson:"_id"` | ||||||
|  | 		Val ErrorSeverity      `bson:"val"` | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	err = cursor.Decode(&c1) | ||||||
|  | 	tst.AssertNoErr(t, err) | ||||||
|  |  | ||||||
|  | 	tst.AssertEqual(t, c1.Val, SevErr) | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func TestBSONMarshalErrorType(t *testing.T) { | ||||||
|  | 	ctx, cancel := context.WithTimeout(context.Background(), 350*time.Millisecond) | ||||||
|  | 	defer cancel() | ||||||
|  |  | ||||||
|  | 	client, err := mongo.Connect(ctx) | ||||||
|  | 	if err != nil { | ||||||
|  | 		t.Skip("Skip test - no local mongo found") | ||||||
|  | 		return | ||||||
|  | 	} | ||||||
|  | 	err = client.Ping(ctx, nil) | ||||||
|  | 	if err != nil { | ||||||
|  | 		t.Skip("Skip test - no local mongo found") | ||||||
|  | 		return | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	primimd := primitive.NewObjectID() | ||||||
|  |  | ||||||
|  | 	_, err = client.Database("_test").Collection("goext-cicd").InsertOne(ctx, bson.M{"_id": primimd, "val": TypeNotImplemented}) | ||||||
|  | 	tst.AssertNoErr(t, err) | ||||||
|  |  | ||||||
|  | 	cursor := client.Database("_test").Collection("goext-cicd").FindOne(ctx, bson.M{"_id": primimd, "val": bson.M{"$type": "string"}}) | ||||||
|  |  | ||||||
|  | 	var c1 struct { | ||||||
|  | 		ID  primitive.ObjectID `bson:"_id"` | ||||||
|  | 		Val ErrorType          `bson:"val"` | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	err = cursor.Decode(&c1) | ||||||
|  | 	tst.AssertNoErr(t, err) | ||||||
|  |  | ||||||
|  | 	tst.AssertEqual(t, c1.Val, TypeNotImplemented) | ||||||
|  | } | ||||||
| @@ -84,6 +84,9 @@ func (ee *ExErr) FormatLog(lvl LogPrintLevel) string { | |||||||
| 	if lvl == LogPrintShort { | 	if lvl == LogPrintShort { | ||||||
|  |  | ||||||
| 		msg := ee.Message | 		msg := ee.Message | ||||||
|  | 		if msg == "" { | ||||||
|  | 			msg = ee.RecursiveMessage() | ||||||
|  | 		} | ||||||
| 		if ee.OriginalError != nil && ee.OriginalError.Category == CatForeign { | 		if ee.OriginalError != nil && ee.OriginalError.Category == CatForeign { | ||||||
| 			msg = msg + " (" + strings.ReplaceAll(ee.OriginalError.Message, "\n", " ") + ")" | 			msg = msg + " (" + strings.ReplaceAll(ee.OriginalError.Message, "\n", " ") + ")" | ||||||
| 		} | 		} | ||||||
|   | |||||||
| @@ -2,10 +2,19 @@ package exerr | |||||||
|  |  | ||||||
| import ( | import ( | ||||||
| 	"errors" | 	"errors" | ||||||
|  | 	"gogs.mikescher.com/BlackForestBytes/goext/langext" | ||||||
| 	"gogs.mikescher.com/BlackForestBytes/goext/tst" | 	"gogs.mikescher.com/BlackForestBytes/goext/tst" | ||||||
|  | 	"os" | ||||||
| 	"testing" | 	"testing" | ||||||
| ) | ) | ||||||
|  |  | ||||||
|  | func TestMain(m *testing.M) { | ||||||
|  | 	if !Initialized() { | ||||||
|  | 		Init(ErrorPackageConfigInit{ZeroLogErrTraces: langext.PFalse, ZeroLogAllTraces: langext.PFalse}) | ||||||
|  | 	} | ||||||
|  | 	os.Exit(m.Run()) | ||||||
|  | } | ||||||
|  |  | ||||||
| type golangErr struct { | type golangErr struct { | ||||||
| 	Message string | 	Message string | ||||||
| } | } | ||||||
|   | |||||||
| @@ -4,15 +4,6 @@ import ( | |||||||
| 	"sync" | 	"sync" | ||||||
| ) | ) | ||||||
|  |  | ||||||
| type Method string |  | ||||||
|  |  | ||||||
| const ( |  | ||||||
| 	MethodOutput Method = "OUTPUT" |  | ||||||
| 	MethodPrint  Method = "PRINT" |  | ||||||
| 	MethodBuild  Method = "BUILD" |  | ||||||
| 	MethodFatal  Method = "FATAL" |  | ||||||
| ) |  | ||||||
|  |  | ||||||
| type Listener = func(method Method, v *ExErr) | type Listener = func(method Method, v *ExErr) | ||||||
|  |  | ||||||
| var listenerLock = sync.Mutex{} | var listenerLock = sync.Mutex{} | ||||||
|   | |||||||
| @@ -72,20 +72,22 @@ func NewEngine(opt Options) *GinWrapper { | |||||||
| 		engine.Use(CorsMiddleware()) | 		engine.Use(CorsMiddleware()) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|  | 	if wrapper.ginDebug { | ||||||
|  | 		gin.SetMode(gin.DebugMode) | ||||||
|  |  | ||||||
| 		// do not debug-print routes | 		// do not debug-print routes | ||||||
| 		gin.DebugPrintRouteFunc = func(_, _, _ string, _ int) {} | 		gin.DebugPrintRouteFunc = func(_, _, _ string, _ int) {} | ||||||
|  |  | ||||||
| 	if !wrapper.ginDebug { |  | ||||||
| 		gin.SetMode(gin.ReleaseMode) |  | ||||||
|  |  | ||||||
| 		if !wrapper.suppressGinLogs { | 		if !wrapper.suppressGinLogs { | ||||||
| 			ginlogger := gin.Logger() | 			ginlogger := gin.Logger() | ||||||
| 			engine.Use(func(context *gin.Context) { | 			engine.Use(func(context *gin.Context) { ginlogger(context) }) | ||||||
| 				ginlogger(context) |  | ||||||
| 			}) |  | ||||||
| 		} | 		} | ||||||
|  |  | ||||||
| 	} else { | 	} else { | ||||||
| 		gin.SetMode(gin.DebugMode) | 		gin.SetMode(gin.ReleaseMode) | ||||||
|  |  | ||||||
|  | 		// do not debug-print routes | ||||||
|  | 		gin.DebugPrintRouteFunc = func(_, _, _ string, _ int) {} | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	return wrapper | 	return wrapper | ||||||
|   | |||||||
							
								
								
									
										10
									
								
								go.mod
									
									
									
									
									
								
							
							
						
						
									
										10
									
								
								go.mod
									
									
									
									
									
								
							| @@ -7,7 +7,7 @@ require ( | |||||||
| 	github.com/glebarez/go-sqlite v1.22.0 // only needed for tests -.- | 	github.com/glebarez/go-sqlite v1.22.0 // only needed for tests -.- | ||||||
| 	github.com/jmoiron/sqlx v1.4.0 | 	github.com/jmoiron/sqlx v1.4.0 | ||||||
| 	github.com/rs/xid v1.5.0 | 	github.com/rs/xid v1.5.0 | ||||||
| 	github.com/rs/zerolog v1.32.0 | 	github.com/rs/zerolog v1.33.0 | ||||||
| 	go.mongodb.org/mongo-driver v1.15.0 | 	go.mongodb.org/mongo-driver v1.15.0 | ||||||
| 	golang.org/x/crypto v0.23.0 | 	golang.org/x/crypto v0.23.0 | ||||||
| 	golang.org/x/sys v0.20.0 | 	golang.org/x/sys v0.20.0 | ||||||
| @@ -21,19 +21,19 @@ require ( | |||||||
| ) | ) | ||||||
|  |  | ||||||
| require ( | require ( | ||||||
| 	github.com/bytedance/sonic v1.11.6 // indirect | 	github.com/bytedance/sonic v1.11.8 // indirect | ||||||
| 	github.com/bytedance/sonic/loader v0.1.1 // indirect | 	github.com/bytedance/sonic/loader v0.1.1 // indirect | ||||||
| 	github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect | 	github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect | ||||||
| 	github.com/chenzhuoyu/iasm v0.9.1 // indirect | 	github.com/chenzhuoyu/iasm v0.9.1 // indirect | ||||||
| 	github.com/cloudwego/base64x v0.1.4 // indirect | 	github.com/cloudwego/base64x v0.1.4 // indirect | ||||||
| 	github.com/cloudwego/iasm v0.2.0 // indirect | 	github.com/cloudwego/iasm v0.2.0 // indirect | ||||||
| 	github.com/dustin/go-humanize v1.0.1 // indirect | 	github.com/dustin/go-humanize v1.0.1 // indirect | ||||||
| 	github.com/gabriel-vasile/mimetype v1.4.3 // indirect | 	github.com/gabriel-vasile/mimetype v1.4.4 // indirect | ||||||
| 	github.com/gin-contrib/sse v0.1.0 // indirect | 	github.com/gin-contrib/sse v0.1.0 // indirect | ||||||
| 	github.com/go-playground/locales v0.14.1 // indirect | 	github.com/go-playground/locales v0.14.1 // indirect | ||||||
| 	github.com/go-playground/universal-translator v0.18.1 // indirect | 	github.com/go-playground/universal-translator v0.18.1 // indirect | ||||||
| 	github.com/go-playground/validator/v10 v10.20.0 // indirect | 	github.com/go-playground/validator/v10 v10.21.0 // indirect | ||||||
| 	github.com/goccy/go-json v0.10.2 // indirect | 	github.com/goccy/go-json v0.10.3 // indirect | ||||||
| 	github.com/golang/snappy v0.0.4 // indirect | 	github.com/golang/snappy v0.0.4 // indirect | ||||||
| 	github.com/google/uuid v1.5.0 // indirect | 	github.com/google/uuid v1.5.0 // indirect | ||||||
| 	github.com/json-iterator/go v1.1.12 // indirect | 	github.com/json-iterator/go v1.1.12 // indirect | ||||||
|   | |||||||
							
								
								
									
										12
									
								
								go.sum
									
									
									
									
									
								
							
							
						
						
									
										12
									
								
								go.sum
									
									
									
									
									
								
							| @@ -18,6 +18,10 @@ github.com/bytedance/sonic v1.11.5 h1:G00FYjjqll5iQ1PYXynbg/hyzqBqavH8Mo9/oTopd9 | |||||||
| github.com/bytedance/sonic v1.11.5/go.mod h1:X2PC2giUdj/Cv2lliWFLk6c/DUQok5rViJSemeB0wDw= | github.com/bytedance/sonic v1.11.5/go.mod h1:X2PC2giUdj/Cv2lliWFLk6c/DUQok5rViJSemeB0wDw= | ||||||
| github.com/bytedance/sonic v1.11.6 h1:oUp34TzMlL+OY1OUWxHqsdkgC/Zfc85zGqw9siXjrc0= | github.com/bytedance/sonic v1.11.6 h1:oUp34TzMlL+OY1OUWxHqsdkgC/Zfc85zGqw9siXjrc0= | ||||||
| github.com/bytedance/sonic v1.11.6/go.mod h1:LysEHSvpvDySVdC2f87zGWf6CIKJcAvqab1ZaiQtds4= | github.com/bytedance/sonic v1.11.6/go.mod h1:LysEHSvpvDySVdC2f87zGWf6CIKJcAvqab1ZaiQtds4= | ||||||
|  | github.com/bytedance/sonic v1.11.7 h1:k/l9p1hZpNIMJSk37wL9ltkcpqLfIho1vYthi4xT2t4= | ||||||
|  | github.com/bytedance/sonic v1.11.7/go.mod h1:LysEHSvpvDySVdC2f87zGWf6CIKJcAvqab1ZaiQtds4= | ||||||
|  | github.com/bytedance/sonic v1.11.8 h1:Zw/j1KfiS+OYTi9lyB3bb0CFxPJVkM17k1wyDG32LRA= | ||||||
|  | github.com/bytedance/sonic v1.11.8/go.mod h1:LysEHSvpvDySVdC2f87zGWf6CIKJcAvqab1ZaiQtds4= | ||||||
| github.com/bytedance/sonic/loader v0.1.0/go.mod h1:UmRT+IRTGKz/DAkzcEGzyVqQFJ7H9BqwBO3pm9H/+HY= | github.com/bytedance/sonic/loader v0.1.0/go.mod h1:UmRT+IRTGKz/DAkzcEGzyVqQFJ7H9BqwBO3pm9H/+HY= | ||||||
| github.com/bytedance/sonic/loader v0.1.1 h1:c+e5Pt1k/cy5wMveRDyk2X4B9hF4g7an8N3zCYjJFNM= | github.com/bytedance/sonic/loader v0.1.1 h1:c+e5Pt1k/cy5wMveRDyk2X4B9hF4g7an8N3zCYjJFNM= | ||||||
| github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU= | github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU= | ||||||
| @@ -49,6 +53,8 @@ github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkp | |||||||
| github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto= | github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto= | ||||||
| github.com/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0= | github.com/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0= | ||||||
| github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk= | github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk= | ||||||
|  | github.com/gabriel-vasile/mimetype v1.4.4 h1:QjV6pZ7/XZ7ryI2KuyeEDE8wnh7fHP9YnQy+R0LnH8I= | ||||||
|  | github.com/gabriel-vasile/mimetype v1.4.4/go.mod h1:JwLei5XPtWdGiMFB5Pjle1oEeoSeEuJfJE+TtfvdB/s= | ||||||
| github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= | github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= | ||||||
| github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= | github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= | ||||||
| github.com/gin-gonic/gin v1.9.1 h1:4idEAncQnU5cB7BeOkPtxjfCSye0AAm1R0RVIqJ+Jmg= | github.com/gin-gonic/gin v1.9.1 h1:4idEAncQnU5cB7BeOkPtxjfCSye0AAm1R0RVIqJ+Jmg= | ||||||
| @@ -73,12 +79,16 @@ github.com/go-playground/validator/v10 v10.19.0 h1:ol+5Fu+cSq9JD7SoSqe04GMI92cbn | |||||||
| github.com/go-playground/validator/v10 v10.19.0/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM= | github.com/go-playground/validator/v10 v10.19.0/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM= | ||||||
| github.com/go-playground/validator/v10 v10.20.0 h1:K9ISHbSaI0lyB2eWMPJo+kOS/FBExVwjEviJTixqxL8= | github.com/go-playground/validator/v10 v10.20.0 h1:K9ISHbSaI0lyB2eWMPJo+kOS/FBExVwjEviJTixqxL8= | ||||||
| github.com/go-playground/validator/v10 v10.20.0/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM= | github.com/go-playground/validator/v10 v10.20.0/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM= | ||||||
|  | github.com/go-playground/validator/v10 v10.21.0 h1:4fZA11ovvtkdgaeev9RGWPgc1uj3H8W+rNYyH/ySBb0= | ||||||
|  | github.com/go-playground/validator/v10 v10.21.0/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM= | ||||||
| github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE= | github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE= | ||||||
| github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg= | github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg= | ||||||
| github.com/go-sql-driver/mysql v1.8.1 h1:LedoTUt/eveggdHS9qUFC1EFSa8bU2+1pZjSRpvNJ1Y= | github.com/go-sql-driver/mysql v1.8.1 h1:LedoTUt/eveggdHS9qUFC1EFSa8bU2+1pZjSRpvNJ1Y= | ||||||
| github.com/go-sql-driver/mysql v1.8.1/go.mod h1:wEBSXgmK//2ZFJyE+qWnIsVGmvmEKlqwuVSjsCm7DZg= | github.com/go-sql-driver/mysql v1.8.1/go.mod h1:wEBSXgmK//2ZFJyE+qWnIsVGmvmEKlqwuVSjsCm7DZg= | ||||||
| github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU= | github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU= | ||||||
| github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I= | github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I= | ||||||
|  | github.com/goccy/go-json v0.10.3 h1:KZ5WoDbxAIgm2HNbYckL0se1fHD6rz5j4ywS6ebzDqA= | ||||||
|  | github.com/goccy/go-json v0.10.3/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M= | ||||||
| github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= | github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= | ||||||
| github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= | github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= | ||||||
| github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= | github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= | ||||||
| @@ -162,6 +172,8 @@ github.com/rs/zerolog v1.31.0 h1:FcTR3NnLWW+NnTwwhFWiJSZr4ECLpqCm6QsEnyvbV4A= | |||||||
| github.com/rs/zerolog v1.31.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss= | github.com/rs/zerolog v1.31.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss= | ||||||
| github.com/rs/zerolog v1.32.0 h1:keLypqrlIjaFsbmJOBdB/qvyF8KEtCWHwobLp5l/mQ0= | github.com/rs/zerolog v1.32.0 h1:keLypqrlIjaFsbmJOBdB/qvyF8KEtCWHwobLp5l/mQ0= | ||||||
| github.com/rs/zerolog v1.32.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss= | github.com/rs/zerolog v1.32.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss= | ||||||
|  | github.com/rs/zerolog v1.33.0 h1:1cU2KZkvPxNyfgEmhHAz/1A9Bz+llsdYzklWFzgp0r8= | ||||||
|  | github.com/rs/zerolog v1.33.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss= | ||||||
| github.com/ruudk/golang-pdf417 v0.0.0-20181029194003-1af4ab5afa58/go.mod h1:6lfFZQK844Gfx8o5WFuvpxWRwnSoipWe/p622j1v06w= | github.com/ruudk/golang-pdf417 v0.0.0-20181029194003-1af4ab5afa58/go.mod h1:6lfFZQK844Gfx8o5WFuvpxWRwnSoipWe/p622j1v06w= | ||||||
| github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= | github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= | ||||||
| github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= | github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= | ||||||
|   | |||||||
| @@ -1,5 +1,5 @@ | |||||||
| package goext | package goext | ||||||
|  |  | ||||||
| const GoextVersion = "0.0.452" | const GoextVersion = "0.0.465" | ||||||
|  |  | ||||||
| const GoextVersionTimestamp = "2024-05-14T12:48:43+0200" | const GoextVersionTimestamp = "2024-06-03T09:39:57+0200" | ||||||
|   | |||||||
| @@ -234,11 +234,15 @@ func ObjectFitImage(img image.Image, bbw float64, bbh float64, fit ImageFit, fil | |||||||
|  |  | ||||||
| 		// we scale the bounding box by fac (both dimension the same amount, to keep the bounding-box ratio) | 		// we scale the bounding box by fac (both dimension the same amount, to keep the bounding-box ratio) | ||||||
|  |  | ||||||
|  | 		// [ow|oh] ==> size of output image (same ratio as bounding box [bbw|bbh]) | ||||||
|  |  | ||||||
| 		ow := int(math.Round(bbw * facOut)) | 		ow := int(math.Round(bbw * facOut)) | ||||||
| 		oh := int(math.Round(bbh * facOut)) | 		oh := int(math.Round(bbh * facOut)) | ||||||
|  |  | ||||||
| 		facScale := mathext.Min(float64(ow)/float64(iw), float64(oh)/float64(ih)) | 		facScale := mathext.Min(float64(ow)/float64(iw), float64(oh)/float64(ih)) | ||||||
|  |  | ||||||
|  | 		// [dw|dh] ==> size of destination rect (where to draw source in output image) (same ratio as input image [iw|ih]) | ||||||
|  |  | ||||||
| 		dw := int(math.Round(float64(iw) * facScale)) | 		dw := int(math.Round(float64(iw) * facScale)) | ||||||
| 		dh := int(math.Round(float64(ih) * facScale)) | 		dh := int(math.Round(float64(ih) * facScale)) | ||||||
|  |  | ||||||
| @@ -248,11 +252,11 @@ func ObjectFitImage(img image.Image, bbw float64, bbh float64, fit ImageFit, fil | |||||||
| 		if fit == ImageFitContainCenter { | 		if fit == ImageFitContainCenter { | ||||||
| 			destBounds = image.Rect((ow-dw)/2, (oh-dh)/2, (ow-dw)/2+dw, (oh-dh)/2+dh) | 			destBounds = image.Rect((ow-dw)/2, (oh-dh)/2, (ow-dw)/2+dw, (oh-dh)/2+dh) | ||||||
| 		} else if fit == ImageFitContainTopLeft { | 		} else if fit == ImageFitContainTopLeft { | ||||||
| 			destBounds = image.Rect(0, 0, iw, dh) | 			destBounds = image.Rect(0, 0, dw, dh) | ||||||
| 		} else if fit == ImageFitContainTopRight { | 		} else if fit == ImageFitContainTopRight { | ||||||
| 			destBounds = image.Rect(ow-iw, 0, ow, dh) | 			destBounds = image.Rect(ow-dw, 0, ow, dh) | ||||||
| 		} else if fit == ImageFitContainBottomLeft { | 		} else if fit == ImageFitContainBottomLeft { | ||||||
| 			destBounds = image.Rect(0, oh-dh, iw, oh) | 			destBounds = image.Rect(0, oh-dh, dw, oh) | ||||||
| 		} else if fit == ImageFitContainBottomRight { | 		} else if fit == ImageFitContainBottomRight { | ||||||
| 			destBounds = image.Rect(ow-dw, oh-dh, ow, oh) | 			destBounds = image.Rect(ow-dw, oh-dh, ow, oh) | ||||||
| 		} | 		} | ||||||
|   | |||||||
| @@ -59,6 +59,18 @@ func ArrUnique[T comparable](array []T) []T { | |||||||
| 	return result | 	return result | ||||||
| } | } | ||||||
|  |  | ||||||
|  | func ArrUniqueStable[T comparable](array []T) []T { | ||||||
|  | 	hist := make(map[T]bool, len(array)) | ||||||
|  | 	result := make([]T, 0, len(array)) | ||||||
|  | 	for _, v := range array { | ||||||
|  | 		if _, ok := hist[v]; !ok { | ||||||
|  | 			hist[v] = true | ||||||
|  | 			result = append(result, v) | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | 	return result | ||||||
|  | } | ||||||
|  |  | ||||||
| func ArrEqualsExact[T comparable](arr1 []T, arr2 []T) bool { | func ArrEqualsExact[T comparable](arr1 []T, arr2 []T) bool { | ||||||
| 	if len(arr1) != len(arr2) { | 	if len(arr1) != len(arr2) { | ||||||
| 		return false | 		return false | ||||||
| @@ -462,6 +474,17 @@ func ArrAppend[T any](arr []T, add ...T) []T { | |||||||
| 	return r | 	return r | ||||||
| } | } | ||||||
|  |  | ||||||
|  | // ArrPrepend works similar to append(x, y, z) - but doe snot touch the old array and creates a new one | ||||||
|  | // Also - in contrast to ArrAppend - the add values are inserted at the start of the resulting array (in reverse order) | ||||||
|  | func ArrPrepend[T any](arr []T, add ...T) []T { | ||||||
|  | 	out := make([]T, len(arr)+len(add)) | ||||||
|  | 	copy(out[len(add):], arr) | ||||||
|  | 	for i := 0; i < len(add); i++ { | ||||||
|  | 		out[len(add)-i-1] = add[i] | ||||||
|  | 	} | ||||||
|  | 	return out | ||||||
|  | } | ||||||
|  |  | ||||||
| // ArrCopy does a shallow copy of the 'in' array | // ArrCopy does a shallow copy of the 'in' array | ||||||
| func ArrCopy[T any](in []T) []T { | func ArrCopy[T any](in []T) []T { | ||||||
| 	out := make([]T, len(in)) | 	out := make([]T, len(in)) | ||||||
|   | |||||||
| @@ -2,6 +2,7 @@ package langext | |||||||
|  |  | ||||||
| import ( | import ( | ||||||
| 	"gogs.mikescher.com/BlackForestBytes/goext/tst" | 	"gogs.mikescher.com/BlackForestBytes/goext/tst" | ||||||
|  | 	"strings" | ||||||
| 	"testing" | 	"testing" | ||||||
| ) | ) | ||||||
|  |  | ||||||
| @@ -10,3 +11,13 @@ func TestJoinString(t *testing.T) { | |||||||
| 	res := JoinString(ids, ",") | 	res := JoinString(ids, ",") | ||||||
| 	tst.AssertEqual(t, res, "1,2,3") | 	tst.AssertEqual(t, res, "1,2,3") | ||||||
| } | } | ||||||
|  |  | ||||||
|  | func TestArrPrepend(t *testing.T) { | ||||||
|  | 	v1 := []string{"1", "2", "3"} | ||||||
|  |  | ||||||
|  | 	v2 := ArrPrepend(v1, "4", "5", "6") | ||||||
|  |  | ||||||
|  | 	tst.AssertEqual(t, strings.Join(v1, ""), "123") | ||||||
|  | 	tst.AssertEqual(t, strings.Join(v2, ""), "654123") | ||||||
|  |  | ||||||
|  | } | ||||||
|   | |||||||
| @@ -5,6 +5,7 @@ import ( | |||||||
| 	"go.mongodb.org/mongo-driver/bson/bsoncodec" | 	"go.mongodb.org/mongo-driver/bson/bsoncodec" | ||||||
| 	"go.mongodb.org/mongo-driver/bson/bsontype" | 	"go.mongodb.org/mongo-driver/bson/bsontype" | ||||||
| 	"go.mongodb.org/mongo-driver/bson/primitive" | 	"go.mongodb.org/mongo-driver/bson/primitive" | ||||||
|  | 	"gogs.mikescher.com/BlackForestBytes/goext/exerr" | ||||||
| 	"gogs.mikescher.com/BlackForestBytes/goext/langext" | 	"gogs.mikescher.com/BlackForestBytes/goext/langext" | ||||||
| 	"gogs.mikescher.com/BlackForestBytes/goext/rfctime" | 	"gogs.mikescher.com/BlackForestBytes/goext/rfctime" | ||||||
| 	"reflect" | 	"reflect" | ||||||
| @@ -34,6 +35,15 @@ func CreateGoExtBsonRegistry() *bsoncodec.Registry { | |||||||
| 	rb.RegisterTypeDecoder(reflect.TypeOf(rfctime.SecondsF64(0)), rfctime.SecondsF64(0)) | 	rb.RegisterTypeDecoder(reflect.TypeOf(rfctime.SecondsF64(0)), rfctime.SecondsF64(0)) | ||||||
| 	rb.RegisterTypeDecoder(reflect.TypeOf(langext.Ptr(rfctime.SecondsF64(0))), rfctime.SecondsF64(0)) | 	rb.RegisterTypeDecoder(reflect.TypeOf(langext.Ptr(rfctime.SecondsF64(0))), rfctime.SecondsF64(0)) | ||||||
|  |  | ||||||
|  | 	rb.RegisterTypeDecoder(reflect.TypeOf(exerr.ErrorCategory{}), exerr.ErrorCategory{}) | ||||||
|  | 	rb.RegisterTypeDecoder(reflect.TypeOf(langext.Ptr(exerr.ErrorCategory{})), exerr.ErrorCategory{}) | ||||||
|  |  | ||||||
|  | 	rb.RegisterTypeDecoder(reflect.TypeOf(exerr.ErrorSeverity{}), exerr.ErrorSeverity{}) | ||||||
|  | 	rb.RegisterTypeDecoder(reflect.TypeOf(langext.Ptr(exerr.ErrorSeverity{})), exerr.ErrorSeverity{}) | ||||||
|  |  | ||||||
|  | 	rb.RegisterTypeDecoder(reflect.TypeOf(exerr.ErrorType{}), exerr.ErrorType{}) | ||||||
|  | 	rb.RegisterTypeDecoder(reflect.TypeOf(langext.Ptr(exerr.ErrorType{})), exerr.ErrorType{}) | ||||||
|  |  | ||||||
| 	bsoncodec.DefaultValueEncoders{}.RegisterDefaultEncoders(rb) | 	bsoncodec.DefaultValueEncoders{}.RegisterDefaultEncoders(rb) | ||||||
| 	bsoncodec.DefaultValueDecoders{}.RegisterDefaultDecoders(rb) | 	bsoncodec.DefaultValueDecoders{}.RegisterDefaultDecoders(rb) | ||||||
|  |  | ||||||
|   | |||||||
| @@ -156,7 +156,7 @@ func SubtractYears(t time.Time, yearCount float64, tz *time.Location) time.Time | |||||||
|  |  | ||||||
| 	intCount, floatCount := math.Modf(yearCount) | 	intCount, floatCount := math.Modf(yearCount) | ||||||
|  |  | ||||||
| 	t.AddDate(-int(intCount), 0, 0) | 	t = t.AddDate(-int(intCount), 0, 0) | ||||||
|  |  | ||||||
| 	t0 := TimeToYearStart(t, tz) | 	t0 := TimeToYearStart(t, tz) | ||||||
| 	t1 := TimeToYearEnd(t, tz) | 	t1 := TimeToYearEnd(t, tz) | ||||||
| @@ -173,7 +173,7 @@ func AddYears(t time.Time, yearCount float64, tz *time.Location) time.Time { | |||||||
|  |  | ||||||
| 	intCount, floatCount := math.Modf(yearCount) | 	intCount, floatCount := math.Modf(yearCount) | ||||||
|  |  | ||||||
| 	t.AddDate(int(intCount), 0, 0) | 	t = t.AddDate(int(intCount), 0, 0) | ||||||
|  |  | ||||||
| 	t0 := TimeToYearStart(t, tz) | 	t0 := TimeToYearStart(t, tz) | ||||||
| 	t1 := TimeToYearEnd(t, tz) | 	t1 := TimeToYearEnd(t, tz) | ||||||
|   | |||||||
							
								
								
									
										158
									
								
								timeext/time_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										158
									
								
								timeext/time_test.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,158 @@ | |||||||
|  | package timeext | ||||||
|  |  | ||||||
|  | import ( | ||||||
|  | 	"testing" | ||||||
|  | 	"time" | ||||||
|  | ) | ||||||
|  |  | ||||||
|  | func TestTimeToDayStart(t *testing.T) { | ||||||
|  | 	tz := TimezoneBerlin | ||||||
|  | 	tm := time.Date(2022, 1, 1, 13, 14, 15, 0, tz) | ||||||
|  | 	expected := time.Date(2022, 1, 1, 0, 0, 0, 0, tz) | ||||||
|  | 	result := TimeToDayStart(tm, tz) | ||||||
|  | 	if !result.Equal(expected) { | ||||||
|  | 		t.Errorf("Expected %v but got %v", expected, result) | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func TestTimeToDayEnd(t *testing.T) { | ||||||
|  | 	tz := TimezoneBerlin | ||||||
|  | 	tm := time.Date(2022, 1, 1, 13, 14, 15, 0, tz) | ||||||
|  | 	expected := time.Date(2022, 1, 2, 0, 0, 0, 0, tz).Add(-1) | ||||||
|  | 	result := TimeToDayEnd(tm, tz) | ||||||
|  | 	if !result.Equal(expected) { | ||||||
|  | 		t.Errorf("Expected %v but got %v", expected, result) | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func TestIsSameDayIncludingDateBoundaries(t *testing.T) { | ||||||
|  | 	tz := TimezoneBerlin | ||||||
|  | 	t1 := time.Date(2022, 1, 1, 23, 59, 59, 0, tz) | ||||||
|  | 	t2 := time.Date(2022, 1, 2, 0, 0, 0, 0, tz) | ||||||
|  | 	if !IsSameDayIncludingDateBoundaries(t1, t2, tz) { | ||||||
|  | 		t.Errorf("Expected %v and %v to be the same day", t1, t2) | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func TestIsDatePartEqual(t *testing.T) { | ||||||
|  | 	tz := TimezoneBerlin | ||||||
|  | 	t1 := time.Date(2022, 1, 1, 23, 59, 59, 0, tz) | ||||||
|  | 	t2 := time.Date(2022, 1, 1, 0, 0, 0, 0, tz) | ||||||
|  | 	if !IsDatePartEqual(t1, t2, tz) { | ||||||
|  | 		t.Errorf("Expected %v and %v to have the same date part", t1, t2) | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func TestWithTimePart(t *testing.T) { | ||||||
|  | 	tz := TimezoneBerlin | ||||||
|  | 	base := time.Date(2022, 1, 1, 0, 0, 0, 0, tz) | ||||||
|  | 	expected := time.Date(2022, 1, 1, 13, 14, 15, 0, tz) | ||||||
|  | 	result := WithTimePart(base, 13, 14, 15) | ||||||
|  | 	if !result.Equal(expected) { | ||||||
|  | 		t.Errorf("Expected %v but got %v", expected, result) | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func TestCombineDateAndTime(t *testing.T) { | ||||||
|  | 	tz := TimezoneBerlin | ||||||
|  | 	d := time.Date(2022, 1, 1, 0, 0, 0, 0, tz) | ||||||
|  | 	tm := time.Date(0, 0, 0, 13, 14, 15, 0, tz) | ||||||
|  | 	expected := time.Date(2022, 1, 1, 13, 14, 15, 0, tz) | ||||||
|  | 	result := CombineDateAndTime(d, tm) | ||||||
|  | 	if !result.Equal(expected) { | ||||||
|  | 		t.Errorf("Expected %v but got %v", expected, result) | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func TestIsSunday(t *testing.T) { | ||||||
|  | 	tz := TimezoneBerlin | ||||||
|  | 	tm := time.Date(2022, 1, 2, 0, 0, 0, 0, tz) // 2nd January 2022 is a Sunday | ||||||
|  | 	if !IsSunday(tm, tz) { | ||||||
|  | 		t.Errorf("Expected %v to be a Sunday", tm) | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func TestDurationFromTime(t *testing.T) { | ||||||
|  | 	expected := time.Duration(13*time.Hour + 14*time.Minute + 15*time.Second) | ||||||
|  | 	result := DurationFromTime(13, 14, 15) | ||||||
|  | 	if result != expected { | ||||||
|  | 		t.Errorf("Expected %v but got %v", expected, result) | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func TestMin(t *testing.T) { | ||||||
|  | 	t1 := time.Date(2022, 1, 1, 0, 0, 0, 0, time.UTC) | ||||||
|  | 	t2 := time.Date(2022, 1, 2, 0, 0, 0, 0, time.UTC) | ||||||
|  | 	expected := t1 | ||||||
|  | 	result := Min(t1, t2) | ||||||
|  | 	if !result.Equal(expected) { | ||||||
|  | 		t.Errorf("Expected %v but got %v", expected, result) | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func TestMax(t *testing.T) { | ||||||
|  | 	t1 := time.Date(2022, 1, 1, 0, 0, 0, 0, time.UTC) | ||||||
|  | 	t2 := time.Date(2022, 1, 2, 0, 0, 0, 0, time.UTC) | ||||||
|  | 	expected := t2 | ||||||
|  | 	result := Max(t1, t2) | ||||||
|  | 	if !result.Equal(expected) { | ||||||
|  | 		t.Errorf("Expected %v but got %v", expected, result) | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func TestUnixFloatSeconds(t *testing.T) { | ||||||
|  | 	v := 1640995200.0 // 1st January 2022 00:00:00 UTC in Unix timestamp | ||||||
|  | 	expected := time.Date(2022, 1, 1, 0, 0, 0, 0, time.UTC) | ||||||
|  | 	result := UnixFloatSeconds(v) | ||||||
|  | 	if !result.Equal(expected) { | ||||||
|  | 		t.Errorf("Expected %v but got %v", expected, result) | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func TestFloorTime(t *testing.T) { | ||||||
|  | 	tm := time.Date(2022, 1, 1, 13, 14, 15, 0, time.UTC) | ||||||
|  | 	expected := time.Date(2022, 1, 1, 0, 0, 0, 0, time.UTC) | ||||||
|  | 	result := FloorTime(tm) | ||||||
|  | 	if !result.Equal(expected) { | ||||||
|  | 		t.Errorf("Expected %v but got %v", expected, result) | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func TestSubtractYears(t *testing.T) { | ||||||
|  | 	tz := TimezoneBerlin | ||||||
|  | 	tm := time.Date(2022, 1, 1, 0, 0, 0, 0, tz) | ||||||
|  |  | ||||||
|  | 	expected := time.Date(2021, 1, 1, 0, 0, 0, 0, tz) | ||||||
|  | 	result := SubtractYears(tm, 1, tz) | ||||||
|  | 	if !result.Equal(expected) { | ||||||
|  | 		t.Errorf("Expected %v but got %v", expected, result) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	expected = time.Date(2020, 1, 1, 0, 0, 0, 0, tz) | ||||||
|  | 	result = SubtractYears(tm, 2, tz) | ||||||
|  | 	if !result.Equal(expected) { | ||||||
|  | 		t.Errorf("Expected %v but got %v", expected, result) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	expected = time.Date(2019, 1, 1, 0, 0, 0, 0, tz) | ||||||
|  | 	result = SubtractYears(tm, 3, tz) | ||||||
|  | 	if !result.Equal(expected) { | ||||||
|  | 		t.Errorf("Expected %v but got %v", expected, result) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	expected = time.Date(2025, 1, 1, 0, 0, 0, 0, tz) | ||||||
|  | 	result = SubtractYears(tm, -3, tz) | ||||||
|  | 	if !result.Equal(expected) { | ||||||
|  | 		t.Errorf("Expected %v but got %v", expected, result) | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func TestAddYears(t *testing.T) { | ||||||
|  | 	tz := TimezoneBerlin | ||||||
|  | 	tm := time.Date(2022, 1, 1, 0, 0, 0, 0, tz) | ||||||
|  | 	expected := time.Date(2023, 1, 1, 0, 0, 0, 0, tz) | ||||||
|  | 	result := AddYears(tm, 1, tz) | ||||||
|  | 	if !result.Equal(expected) { | ||||||
|  | 		t.Errorf("Expected %v but got %v", expected, result) | ||||||
|  | 	} | ||||||
|  | } | ||||||
							
								
								
									
										9
									
								
								wpdf/utils.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								wpdf/utils.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,9 @@ | |||||||
|  | package wpdf | ||||||
|  |  | ||||||
|  | func hexToColor(c uint32) PDFColor { | ||||||
|  | 	return PDFColor{R: int((c >> 16) & 0xFF), G: int((c >> 8) & 0xFF), B: int((c >> 0) & 0xFF)} | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func rgbToColor(r, g, b int) PDFColor { | ||||||
|  | 	return PDFColor{R: r, G: g, B: b} | ||||||
|  | } | ||||||
							
								
								
									
										14
									
								
								wpdf/wpdf.go
									
									
									
									
									
								
							
							
						
						
									
										14
									
								
								wpdf/wpdf.go
									
									
									
									
									
								
							| @@ -9,6 +9,7 @@ type WPDFBuilder struct { | |||||||
| 	b           *gofpdf.Fpdf | 	b           *gofpdf.Fpdf | ||||||
| 	tr          func(string) string | 	tr          func(string) string | ||||||
| 	cellHeight  float64 | 	cellHeight  float64 | ||||||
|  | 	cellSpacing float64 | ||||||
| 	fontName    PDFFontFamily | 	fontName    PDFFontFamily | ||||||
| 	fontStyle   PDFFontStyle | 	fontStyle   PDFFontStyle | ||||||
| 	fontSize    float64 | 	fontSize    float64 | ||||||
| @@ -41,6 +42,7 @@ func NewPDFBuilder(orientation PDFOrientation, size PDFSize, unicode bool) *WPDF | |||||||
| 		b:           fpdfbuilder, | 		b:           fpdfbuilder, | ||||||
| 		tr:          tr, | 		tr:          tr, | ||||||
| 		cellHeight:  5, | 		cellHeight:  5, | ||||||
|  | 		cellSpacing: 1, | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	b.SetMargins(PDFMargins{Left: 15, Top: 25, Right: 15}) // default values | 	b.SetMargins(PDFMargins{Left: 15, Top: 25, Right: 15}) // default values | ||||||
| @@ -49,6 +51,10 @@ func NewPDFBuilder(orientation PDFOrientation, size PDFSize, unicode bool) *WPDF | |||||||
| 	return b | 	return b | ||||||
| } | } | ||||||
|  |  | ||||||
|  | func (b *WPDFBuilder) FPDF() *gofpdf.Fpdf { | ||||||
|  | 	return b.b | ||||||
|  | } | ||||||
|  |  | ||||||
| func (b *WPDFBuilder) SetMargins(v PDFMargins) { | func (b *WPDFBuilder) SetMargins(v PDFMargins) { | ||||||
| 	b.b.SetMargins(v.Left, v.Top, v.Right) | 	b.b.SetMargins(v.Left, v.Top, v.Right) | ||||||
| } | } | ||||||
| @@ -99,6 +105,10 @@ func (b *WPDFBuilder) SetFont(fontName PDFFontFamily, fontStyle PDFFontStyle, fo | |||||||
| 	b.cellHeight = b.b.PointConvert(fontSize) | 	b.cellHeight = b.b.PointConvert(fontSize) | ||||||
| } | } | ||||||
|  |  | ||||||
|  | func (b *WPDFBuilder) SetCellSpacing(h float64) { | ||||||
|  | 	b.cellSpacing = h | ||||||
|  | } | ||||||
|  |  | ||||||
| func (b *WPDFBuilder) Ln(h float64) { | func (b *WPDFBuilder) Ln(h float64) { | ||||||
| 	b.b.Ln(h) | 	b.b.Ln(h) | ||||||
| } | } | ||||||
| @@ -116,6 +126,10 @@ func (b *WPDFBuilder) SetX(x float64) { | |||||||
| 	b.b.SetX(x) | 	b.b.SetX(x) | ||||||
| } | } | ||||||
|  |  | ||||||
|  | func (b *WPDFBuilder) IncX(dx float64) { | ||||||
|  | 	b.b.SetX(b.b.GetX() + dx) | ||||||
|  | } | ||||||
|  |  | ||||||
| func (b *WPDFBuilder) GetX() float64 { | func (b *WPDFBuilder) GetX() float64 { | ||||||
| 	return b.b.GetX() | 	return b.b.GetX() | ||||||
| } | } | ||||||
|   | |||||||
| @@ -17,6 +17,10 @@ type PDFCellOpt struct { | |||||||
| 	extraLn           *float64 | 	extraLn           *float64 | ||||||
| 	x                 *float64 | 	x                 *float64 | ||||||
| 	autoWidth         *bool | 	autoWidth         *bool | ||||||
|  | 	textColor         *PDFColor | ||||||
|  | 	borderColor       *PDFColor | ||||||
|  | 	fillColor         *PDFColor | ||||||
|  | 	autoWidthPaddingX *float64 | ||||||
| } | } | ||||||
|  |  | ||||||
| func NewPDFCellOpt() *PDFCellOpt { | func NewPDFCellOpt() *PDFCellOpt { | ||||||
| @@ -110,12 +114,47 @@ func (opt *PDFCellOpt) AutoWidth() *PDFCellOpt { | |||||||
| 	return opt | 	return opt | ||||||
| } | } | ||||||
|  |  | ||||||
|  | func (opt *PDFCellOpt) AutoWidthPaddingX(v float64) *PDFCellOpt { | ||||||
|  | 	opt.autoWidthPaddingX = &v | ||||||
|  | 	return opt | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func (opt *PDFCellOpt) TextColor(cr, cg, cb int) *PDFCellOpt { | ||||||
|  | 	opt.textColor = langext.Ptr(rgbToColor(cr, cg, cb)) | ||||||
|  | 	return opt | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func (opt *PDFCellOpt) TextColorHex(c uint32) *PDFCellOpt { | ||||||
|  | 	opt.textColor = langext.Ptr(hexToColor(c)) | ||||||
|  | 	return opt | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func (opt *PDFCellOpt) BorderColor(cr, cg, cb int) *PDFCellOpt { | ||||||
|  | 	opt.borderColor = langext.Ptr(rgbToColor(cr, cg, cb)) | ||||||
|  | 	return opt | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func (opt *PDFCellOpt) BorderColorHex(c uint32) *PDFCellOpt { | ||||||
|  | 	opt.borderColor = langext.Ptr(hexToColor(c)) | ||||||
|  | 	return opt | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func (opt *PDFCellOpt) FillColor(cr, cg, cb int) *PDFCellOpt { | ||||||
|  | 	opt.fillColor = langext.Ptr(rgbToColor(cr, cg, cb)) | ||||||
|  | 	return opt | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func (opt *PDFCellOpt) FillColorHex(c uint32) *PDFCellOpt { | ||||||
|  | 	opt.fillColor = langext.Ptr(hexToColor(c)) | ||||||
|  | 	return opt | ||||||
|  | } | ||||||
|  |  | ||||||
| func (b *WPDFBuilder) Cell(txt string, opts ...*PDFCellOpt) { | func (b *WPDFBuilder) Cell(txt string, opts ...*PDFCellOpt) { | ||||||
|  |  | ||||||
| 	txtTR := b.tr(txt) | 	txtTR := b.tr(txt) | ||||||
|  |  | ||||||
| 	width := float64(0) | 	width := float64(0) | ||||||
| 	height := b.cellHeight | 	height := b.cellHeight + b.cellSpacing | ||||||
| 	border := BorderNone | 	border := BorderNone | ||||||
| 	ln := BreakToNextLine | 	ln := BreakToNextLine | ||||||
| 	align := AlignLeft | 	align := AlignLeft | ||||||
| @@ -128,6 +167,10 @@ func (b *WPDFBuilder) Cell(txt string, opts ...*PDFCellOpt) { | |||||||
| 	extraLn := float64(0) | 	extraLn := float64(0) | ||||||
| 	var x *float64 | 	var x *float64 | ||||||
| 	autoWidth := false | 	autoWidth := false | ||||||
|  | 	var textColor *PDFColor | ||||||
|  | 	var borderColor *PDFColor | ||||||
|  | 	var fillColor *PDFColor | ||||||
|  | 	autoWidthPaddingX := float64(0) | ||||||
|  |  | ||||||
| 	for _, opt := range opts { | 	for _, opt := range opts { | ||||||
| 		width = langext.Coalesce(opt.width, width) | 		width = langext.Coalesce(opt.width, width) | ||||||
| @@ -144,6 +187,10 @@ func (b *WPDFBuilder) Cell(txt string, opts ...*PDFCellOpt) { | |||||||
| 		extraLn = langext.Coalesce(opt.extraLn, extraLn) | 		extraLn = langext.Coalesce(opt.extraLn, extraLn) | ||||||
| 		x = langext.CoalesceOpt(opt.x, x) | 		x = langext.CoalesceOpt(opt.x, x) | ||||||
| 		autoWidth = langext.Coalesce(opt.autoWidth, autoWidth) | 		autoWidth = langext.Coalesce(opt.autoWidth, autoWidth) | ||||||
|  | 		textColor = langext.CoalesceOpt(opt.textColor, textColor) | ||||||
|  | 		borderColor = langext.CoalesceOpt(opt.borderColor, borderColor) | ||||||
|  | 		fillColor = langext.CoalesceOpt(opt.fillColor, fillColor) | ||||||
|  | 		autoWidthPaddingX = langext.Coalesce(opt.autoWidthPaddingX, autoWidthPaddingX) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	if fontNameOverride != nil || fontStyleOverride != nil || fontSizeOverride != nil { | 	if fontNameOverride != nil || fontStyleOverride != nil || fontSizeOverride != nil { | ||||||
| @@ -157,12 +204,30 @@ func (b *WPDFBuilder) Cell(txt string, opts ...*PDFCellOpt) { | |||||||
| 		defer func() { b.SetFont(oldFontName, oldFontStyle, oldFontSize) }() | 		defer func() { b.SetFont(oldFontName, oldFontStyle, oldFontSize) }() | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|  | 	if textColor != nil { | ||||||
|  | 		oldColorR, oldColorG, oldColorB := b.b.GetTextColor() | ||||||
|  | 		b.SetTextColor(textColor.R, textColor.G, textColor.B) | ||||||
|  | 		defer func() { b.SetTextColor(oldColorR, oldColorG, oldColorB) }() | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	if borderColor != nil { | ||||||
|  | 		oldColorR, oldColorG, oldColorB := b.b.GetDrawColor() | ||||||
|  | 		b.SetDrawColor(borderColor.R, borderColor.G, borderColor.B) | ||||||
|  | 		defer func() { b.SetDrawColor(oldColorR, oldColorG, oldColorB) }() | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	if fillColor != nil { | ||||||
|  | 		oldColorR, oldColorG, oldColorB := b.b.GetFillColor() | ||||||
|  | 		b.SetFillColor(fillColor.R, fillColor.G, fillColor.B) | ||||||
|  | 		defer func() { b.SetFillColor(oldColorR, oldColorG, oldColorB) }() | ||||||
|  | 	} | ||||||
|  |  | ||||||
| 	if x != nil { | 	if x != nil { | ||||||
| 		b.b.SetX(*x) | 		b.b.SetX(*x) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	if autoWidth { | 	if autoWidth { | ||||||
| 		width = b.b.GetStringWidth(txtTR) | 		width = b.b.GetStringWidth(txtTR) + autoWidthPaddingX | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	b.b.CellFormat(width, height, txtTR, string(border), int(ln), string(align), fill, link, linkStr) | 	b.b.CellFormat(width, height, txtTR, string(border), int(ln), string(align), fill, link, linkStr) | ||||||
|   | |||||||
| @@ -13,6 +13,9 @@ type PDFMultiCellOpt struct { | |||||||
| 	fontSizeOverride  *float64 | 	fontSizeOverride  *float64 | ||||||
| 	extraLn           *float64 | 	extraLn           *float64 | ||||||
| 	x                 *float64 | 	x                 *float64 | ||||||
|  | 	textColor         *PDFColor | ||||||
|  | 	borderColor       *PDFColor | ||||||
|  | 	fillColor         *PDFColor | ||||||
| } | } | ||||||
|  |  | ||||||
| func NewPDFMultiCellOpt() *PDFMultiCellOpt { | func NewPDFMultiCellOpt() *PDFMultiCellOpt { | ||||||
| @@ -86,12 +89,42 @@ func (opt *PDFMultiCellOpt) X(v float64) *PDFMultiCellOpt { | |||||||
| 	return opt | 	return opt | ||||||
| } | } | ||||||
|  |  | ||||||
|  | func (opt *PDFMultiCellOpt) TextColor(cr, cg, cb int) *PDFMultiCellOpt { | ||||||
|  | 	opt.textColor = langext.Ptr(rgbToColor(cr, cg, cb)) | ||||||
|  | 	return opt | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func (opt *PDFMultiCellOpt) TextColorHex(c uint32) *PDFMultiCellOpt { | ||||||
|  | 	opt.textColor = langext.Ptr(hexToColor(c)) | ||||||
|  | 	return opt | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func (opt *PDFMultiCellOpt) BorderColor(cr, cg, cb int) *PDFMultiCellOpt { | ||||||
|  | 	opt.borderColor = langext.Ptr(rgbToColor(cr, cg, cb)) | ||||||
|  | 	return opt | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func (opt *PDFMultiCellOpt) BorderColorHex(c uint32) *PDFMultiCellOpt { | ||||||
|  | 	opt.borderColor = langext.Ptr(hexToColor(c)) | ||||||
|  | 	return opt | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func (opt *PDFMultiCellOpt) FillColor(cr, cg, cb int) *PDFMultiCellOpt { | ||||||
|  | 	opt.fillColor = langext.Ptr(rgbToColor(cr, cg, cb)) | ||||||
|  | 	return opt | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func (opt *PDFMultiCellOpt) FillColorHex(c uint32) *PDFMultiCellOpt { | ||||||
|  | 	opt.fillColor = langext.Ptr(hexToColor(c)) | ||||||
|  | 	return opt | ||||||
|  | } | ||||||
|  |  | ||||||
| func (b *WPDFBuilder) MultiCell(txt string, opts ...*PDFMultiCellOpt) { | func (b *WPDFBuilder) MultiCell(txt string, opts ...*PDFMultiCellOpt) { | ||||||
|  |  | ||||||
| 	txtTR := b.tr(txt) | 	txtTR := b.tr(txt) | ||||||
|  |  | ||||||
| 	width := float64(0) | 	width := float64(0) | ||||||
| 	height := b.cellHeight | 	height := b.cellHeight + b.cellSpacing | ||||||
| 	border := BorderNone | 	border := BorderNone | ||||||
| 	align := AlignLeft | 	align := AlignLeft | ||||||
| 	fill := false | 	fill := false | ||||||
| @@ -100,6 +133,9 @@ func (b *WPDFBuilder) MultiCell(txt string, opts ...*PDFMultiCellOpt) { | |||||||
| 	var fontSizeOverride *float64 | 	var fontSizeOverride *float64 | ||||||
| 	extraLn := float64(0) | 	extraLn := float64(0) | ||||||
| 	var x *float64 | 	var x *float64 | ||||||
|  | 	var textColor *PDFColor | ||||||
|  | 	var borderColor *PDFColor | ||||||
|  | 	var fillColor *PDFColor | ||||||
|  |  | ||||||
| 	for _, opt := range opts { | 	for _, opt := range opts { | ||||||
| 		width = langext.Coalesce(opt.width, width) | 		width = langext.Coalesce(opt.width, width) | ||||||
| @@ -112,6 +148,9 @@ func (b *WPDFBuilder) MultiCell(txt string, opts ...*PDFMultiCellOpt) { | |||||||
| 		fontSizeOverride = langext.CoalesceOpt(opt.fontSizeOverride, fontSizeOverride) | 		fontSizeOverride = langext.CoalesceOpt(opt.fontSizeOverride, fontSizeOverride) | ||||||
| 		extraLn = langext.Coalesce(opt.extraLn, extraLn) | 		extraLn = langext.Coalesce(opt.extraLn, extraLn) | ||||||
| 		x = langext.CoalesceOpt(opt.x, x) | 		x = langext.CoalesceOpt(opt.x, x) | ||||||
|  | 		textColor = langext.CoalesceOpt(opt.textColor, textColor) | ||||||
|  | 		borderColor = langext.CoalesceOpt(opt.borderColor, borderColor) | ||||||
|  | 		fillColor = langext.CoalesceOpt(opt.fillColor, fillColor) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	if fontNameOverride != nil || fontStyleOverride != nil || fontSizeOverride != nil { | 	if fontNameOverride != nil || fontStyleOverride != nil || fontSizeOverride != nil { | ||||||
| @@ -125,6 +164,24 @@ func (b *WPDFBuilder) MultiCell(txt string, opts ...*PDFMultiCellOpt) { | |||||||
| 		defer func() { b.SetFont(oldFontName, oldFontStyle, oldFontSize) }() | 		defer func() { b.SetFont(oldFontName, oldFontStyle, oldFontSize) }() | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|  | 	if textColor != nil { | ||||||
|  | 		oldColorR, oldColorG, oldColorB := b.b.GetTextColor() | ||||||
|  | 		b.SetTextColor(textColor.R, textColor.G, textColor.B) | ||||||
|  | 		defer func() { b.SetTextColor(oldColorR, oldColorG, oldColorB) }() | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	if borderColor != nil { | ||||||
|  | 		oldColorR, oldColorG, oldColorB := b.b.GetDrawColor() | ||||||
|  | 		b.SetDrawColor(borderColor.R, borderColor.G, borderColor.B) | ||||||
|  | 		defer func() { b.SetDrawColor(oldColorR, oldColorG, oldColorB) }() | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	if fillColor != nil { | ||||||
|  | 		oldColorR, oldColorG, oldColorB := b.b.GetFillColor() | ||||||
|  | 		b.SetFillColor(fillColor.R, fillColor.G, fillColor.B) | ||||||
|  | 		defer func() { b.SetFillColor(oldColorR, oldColorG, oldColorB) }() | ||||||
|  | 	} | ||||||
|  |  | ||||||
| 	if x != nil { | 	if x != nil { | ||||||
| 		b.b.SetX(*x) | 		b.b.SetX(*x) | ||||||
| 	} | 	} | ||||||
|   | |||||||
| @@ -34,22 +34,22 @@ func (opt *PDFRectOpt) LineWidth(v float64) *PDFRectOpt { | |||||||
| } | } | ||||||
|  |  | ||||||
| func (opt *PDFRectOpt) DrawColor(cr, cg, cb int) *PDFRectOpt { | func (opt *PDFRectOpt) DrawColor(cr, cg, cb int) *PDFRectOpt { | ||||||
| 	opt.drawColor = &PDFColor{R: cr, G: cg, B: cb} | 	opt.drawColor = langext.Ptr(rgbToColor(cr, cg, cb)) | ||||||
| 	return opt | 	return opt | ||||||
| } | } | ||||||
|  |  | ||||||
| func (opt *PDFRectOpt) DrawColorHex(c uint32) *PDFRectOpt { | func (opt *PDFRectOpt) DrawColorHex(c uint32) *PDFRectOpt { | ||||||
| 	opt.drawColor = &PDFColor{R: int((c >> 16) & 0xFF), G: int((c >> 8) & 0xFF), B: int((c >> 0) & 0xFF)} | 	opt.drawColor = langext.Ptr(hexToColor(c)) | ||||||
| 	return opt | 	return opt | ||||||
| } | } | ||||||
|  |  | ||||||
| func (opt *PDFRectOpt) FillColor(cr, cg, cb int) *PDFRectOpt { | func (opt *PDFRectOpt) FillColor(cr, cg, cb int) *PDFRectOpt { | ||||||
| 	opt.fillColor = &PDFColor{R: cr, G: cg, B: cb} | 	opt.fillColor = langext.Ptr(rgbToColor(cr, cg, cb)) | ||||||
| 	return opt | 	return opt | ||||||
| } | } | ||||||
|  |  | ||||||
| func (opt *PDFRectOpt) FillColorHex(c uint32) *PDFRectOpt { | func (opt *PDFRectOpt) FillColorHex(c uint32) *PDFRectOpt { | ||||||
| 	opt.fillColor = &PDFColor{R: int((c >> 16) & 0xFF), G: int((c >> 8) & 0xFF), B: int((c >> 0) & 0xFF)} | 	opt.fillColor = langext.Ptr(hexToColor(c)) | ||||||
| 	return opt | 	return opt | ||||||
| } | } | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user