Skip to content

Instantly share code, notes, and snippets.

@dolmen
Last active April 23, 2020 08:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dolmen/6a847436476785e9c87d7cc69f23f66b to your computer and use it in GitHub Desktop.
Save dolmen/6a847436476785e9c87d7cc69f23f66b to your computer and use it in GitHub Desktop.
Design for a Go tool for moving symbols from a package to another: gomvfile

gomvfile design draft

This is a design draft for a tool (gomvfile) that would allow to migrate a set of symbols from a package to another.

Strategy for symbols migration

  1. Isolate symbols that have to move: move all symbols to migrate into one or multiple separate source files. Those sources must not have depenencies on the internals (private symbols, private struct members) of the rest of the package.
  2. Use gomvfile to migrate the files to the target package

gomvfile

The task of the gomvfile tool (input: a set of files in a single package ($files in package $src), and a destination package $dst):

  1. Collect the list of package block symbols in $files
    • exported => $symbols_pub
    • private => $symbols_priv
  2. Check that $dst has no package block scoped symbol with name in ($symbols_pub, $symbols_priv)
  3. For each file in $src excluding $files:
    • check that none of $symbols_priv and private struct members defined in $files are used
  4. Collect every source file in $GOPATH (excluding $files) that:
    • imports one symbol of $symbols_pub from $src => $src_users
    • imports $dst: warn if $dst is imported as '.' (or better, check that there will be no import conflict)
  5. Move $files from $src to $dst
    • move files
    • fix imports (remove imports of $dst, add imports of $src)
  6. For files in $src_users:
    • determine the alias for importing $dst
    • if import of $dst exists, use the existing alias
    • add import of $dst if necessary

    Note: check the case where $dst and $src have the same package name (short part)

    • replace symbols $symbols_pub
    • remove import of $src if none of its symbols are used anymore (maybe delagate this to goimports)
    • save

TODO:

  • handle test sources among $files:
    • which are declared in package $src (maybe there is nothing special to do)
    • which are declared in another package (usually "${src}_test")
  • update the proposal now that Go has type aliases which was not the case when the proposal has originally been designed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment