cbadawi / go-stacks

Other
2 stars 1 forks source link

Unable to use structs inside internal package #3

Open chrisbward opened 3 months ago

chrisbward commented 3 months ago

Hi,

I'm trying to write a controller for the basic demo in readme, but seems like I can't use the structs inside internal, Go doesn't like it much.

package stackscontroller

import (
    "context"
    "net/http"

    "github.com/cbadawi/go-stacks/stacks/api"
    "github.com/cbadawi/go-stacks/stacks/config"
    "github.com/cbadawi/go-stacks/stacks/internal/models"
)

type StacksController struct {
    ApiClient *api.APIClient
}

func NewStacksController(verboseLogger bool, configuration *config.Configuration) *StacksController {

    configuration = config.NewConfiguration()
    configuration.Logger.Verbose = verboseLogger // defaults to false
    // configuration.Host = "api.testnet.hiro.so" // can define a custom host
    apiClient := api.NewAPIClient(configuration)

    return &StacksController{
        ApiClient: apiClient,
    }
}

func (sc *StacksController) GetAccountBalance(ctx context.Context, principal string, unanchored bool, untilBlock string) (resp *models.AddressBalanceResponse, r *http.Response, err error) {

    return sc.ApiClient.AccountsAPI.GetAccountBalance(context.Background(), principal).Unanchored(unanchored).UntilBlock(untilBlock).Execute()
}

Been considering a couple of workarounds;

This is the first time I've come across "internal", so probably just a skill issue!

chrisbward commented 3 months ago

for reference, noticed this; https://github.com/OpenAPITools/openapi-generator/issues/1931

cbadawi commented 3 months ago

@chrisbward internal packages cant be exported. Normally internal is used to store packages you dont want to export such as configurations but having models in there is also good practice. In this case, I can see why having access to the models is more typesafe so I will take them out of internal. Thank you for your feedback.

chrisbward commented 3 months ago

Thanks Carl, but is there a way we can do it via the generation tool? Thinking that if the spec changes on the Stacks side, then it's a manual job to run again!

(keen to collaborate!)

cbadawi commented 3 months ago

The generation tool is un-opinionated regarding packages and provides them as is. We can potentially write a bash script that organizes the packages. There was a non-trivial amount manual work after the autogeneration that would be helpful to automate/fix (logger, error handling, packages reorg, issues with the spec etc...).

chrisbward commented 3 months ago

Interested to understand what that work involved - would like to replicate on my side!

assuming you were using this; https://github.com/OpenAPITools/openapi-generator