// generateMapFunctions creates the converter functions
generateMapFunctions creates the converter functions
// generateMapFunctions creates the converter functions
func (p *OrmPlugin) generateConvertFunctions(message pgs.Message) {
	typeName := p.TypeName(message)
	ormable := p.getOrmable(generator.CamelCase(message.FullyQualifiedName()))
	p.P(`// ToORM runs the BeforeToORM hook if present, converts the fields of this`)
	p.P(`// object to ORM format, runs the AfterToORM hook, then returns the ORM object`)
	p.P(`func (m *`, typeName, `) ToORM (ctx context.Context) (`, typeName, `ORM, error) {`)
	p.P(`to := `, typeName, `ORM{}`)
	p.P(`var err error`)
	p.P(`if prehook, ok := interface{}(m).(`, typeName, `WithBeforeToORM); ok {`)
	p.P(`if err = prehook.BeforeToORM(ctx, &to); err != nil {`)
	p.P(`return to, err`)
	p.P(`}`)
	p.P(`}`)
	for _, field := range message.Fields() {
		if getFieldOptions(field).GetDrop() {
			continue
		}
		ofield := ormable.Fields[generator.CamelCase(string(field.Name()))]
		p.generateFieldConversion(message, field, true, ofield)
	}
	p.P(`if posthook, ok := interface{}(m).(`, typeName, `WithAfterToORM); ok {`)
	p.P(`err = posthook.AfterToORM(ctx, &to)`)
	p.P(`}`)
	p.P(`return to, err`)
	p.P(`}`)
	p.P()
	p.P(`// ToPB runs the BeforeToPB hook if present, converts the fields of this`)
	p.P(`// object to PB format, runs the AfterToPB hook, then returns the PB object`)
	p.P(`func (m *`, typeName, `ORM) ToPB (ctx context.Context) (`,
		typeName, `, error) {`)
	p.P(`to := `, typeName, `{}`)
	p.P(`var err error`)
	p.P(`if prehook, ok := interface{}(m).(`, typeName, `WithBeforeToPB); ok {`)
	p.P(`if err = prehook.BeforeToPB(ctx, &to); err != nil {`)
	p.P(`return to, err`)
	p.P(`}`)
	p.P(`}`)
	for _, field := range message.Fields() {
		if getFieldOptions(field).GetDrop() {
			continue
		}
		ofield := ormable.Fields[generator.CamelCase(string(field.Name()))]
		p.generateFieldConversion(message, field, false, ofield)
	}
	p.P(`if posthook, ok := interface{}(m).(`, typeName, `WithAfterToPB); ok {`)
	p.P(`err = posthook.AfterToPB(ctx, &to)`)
	p.P(`}`)
	p.P(`return to, err`)
	p.P(`}`)
}
func ( p * OrmPlugin ) generateConvertFunctions ( message pgs . Message ) { typeName := p . TypeName ( message ) ormable := p . getOrmable ( generator . CamelCase ( message . FullyQualifiedName ( ) ) ) p . P ( `// ToORM runs the BeforeToORM hook if present, converts the fields of this` ) p . P ( `// object to ORM format, runs the AfterToORM hook, then returns the ORM object` ) p . P ( `func (m *` , typeName , `) ToORM (ctx context.Context) (` , typeName , `ORM, error) {` ) p . P ( `to := ` , typeName , `ORM{}` ) p . P ( `var err error` ) p . P ( `if prehook, ok := interface{}(m).(` , typeName , `WithBeforeToORM); ok {` ) p . P ( `if err = prehook.BeforeToORM(ctx, &to); err != nil {` ) p . P ( `return to, err` ) p . P ( `}` ) p . P ( `}` ) for _ , field := range message . Fields ( ) { if getFieldOptions ( field ) . GetDrop ( ) { continue } ofield := ormable . Fields [ generator . CamelCase ( string ( field . Name ( ) ) ) ] p . generateFieldConversion ( message , field , true , ofield ) } p . P ( `if posthook, ok := interface{}(m).(` , typeName , `WithAfterToORM); ok {` ) p . P ( `err = posthook.AfterToORM(ctx, &to)` ) p . P ( `}` ) p . P ( `return to, err` ) p . P ( `}` ) p . P ( ) p . P ( `// ToPB runs the BeforeToPB hook if present, converts the fields of this` ) p . P ( `// object to PB format, runs the AfterToPB hook, then returns the PB object` ) p . P ( `func (m *` , typeName , `ORM) ToPB (ctx context.Context) (` , typeName , `, error) {` ) p . P ( `to := ` , typeName , `{}` ) p . P ( `var err error` ) p . P ( `if prehook, ok := interface{}(m).(` , typeName , `WithBeforeToPB); ok {` ) p . P ( `if err = prehook.BeforeToPB(ctx, &to); err != nil {` ) p . P ( `return to, err` ) p . P ( `}` ) p . P ( `}` ) for _ , field := range message . Fields ( ) { if getFieldOptions ( field ) . GetDrop ( ) { continue } ofield := ormable . Fields [ generator . CamelCase ( string ( field . Name ( ) ) ) ] p . generateFieldConversion ( message , field , false , ofield ) } p . P ( `if posthook, ok := interface{}(m).(` , typeName , `WithAfterToPB); ok {` ) p . P ( `err = posthook.AfterToPB(ctx, &to)` ) p . P ( `}` ) p . P ( `return to, err` ) p . P ( `}` ) }
generateMapFunctions creates the converter functions
generateMapFunctions creates the converter functions
///// To Orm // Checking if field is skipped ///// To Pb // Checking if field is skipped