Skip to content
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

refactor: Create shorten package #88

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

gabe565
Copy link

@gabe565 gabe565 commented Oct 28, 2022

Description

Refactor codebase to add a shorten package. This allows other projects to use this library and closes #34.

Some notes:

  • Sometimes a dir called pkg is preferred for things that should be public. I could easily move to pkg/shorten. I opted to keep the imports shorter, but let me know if you have a different preference!
  • Most of the funcs in the shorten package could be moved to their own packages in internal. Again, I don't mind doing the refactoring if you'd prefer that.
  • To simplify usage in other projects, I have added shorten.DefaultConfig() which returns a shorten.Config struct that matches the default flag values. I have also changed shorten.New() so that config is an optional parameter. If not passed, the default config is used.
  • I renamed most funcs and vars to make them private. Only shorten.New(), shorten.DefaultConfig(), and Shortener.Shorten() are exposed.
  • All tests are passing.

Unfortunately, this PR required some major refactoring and will create merge conflicts with any other open PRs.

Let me know if you have any suggestions!

Example: Usage in another project

main.go
package main

import (
	"fmt"
	"github.com/segmentio/golines/shorten"
)

var code = `package main

var myMap = map[string]string{"key1": "value1", "key2": "value2", "key3": "value3", "key4": "value4", "key5", "value5"}
`


func main() {
	shortener := shorten.New()

	out, err := shortener.Shorten([]byte(code))
	if err != nil {
		panic(err)
	}

	fmt.Print(string(out))
}

Todo

  • Add a quick section to the readme that shows usage from other packages.

@mdwhatcott
Copy link

I was just looking for a code-shortener-as-a-library and found this project, which is just what I was hoping for! My usecase includes code generation and I was hoping for a library rather than a command. Anything holding up this PR from being merged?

@yipal
Copy link

yipal commented Aug 13, 2024

I would also love to see this merged.

@gabe565
Copy link
Author

gabe565 commented Aug 14, 2024

This repo seems pretty inactive, but I can rebase this PR if there's a chance it'll be merged! It might be worth it to investigate gofumpt at this point. Looks like it'll split long lines if the env GOFUMPT_SPLIT_LONG_LINES=on is set

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.

Plans to provide this utility as a library to be used within other projects
3 participants