refactor: extract raw pattern retrieval into dedicated GetRaw method`

- Add `GetRaw` method to `PatternsEntity` for unprocessed pattern retrieval
- Replace inline raw pattern loading logic in server handler with `GetRaw`
- Remove manual `Pattern` struct construction from `PatternsHandler.Get`
- Simplify server handler by delegating storage access to database layer
- Add test coverage for `GetRaw` with custom patterns directory
This commit is contained in:
Kayvan Sylvan 2026-03-05 12:19:06 -08:00
parent fa2e1ee90a
commit 1f7e1d3d20
3 changed files with 11 additions and 9 deletions

View file

@ -50,6 +50,11 @@ func (o *PatternsEntity) GetWithoutVariables(source, input string) (pattern *Pat
return
}
// GetRaw returns a pattern from storage without applying variable processing.
func (o *PatternsEntity) GetRaw(name string) (*Pattern, error) {
return o.getFromDB(name)
}
func (o *PatternsEntity) loadPattern(source string) (pattern *Pattern, err error) {
// Determine if this is a file path
isFilePath := strings.HasPrefix(source, "\\") ||

View file

@ -256,6 +256,11 @@ func TestPatternsEntity_CustomPatterns(t *testing.T) {
require.NoError(t, err)
assert.Equal(t, "Main pattern content", pattern.Pattern)
// Test GetRaw also respects custom patterns directory
rawPattern, err := entity.GetRaw("shared-pattern")
require.NoError(t, err)
assert.Equal(t, "Custom shared pattern", rawPattern.Pattern)
// Test that custom pattern is accessible
pattern, err = entity.getFromDB("custom-pattern")
require.NoError(t, err)

View file

@ -46,19 +46,11 @@ func NewPatternsHandler(r *gin.Engine, patterns *fsdb.PatternsEntity) (ret *Patt
func (h *PatternsHandler) Get(c *gin.Context) {
name := c.Param("name")
// Get the raw pattern content without any variable processing
content, err := h.patterns.Load(name + "/" + h.patterns.SystemPatternFile)
pattern, err := h.patterns.GetRaw(name)
if err != nil {
c.JSON(http.StatusInternalServerError, err.Error())
return
}
// Return raw pattern in the same format as the processed patterns
pattern := &fsdb.Pattern{
Name: name,
Description: "",
Pattern: string(content),
}
c.JSON(http.StatusOK, pattern)
}