Skip to content

Commit

Permalink
Sarthak | Adds error handling for mime related functions
Browse files Browse the repository at this point in the history
  • Loading branch information
SarthakMakhija committed Sep 12, 2022
1 parent b4ea353 commit b000f53
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 15 deletions.
10 changes: 5 additions & 5 deletions parser/context/Functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,27 +306,27 @@ var functionDefinitions = map[string]*FunctionDefinition{
},
FunctionNameIsFileTypeText: {
aliases: []string{"istext", "istxt"},
description: "Returns true if the mime type of the file is text/plain, false otherwise.",
description: "Returns true if the mime type of a file is text/plain, false otherwise. \nFor example, the common use of this function is with mime attribute, istext(mime).",
block: IsFileTypeTextFunctionBlock{},
},
FunctionNameIsFileTypeImage: {
aliases: []string{"isimage", "isimg"},
description: "Returns true if the mime type of the file is an image, false otherwise.",
description: "Returns true if the mime type of a file is an image, false otherwise. \nFor example, the common use of this function is with mime attribute, isimage(mime).",
block: IsFileTypeImageFunctionBlock{},
},
FunctionNameIsFileTypeAudio: {
aliases: []string{"isaudio"},
description: "Returns true if the mime type of the file is an audio, false otherwise.",
description: "Returns true if the mime type of a file is an audio, false otherwise. \nFor example, the common use of this function is with mime attribute, isaudio(mime).",
block: IsFileTypeAudioFunctionBlock{},
},
FunctionNameIsFileTypeVideo: {
aliases: []string{"isvideo"},
description: "Returns true if the mime type of the file is video, false otherwise.",
description: "Returns true if the mime type of a file is video, false otherwise. \nFor example, the common use of this function is with mime attribute, isvideo(mime).",
block: IsFileTypeVideoFunctionBlock{},
},
FunctionNameIsFileTypePdf: {
aliases: []string{"ispdf"},
description: "Returns true if the mime type of the file is pdf, false otherwise.",
description: "Returns true if the mime type of a file is pdf, false otherwise. \nFor example, the common use of this function is with mime attribute, ispdf(mime).",
block: IsFileTypePdfFunctionBlock{},
},
FunctionNameCount: {
Expand Down
32 changes: 22 additions & 10 deletions parser/context/ScalarFunctionBlock.go
Original file line number Diff line number Diff line change
Expand Up @@ -433,32 +433,44 @@ func (r ReplaceAllFunctionBlock) run(args ...Value) (Value, error) {
}

func (i IsFileTypeTextFunctionBlock) run(args ...Value) (Value, error) {
return booleanValueUsing(mimeTypeMatches("text/plain", args...)), nil
if err := ensureNParametersOrError(args, FunctionNameIsFileTypeText, 1); err != nil {
return EmptyValue, err
}
return booleanValueUsing(mimeTypeMatches("text/plain", args[0])), nil
}

func (i IsFileTypeImageFunctionBlock) run(args ...Value) (Value, error) {
return booleanValueUsing(mimeTypeMatches("image/", args...)), nil
if err := ensureNParametersOrError(args, FunctionNameIsFileTypeImage, 1); err != nil {
return EmptyValue, err
}
return booleanValueUsing(mimeTypeMatches("image/", args[0])), nil
}

func (i IsFileTypeAudioFunctionBlock) run(args ...Value) (Value, error) {
return booleanValueUsing(mimeTypeMatches("audio/", args...)), nil
if err := ensureNParametersOrError(args, FunctionNameIsFileTypeAudio, 1); err != nil {
return EmptyValue, err
}
return booleanValueUsing(mimeTypeMatches("audio/", args[0])), nil
}

func (i IsFileTypeVideoFunctionBlock) run(args ...Value) (Value, error) {
return booleanValueUsing(mimeTypeMatches("video/", args...)), nil
if err := ensureNParametersOrError(args, FunctionNameIsFileTypeVideo, 1); err != nil {
return EmptyValue, err
}
return booleanValueUsing(mimeTypeMatches("video/", args[0])), nil
}

func (i IsFileTypePdfFunctionBlock) run(args ...Value) (Value, error) {
if err := ensureNParametersOrError(args, FunctionNameIsFileTypePdf, 1); err != nil {
return EmptyValue, err
}
return booleanValueUsing(
mimeTypeMatches("application/pdf", args...) || mimeTypeMatches("application/x-pdf", args...),
mimeTypeMatches("application/pdf", args[0]) || mimeTypeMatches("application/x-pdf", args[0]),
), nil
}

func mimeTypeMatches(expectedMimeType string, args ...Value) bool {
mimeType := ""
if len(args) >= 1 {
mimeType = args[0].GetAsString()
}
func mimeTypeMatches(expectedMimeType string, arg Value) bool {
mimeType := arg.GetAsString()
return strings.Contains(mimeType, expectedMimeType)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -609,6 +609,28 @@ func TestResultsWithProjectionsWithMimeType3(t *testing.T) {
assertMatch(t, expected, queryResults)
}

func TestResultsWithProjectionsWithMimeType4(t *testing.T) {
newContext := context.NewContext(context.NewFunctions(), context.NewAttributes())
aParser, err := parser.NewParser("select isdir, lower(name), mime from ../resources/TestResultsWithProjections/ where eq(istext(mime), true) order by 1 desc, 2 asc", newContext)
if err != nil {
t.Fatalf("error is %v", err)
}
selectQuery, err := aParser.Parse()
if err != nil {
t.Fatalf("error is %v", err)
}
queryResults, _ := NewSelectQueryExecutor(selectQuery, newContext, NewDefaultOptions()).Execute()
expected := [][]context.Value{
{context.BooleanValue(false), context.StringValue("empty.log"), context.StringValue("text/plain")},
{context.BooleanValue(false), context.StringValue("testresultswithprojections_a.log"), context.StringValue("text/plain; charset=utf-8")},
{context.BooleanValue(false), context.StringValue("testresultswithprojections_a.txt"), context.StringValue("text/plain; charset=utf-8")},
{context.BooleanValue(false), context.StringValue("testresultswithprojections_b.log"), context.StringValue("text/plain; charset=utf-8")},
{context.BooleanValue(false), context.StringValue("testresultswithprojections_c.txt"), context.StringValue("text/plain; charset=utf-8")},
{context.BooleanValue(false), context.StringValue("testresultswithprojections_d.txt"), context.StringValue("text/plain; charset=utf-8")},
}
assertMatch(t, expected, queryResults)
}

func TestResultsWithProjectionsWithoutProperParametersToAFunction(t *testing.T) {
newContext := context.NewContext(context.NewFunctions(), context.NewAttributes())
aParser, err := parser.NewParser("select name, lower() from ../resources/TestResultsWithProjections/single", newContext)
Expand Down

0 comments on commit b000f53

Please sign in to comment.