Skip to content
This repository was archived by the owner on Dec 19, 2025. It is now read-only.

refactor: Create shorten package#88

Open
gabe565 wants to merge 3 commits into
segmentio:masterfrom
gabe565:package
Open

refactor: Create shorten package#88
gabe565 wants to merge 3 commits into
segmentio:masterfrom
gabe565:package

Conversation

@gabe565

@gabe565 gabe565 commented Oct 28, 2022

Copy link
Copy Markdown

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
Copy Markdown

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

yipal commented Aug 13, 2024

Copy link
Copy Markdown

I would also love to see this merged.

@gabe565

gabe565 commented Aug 14, 2024

Copy link
Copy Markdown
Author

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 subscribe to this conversation on GitHub. Already have an account? Sign in.

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