Response Transformation Pipeline in Go
This challenge focuses on building a flexible response transformation pipeline in Go. You'll design a system that takes an initial response (represented as a string) and applies a series of transformations to it, ultimately producing a modified response. This is useful for tasks like formatting API responses, sanitizing user input, or applying custom logic to data before it's displayed.
Problem Description
You are tasked with creating a ResponseTransformer in Go. This transformer will accept an initial response string and a slice of transformation functions. Each transformation function takes a string as input and returns a modified string. The ResponseTransformer should apply these transformations sequentially to the initial response, returning the final transformed string.
Key Requirements:
- Transformation Functions: Define a type for transformation functions:
type TransformerFunc func(string) string. - ResponseTransformer Struct: Create a
ResponseTransformerstruct that holds a slice ofTransformerFunc. - Transform Method: Implement a
Transformmethod on theResponseTransformerstruct. This method takes an initial response string as input and applies each transformation function in the slice sequentially. - Error Handling: The
Transformmethod should return an error if any of the transformation functions panic. This prevents a single failing transformation from crashing the entire pipeline. - Immutability: The original response string should not be modified directly. Each transformation should return a new string.
Expected Behavior:
The Transform method should iterate through the transformation functions in the order they are defined in the ResponseTransformer's slice. The output of one transformation becomes the input of the next. If no transformation functions are provided, the initial response should be returned unchanged.
Edge Cases to Consider:
- Empty slice of transformation functions.
- Transformation functions that return empty strings.
- Transformation functions that panic.
- Large input strings (consider potential memory usage).
Examples
Example 1:
Input: Initial Response: "Hello, world!", Transformations: [ToUpper, TrimSpace]
Output: "HELLO,WORLD"
Explanation: The `ToUpper` transformation converts "Hello, world!" to "HELLO, world!". The `TrimSpace` transformation removes leading/trailing whitespace, resulting in "HELLO,WORLD".
Example 2:
Input: Initial Response: " Go is fun! ", Transformations: [TrimSpace, LowerCase]
Output: "go is fun!"
Explanation: `TrimSpace` removes the leading and trailing spaces, resulting in "Go is fun!". `LowerCase` converts the string to lowercase, resulting in "go is fun!".
Example 3: (Edge Case - Empty Transformations)
Input: Initial Response: "Original", Transformations: []
Output: "Original"
Explanation: Since there are no transformations, the original response is returned.
Example 4: (Edge Case - Panicking Transformation)
Input: Initial Response: "Test", Transformations: [PanicTransformer]
Output: Error: "panic: test"
Explanation: The `PanicTransformer` is designed to panic. The `Transform` method should catch the panic and return an error.
Constraints
- The input response string can be up to 1000 characters long.
- The number of transformation functions in the slice can be up to 10.
- Transformation functions should be relatively lightweight (avoid computationally expensive operations).
- The
Transformmethod should return an error if any transformation function panics.
Notes
- You'll need to define helper functions like
ToUpper,TrimSpace, andLowerCasefor the examples. These are simple string manipulation functions. - Consider using
deferandrecoverto handle panics within theTransformmethod. - Think about how to make the
ResponseTransformerextensible and easy to use with different transformation functions. - The
PanicTransformeris for testing the error handling. It's a function that intentionally panics. You don't need to implement it for the core functionality, but it's useful for demonstrating the error handling. - Focus on clarity and readability in your code. Good error handling is crucial.
- The goal is to create a robust and reusable response transformation pipeline.