import { BaseVisitor } from "@wapc/widl/ast";
import { capitalize, encode, fieldName, isReference, strQuote, } from "./helpers";
export class EncoderVisitor extends BaseVisitor {
    constructor(writer) {
        super(writer);
    }
    visitObjectFieldsBefore(context) {
        super.triggerObjectFieldsBefore(context);
        this.write(`func (o *${capitalize(context.object.name.value)}) Encode(encoder msgpack.Writer) error {
    if o == nil {
      encoder.WriteNil()
      return nil
    }
    encoder.WriteMapSize(${context.fields.length})\n`);
    }
    visitObjectField(context) {
        const field = context.field;
        this.write(`encoder.WriteString(${strQuote(field.name.value)})\n`);
        this.write(encode(false, "o." + fieldName(field.name.value), field.type, isReference(field.annotations)));
        super.triggerObjectField(context);
    }
    visitObjectFieldsAfter(context) {
        this.write(`
    return nil
  }\n\n`);
        super.triggerObjectFieldsAfter(context);
    }
}
//# sourceMappingURL=encoder_visitor.js.map