Skip to content

Adds a WithID method to allow setting IDs with names (not just types) #9670

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

Conversation

hughesjj
Copy link

@hughesjj hughesjj commented Mar 2, 2024

Description:
Optional PR inspired by -contrib's 31457 I came across 9473

I recently had to update a bunch of code related to the Type regex lockdown (good thing imo! I'm glad we're doing it!) and wanted some functionality like this

  1. The validation logic is only "accessible" outside of the component package if you call NewType (one one of the Must functions which calls such), but a lot of legacy code directly instantiates Type. While that can always be refactored, I thought having a Validate command would be less invasive and allows us to centralize our validation logic. Might want to add it to component.ID as well.
  2. It's a tad annoying that we take a raw string value for the constructor of scraper when the underlying member variable is an ID. IMHO an ideal world would have us either change the type of that member to component.Type, or permit the constructor to take in any valid component.ID.

Also, as of now we don't length-limit our types, but we may want to start doing that...

Copy link

codecov bot commented Mar 2, 2024

Codecov Report

Attention: Patch coverage is 82.35294% with 3 lines in your changes are missing coverage. Please review.

Project coverage is 90.92%. Comparing base (412ed26) to head (990cc47).
Report is 23 commits behind head on main.

Files Patch % Lines
receiver/scraperhelper/scraper.go 57.14% 2 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #9670      +/-   ##
==========================================
+ Coverage   90.89%   90.92%   +0.03%     
==========================================
  Files         347      347              
  Lines       18326    18359      +33     
==========================================
+ Hits        16657    16693      +36     
+ Misses       1344     1342       -2     
+ Partials      325      324       -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Member

@bogdandrutu bogdandrutu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need a deeper refactoring/cleaning of the Scraper, not sure if it is ok to add this now.

@@ -123,19 +123,22 @@ func (t Type) String() string {
// This must be kept in sync with the regex in cmd/mdatagen/validate.go.
var typeRegexp = regexp.MustCompile(`^[a-zA-Z][0-9a-zA-Z_]*$`)

const NoType = Type("")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you need this public?

Copy link
Author

@hughesjj hughesjj Mar 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't. We could re-use it downstream or to otherwise identify the "empty" type (the PR on my end which inspired this does a NoType locally), but you're right that this shouldn't be introduced as part of the API by making it public.

(technically my test cases in scraperhelper require this but I could move them to config)

@@ -151,6 +154,13 @@ func MustNewType(strType string) Type {
return ty
}

func (t Type) Validate() error {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mx-psi do I remember wrongly or you were working to change this to be a struct? So this is not needed since we cannot construct an invalid type.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, see #9472 which is ready to merge. I think that PR would make this one unnecessary since it would be impossible to construct an invalid type

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, technically we still don't allow empty string names for type right? Given the regex does not permit empty strings (also we should probably add a generous length limit imho)

We can move this discussion to a bespoke issue to get some more clarity of thought.

The point w.r.t scraperhelper constructing a nameless ID still exists, unless that's desired. If the current behavior is desired, I'd argue Type would be a better type than ID for the member variable

Regardless, it's probably best to close this PR out in favor of an issue, will comment in 9473, we can spin another out if need be

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, technically we still don't allow empty string names for type right? Given the regex does not permit empty strings

I don't think we should allow empty names, at least not through an empty component.Type, but I am not sure what you want to accomplish exactly, so it's better if we discuss in an issue

(also we should probably add a generous length limit imho)

Would you be open to filing an issue about this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants