jackc / pglogrepl

PostgreSQL logical replication library for Go.
MIT License
330 stars 62 forks source link

Add SetType to WAL message structs #18

Closed diabloneo closed 3 years ago

diabloneo commented 3 years ago

The SetType() function allows application code using this library to create unittest code by constructing a object of specific message type.

Signed-off-by: diabloneo diabloneo@gmail.com

Because the Parse() function returns an interface type Message which has a method tells message type. The application may has the form:

func processWALMessage(msg pglogrepl.Message) (err error) {
    switch msg.Type() {
    case pglogrepl.MessageTypeInsert:
    ...
    }

    return nil
}

So, the way to write unittest code for the function requires setting message type explicitly which has not supported yet. After adding the method, the unittest code can wirte like this:

func TestProcessWALMessage(t *testing.T) {
    msg := &pglogrepl.InsertMessage{
        RelationID: 16789,
        Tuple: &pglogrepl.TupleData{},
    }
    msg.SetType(pglogrepl.MessageTypeInsert)
    err := processWALMessage(msg)
    ...
}