alvaroloes / enumer

A Go tool to auto generate methods for your enums
Other
480 stars 111 forks source link

Support prefix subtraction from variable name when generating string method #18

Closed itsjamie closed 6 years ago

itsjamie commented 7 years ago

Since the variable name becomes the enum string, it would be nice to be able to define a Prefix or Suffix string that should be removed.

For example, while adding enums for Cloudfront log parsing.

// EdgeResultType represents a specific result type that is represented in the Logline
type EdgeResultType int

const (
    // Hit means the object was served to the viewer from the edge cache.
    Hit EdgeResultType = iota
    // RefreshHit means the object was in the edge cache but it had expired, so CloudFront contacted the origin to verify that the cache has the latest version of the object.
    RefreshHit
    // Miss means the request could not be satisfied by an object in the edge cache, so CloudFront forwarded the request to the origin server and returned the result to the viewer.
    Miss
    // LimitExceeded means the request was denied because a CloudFront limit was exceeded.
    LimitExceeded
    // CapacityExceeded returns a 503 error because the edge location didn't have enough capacity at the time of the request to serve the object
    CapacityExceeded
    // Error means the request resulted in a client error (sc-status is 4xx) or a server error (sc-status is 5xx).
    Error
    // Redirect means that we redirected from HTTP to HTTPS. Alternatively, if the status is 403 and the distribution is configured CloudFront to restrict the geographic distribution of your content, the request might have come from a restricted location.
    // If the value of x-edge-result-type is Error and the value of x-edge-response-result-type is not Error, the client disconnected before finishing the download.
    Redirect
)

It would be useful to be able to Prefix these enums to be EdgeResultHit, EdgeResultRefreshHit, but keep the generated string as Hit, RefreshHit, for actual parsing.

itsjamie commented 7 years ago

Currently, we could move them into their own subpackage so usage would become edgeresult.Hit, but that becomes unwieldy with multiple enums.

tonyhb commented 7 years ago

+1 This would be dope!

alvaroloes commented 6 years ago

Hi! Thank you very much for filling the issue! I've been very busy this year, but now I'm starting to have time again. So I expect to have time to review this soon 😉

alvaroloes commented 6 years ago

@itsjamie I am willing to merge #15 to solve this. Waiting for the author of that PR to make some changes

itsjamie commented 6 years ago

@alvaroloes Thanks!

alvaroloes commented 6 years ago

Done!