-
Notifications
You must be signed in to change notification settings - Fork 78
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add codec method to retrieve a Go type registered name #316
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -308,6 +308,30 @@ func (cdc *Codec) PrintTypes(out io.Writer) error { | |
return nil | ||
} | ||
|
||
// ConcreteRegisteredName returns the name under which i has been registered. | ||
func (cdc *Codec) ConcreteRegisteredName(i interface{}) (string, error) { | ||
cdc.mtx.RLock() | ||
defer cdc.mtx.RUnlock() | ||
|
||
if i == nil { | ||
return "", errors.New("argument must not be nil") | ||
} | ||
|
||
gt := reflect.TypeOf(i) | ||
|
||
// if i is a non-nil pointer, dereference it and grab its inner Elem | ||
if gt.Kind() == reflect.Ptr { | ||
gt = gt.Elem() | ||
} | ||
|
||
ct, ok := cdc.typeInfos[gt] | ||
if !ok { | ||
return "", fmt.Errorf("no type registered for %s", gt.String()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should probably be a sentinel error, such that callers can switch over a returned error to see if the type wasn't registered or if any other error occurred. And independent of that: I think There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would those two errors be okay for the purpose?
Yup, |
||
} | ||
|
||
return ct.Name, nil | ||
} | ||
|
||
// A heuristic to guess the size of a registered type and return it as a string. | ||
// If the size is not fixed it returns "variable". | ||
func getLengthStr(info *TypeInfo) string { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What happens if this a **SomeType? (pointer to pointer)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Uhm, **SomeType would return error even though the struct type has been registered previously.
I could add a check on the inner gt.Elem(), this way it would dereference 2 times and get to the concrete instance.
Thoughts?
Something like: