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

Provide ability to read inner structCache #95

Closed
greenpart opened this issue Dec 10, 2015 · 4 comments
Closed

Provide ability to read inner structCache #95

greenpart opened this issue Dec 10, 2015 · 4 comments

Comments

@greenpart
Copy link

I'm building model layer on top of this wonderful library. And it would be great to have read access to structs (structCache).
I've monkey-patched my fork with

func Structs() *structCache {
    return structs
}

and used it to access name from map, pgValue.Source and pgValue.Index.

I think it's good to form low level library interface which allows upper levels on top of it.
What do you think about it?

@vmihailenco
Copy link
Member

I am building model too and it has everything you mentioned :) But it is not ready yet. #96 is what I already have and it can already do simple queries - https://github.com/go-pg/pg/blob/feature/pg-v4/db_test.go#L176-L191. Any help is appreciated but code is not documented (but it should be clean and readable).

@greenpart
Copy link
Author

Great! Hope your model layer will fit my requirements, but now I'm bound to existing codebase.

Anyway, what do you think about opening in terms of layers?

I've inspected code changes on master and found that index became unexported. It's great to know field depth as I need only top level fields. To achieve this we can define:

func (f *field) IsTopLevel() bool {
    return len(f.index) == 1
}

With this func we can iterate through fields.List and filter top level fields. But index values would be required someday too. So, I'm for making it exported. And I still need a way to get fields. Maybe something like this?

func FieldsForType(typ reflect.Type) fields {
    return structs.Fields(typ)
}

@vmihailenco
Copy link
Member

If you are bound to existing codebase then it is easier to just fork the lib and export whatever you need since it is very unlikely that v3 will receive big updates.

Anyway, what do you think about opening in terms of layers?

In v4 I plan to export those API in a subpackage so it does not clatter main API, e.g.:

import "gopkg.in/pg.v4/orm"

article := &MyArticle{}
table := orm.Tables.Get(reflect.TypeOf(article))
for _, field := range table.Fields {
    fmt.Println(field.Name)
    fmt.Println(field.GetValue(article)) // returns reflect.Value
}

@vmihailenco
Copy link
Member

This info is now exported in v4. You can start from reading Table definition https://godoc.org/gopkg.in/pg.v4/orm#Table

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

No branches or pull requests

2 participants