-
-
Notifications
You must be signed in to change notification settings - Fork 184
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Daniil Stepanenko <daniil.stepanenko.96@gmail.com>
- Loading branch information
1 parent
bcb33a2
commit 7d9034e
Showing
9 changed files
with
713 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package funcs | ||
|
||
import ( | ||
"context" | ||
"log/slog" | ||
) | ||
|
||
const TraceFuncsLevel = slog.LevelDebug - 1 | ||
|
||
func trace(ctx context.Context, msg string, attrs ...slog.Attr) { | ||
slog.LogAttrs(ctx, TraceFuncsLevel, msg, attrs...) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package funcs | ||
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
"strings" | ||
) | ||
|
||
var _ fmt.Stringer = (*namespace)(nil) | ||
|
||
type namespace struct { | ||
self any //must be pointer to outer struct | ||
} | ||
|
||
func (n *namespace) String() string { | ||
ns := n.self | ||
if ns == nil { | ||
return "<namespace>" | ||
} | ||
|
||
nsType := reflect.TypeOf(ns) | ||
if nsType.Kind() != reflect.Pointer || nsType.Elem().Kind() != reflect.Struct { | ||
panic("invalid namespace type " + nsType.String() + ": must be pointer to struct") | ||
} | ||
|
||
var public []string | ||
public = appendPublicMethods(nsType, public) | ||
nsType = nsType.Elem() | ||
public = appendPublicFields(nsType, public) | ||
|
||
nsName := nsType.String() | ||
nsName = strings.TrimPrefix(nsName, "funcs.") | ||
nsName = strings.TrimSuffix(nsName, "Funcs") | ||
|
||
return fmt.Sprintf("<namespace %s %s>", nsName, public) | ||
} | ||
|
||
func appendPublicFields(nsType reflect.Type, public []string) []string { | ||
for _, field := range reflect.VisibleFields(nsType) { | ||
if !field.IsExported() { | ||
continue | ||
} | ||
|
||
public = append(public, field.Name) | ||
} | ||
|
||
return public | ||
} | ||
|
||
func appendPublicMethods(nsType reflect.Type, public []string) []string { | ||
for i := range nsType.NumMethod() { | ||
method := nsType.Method(i) | ||
if !method.IsExported() { | ||
continue | ||
} | ||
|
||
if method.Name == "String" && nsType.Implements(reflect.TypeFor[fmt.Stringer]()) { | ||
continue | ||
} | ||
|
||
public = append(public, method.Name) | ||
} | ||
|
||
return public | ||
} |
Oops, something went wrong.