aboutsummaryrefslogtreecommitdiff
path: root/lib/reflect
AgeCommit message (Collapse)Author
11 daysall: apply go fixShulhan
2025-01-23all: use for-range with numericShulhan
Go 1.22 now support for-range on numeric value.
2025-01-23all: replace "interface{}" with "any"Shulhan
2024-12-28lib/reflect: refactor the Equaler method EqualShulhan
This changes the Equal signature from "Equal(v any) bool" to "Equal(v any) error". The reason for this changes is to force the method to return an error message that is understand-able by caller. While at it, simplify checking for Equaler interface in internal doEqualStruct function.
2024-03-05all: comply with linter recommendations #2Shulhan
HTTP request now implicitly create request with context. Any false positive related to not closing HTTP response body has been annotated with "nolint:bodyclose". In the example code, use consistent "// Output:" comment format, by prefixing with single space. Any comment on code now also prefixing with single space. An error returned without variables now use [errors.New] instead of [fmt.Errorf]. Any error returned using [fmt.Errorf] now wrapped using "%w" instead of "%s". Also, replace error checking using [errors.Is] or [errors.As], instead of using equal/not-equal operator. Any statement like "x = x OP y" now replaced with "x OP= y". Also, swap statement is simplified using "x, y = y, x". Any switch statement with single case now replaced with if-condition. Any call to defer on function or program that call [os.Exit], now replaced by calling the deferred function directly. Any if-else condition now replaced with switch statement, if possible.
2024-03-02all: move the repository to "git.sr.ht/~shulhan/pakakeh.go"Shulhan
There are several reasons that why we move from github.com. First, related to the name of package. We accidentally name the package with "share" a common word in English that does not reflect the content of repository. By moving to other repository, we can rename it to better and unique name, in this "pakakeh.go". Pakakeh is Minang word for tools, and ".go" suffix indicate that the repository related to Go programming language. Second, supporting open source. The new repository is hosted under sourcehut.org, the founder is known to support open source, and all their services are licensed under AGPL, unlike GitHub that are closed sources. Third, regarding GitHub CoPilot. The GitHub Terms of Service [1], allow any public content that are hosted there granted them to parse the content. On one side, GitHub helps and flourish the open source, but on another side have an issues regarding scraping the copyleft license [2]. [1]: https://docs.github.com/en/site-policy/github-terms/github-terms-of-service#4-license-grant-to-us [2]: https://githubcopilotinvestigation.com
2023-12-13all: fix linter warnings reported by reviveShulhan
There are some reports that I disagree with revive, in example, code should not declare the type after variables. In my opinion, on some cases, declaring the type make the code more readable and explicit. Since I did not want to add new configuration file, we changes it and follow revive for now.
2023-04-13lib/reflect: change the package name in Example to have _test suffixShulhan
Package name in test Example should be different with the actual package. This is to minimize leaking un-exported function or method in Example test.
2023-04-13lib/reflect: use doEqual inside IsEqualShulhan
Previously, IsEqual internally use isEqual, which have the same logic as doEqual. This changes minimize duplicate code between IsEqual and DoEqual, by calling doEqual for both of functions.
2023-04-13lib/reflect: add option to skip processing struct field in Do/IsEqualShulhan
A struct's field tagged with `noequal:""`, its value will not be processed for equality.
2023-04-13lib/reflect: remove the third return value from MarshalShulhan
The third, boolean, return value is redundant with the second error value.
2022-09-15lib/reflect: remove prefix from returned error on DoEqualShulhan
Prefixing an error cause may cause confusion when used on lib/test.Assert. The returned error from test.Assert will print "DoEqual: ...", where user never call DoEqual in their test.
2022-06-25lib/reflect: add function MarshalShulhan
The Marshal function marshal the obj value to []byte by calling one of the method: MarshalBinary, MarshalJSON, or MarshalText; in respective order. If obj implement one of the method with valid signature, it will return (out, nil, true); unless there is an error. If the method signature invalid it will return (nil, err, false). If obj is nil or none of the method exist it will return (nil, nil, false).
2022-06-24lib/reflect: simplify example using fmt.PrintlnShulhan
2022-06-24lib/reflect: support excluding field using "-" on TagShulhan
If field tagged with "-" the Tag function will return empty value, nil options, and false.
2022-06-08lib/reflect: implement Set function to set reflect.Value by stringShulhan
The Set function set the obj value by converting the string val from parameter to the obj type. If the obj is an interface or struct, its value will be set by calling Unmarshal. It will return an error if, - obj is not setable, variable is passed without pointer or pointer not initialized. - val is overflow - obj Kind is Invalid, Array, Chan, Func, Map, or UnsafePointer.
2022-06-07lib/reflect: rename example fileShulhan
No need to prefix the filename with the package name itself.
2022-06-05lib/reflect: add function UnmarshalShulhan
The Unmarshal function set the obj value by calling one of the method: UnmarshalBinary, UnmarshalJSON, or UnmarshalText; in respective order. Just like reflect, the obj value must be pointer to initialized variable (&T) or pointer-to-pointer to uninitialized variable (**T). If obj implement one of the method, it will return (true, nil) if there is no error. If none of the method exist on obj, it will return (false, nil).
2022-06-01lib/reflect: add function Tag to simplify lookup on struct's field tagShulhan
Given a StructField and the name of tag, return the tag's value and options inside the tag. The options is any string after tag's value, separated by comma. For example, given the following field definition F `tag:"name,opt1, opt2"` It will return (name, [opt1 opt2], true). If the field is exported but does not have tag, it will return the field name (as is without converting to lower case) in val with hasTag is false: (Name, nil, false). If the field is un-exported it will return empty val with hasTag is false ("", nil, false).
2022-06-01lib/reflect: cleanup codes and fix some commentsShulhan
The code cleanup consists of replacing variable assignment from using ":=" to use var declaration so the reader can know the type result from assignment. The fix on comments are misspelling of "Uninitialized" in the example.
2022-05-09all: reformat all codes using gofmt 1.19 (the Go tip)Shulhan
2021-08-25lib/reflect: make the IsNil tests to become an exampleShulhan
In this way we do test and provide an example at the same time. While at it, add another test cases for boolean, initialized slice, map, and errors.
2021-03-01reflect: check using equal statement on IsNilShulhan
Beside using IsNil(), return the last check using "v == nil".
2020-11-23test: refactoring Assert with better error messageShulhan
The new Assert function use the reflect.DoEqual that return an error which describe which field have unmatched value. This changes affect other test that use "false" as the last parameter.
2020-06-10all: update email addressShulhan
2020-06-06all: use default linter optionsShulhan
2020-04-23reflect: remove checking for method IsEqual on type reflect.PtrShulhan
The check for method IsEqual only happened if type is struct. This changes affect the test on package math/big.
2020-03-26all: fix and suppress linter warningsShulhan
2020-03-20reflect: add function IsEqual that works with Equaler interfaceShulhan
The IsEqual() function is like reflect.DeepEqual but its check if a struct have method "IsEqual", if its exist it will call the method to compare the value.
2020-03-20reflect: new package that extends the standard reflectShulhan
This package add new interface "Equaler", it is an interface that have single method "IsEqual()". Also, we have new function "IsNil(interface{})" that will return true if the value in interface{} is nil.