YZ1 (Expected simple string or bulk string, got NIL)

I’m stuck on Stage #YZ1

I’ve tried …

Here are my logs:

response "$-1\r\n"
[tester::#LA7] Received bytes: "$-1\r\n"
[tester::#LA7] Received RESP null bulk string: "$-1\r\n"
[tester::#LA7] Expected simple string or bulk string, got NIL
[tester::#LA7] Test failed
[tester::#LA7] Terminating program
[tester::#LA7] Program terminated successfully

And here’s a snippet of my code:


const delimiter = '\r\n'
export class RespSerializer {
    public static serialize(data: IRespData | null) {
        if (!data) {
            return RespSerializer.serializeBulkString(null)
        }
        console.log("data", data)
        switch (data.type) {
            case RespDataType.ArrayType:
                return RespSerializer.serializeArrays(data.value as IRespData[])
            case RespDataType.BulkString:
                return RespSerializer.serializeBulkString(data.value as string)
            case RespDataType.SimpleString:
                return RespSerializer.serializeSimpleString(data.value as string)
            case RespDataType.Integer:
                return RespSerializer.serializeInteger(data.value as number)
            case RespDataType.Error:
                return RespSerializer.serializeError(data.value as string)
            default:
            //handle default
        }
    }

    public static serializeSimpleString(str: string) {
        return `${RespDataType.SimpleString}${str}${delimiter}`
    }
    public static serializeInteger(num: number) {
        return `${RespDataType.Integer}${num}${delimiter}`
    }
    public static serializeError(str: string) {
        return `${RespDataType.Error}${str}${delimiter}`
    }
    public static serializeBulkString(str: string | null) {
        if (str === null) {
            return `${RespDataType.BulkString}${-1}${delimiter}`
        }
        return `${RespDataType.BulkString}${str.length}${delimiter}${str}${delimiter}`
    }
    public static serializeArrays(data: IRespData[]) {
        let res = ""
        data.map((item) => {
            const itemStr = this.serialize(item)
            res += itemStr
        })
        return `${RespDataType.ArrayType}${data.length}${delimiter}${res}`
    }
}
public static serializeBulkString(str: string | null) {
        if (str === null) {
            return `${RespDataType.BulkString}${-1}${delimiter}`
        }
        return `${RespDataType.BulkString}${str.length}${delimiter}${str}${delimiter}`
    }

@softwarelifter Your response was correct for Stage #YZ1.

The error occurred when our tester tried to test the previous stage (Implement the SET & GET commands #la7) after your code had passed #YZ1.

It’s common for changes made in later stages to inadvertently break earlier ones. You can install our CLI and test against previous stages first by running this command:

codecrafters test --previous

(There are actually several earlier stages that are broken, not just #la7.)

@andy1li thanks that was helpful, it’s working now

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.