go-reform/reform

Allow non-exported fields with reform tag

Opened this issue · 2 comments

Consider the following code:

package audit

type Message struct {
	ID      string `reform:"id,pk"`
	Method  string `reform:"method"`
	Payload interface{}
	body    []byte `reform:"body"`
}

func (m *Message) BeforeInsert() error {
	b, err := json.Marshal(m.Payload)
	m.body = b
	return err
}

An external user of that package can set Payload, but it is always stored in serialized form.

Currently, it is not possible due to check in reform tool: Message has non-exported field body with "reform:" tag, it is not allowed. But we don't have a reason for that check anymore – generated code can access this field directly, no reflection (which almost always works only for exported fields) is required.

@rumyantseva Does it make sense to you?

@AlekSi I'm afraid, I'm not 100% understand the problem. What is the goal? Is the idea that an external user can set a field in a very "generic" form (interface{}) but wants to serialize it in a specific manner before insert? Could you give me an example? Smth about JSONB fields in Postgres?

I'd say this implementation look like "костыль" a little bit but on the other hand it gives some flexibility. Let me think about it a little bit.