Search code examples
gotiedot

Add method to Struct Property of pointer type


I have this struct:

type AppContext struct {
    DB                *db.DB
    Properties        *db.Col
}

Properties of the Type *db.Col is a Tiedot Collection.

The problem I have is that for my buffering system I want to be able to fetch name of the collection. Weirdly enough the default deployment of the library can't do it.

When I instantiate the AppContext like so:

App = AppContext{}

..and then do:

App.DB.Create("Properties")
App.Properties = App.DB.Use("Properties")

I want to add an instance method, but it doesn't allow me to:

func (dbCol App.Properties) ColName() string {
    return "Properties"
}

Any idea how I could accomplish this or maybe extend Tiedot in a smarter way?


Solution

  • I do not think that such extension is possible in Go. However, I sure that the problem can be solved in some other way. For example, you can create structure that holds the collection and its name:

    type Collection struct {
      Col *tiedot.Col
      Name string
    }
    

    and initialise it

    App.Properties = Collection{App.DB.Use("Properties"), "Properties"}