Skip to content

Commit

Permalink
✨ feat: support filter pointer string value type
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Jan 4, 2024
1 parent 20c7e85 commit 9772411
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 26 deletions.
72 changes: 72 additions & 0 deletions .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# For most projects, this workflow file will not need changing; you simply need
# to commit it to your repository.
#
# You may wish to alter this file to override the set of languages analyzed,
# or to provide custom queries or build logic.
#
# ******** NOTE ********
# We have attempted to detect the languages in your repository. Please check
# the `language` matrix defined below to confirm you have the correct set of
# supported CodeQL languages.
#
name: "CodeQL"

on:
push:
branches: [ "master" ]
pull_request:
# The branches below must be a subset of the branches above
branches: [ "master" ]
schedule:
- cron: '28 10 * * 0'

jobs:
analyze:
name: Analyze
runs-on: ubuntu-latest
permissions:
actions: read
contents: read
security-events: write

strategy:
fail-fast: false
matrix:
language: [ 'go' ]
# CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python', 'ruby' ]
# Learn more about CodeQL language support at https://aka.ms/codeql-docs/language-support

steps:
- name: Checkout repository
uses: actions/checkout@v4

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@v2
with:
languages: ${{ matrix.language }}
# If you wish to specify custom queries, you can do so here or in a config file.
# By default, queries listed here will override any specified in a config file.
# Prefix the list here with "+" to use these queries and those in the config file.

# Details on CodeQL's query packs refer to : https://docs.github.com/en/code-security/code-scanning/automatically-scanning-your-code-for-vulnerabilities-and-errors/configuring-code-scanning#using-queries-in-ql-packs
# queries: security-extended,security-and-quality


# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
# If this step fails, then you should remove it and run the build manually (see below)
- name: Autobuild
uses: github/codeql-action/autobuild@v2

# ℹ️ Command-line programs to run using the OS shell.
# 📚 See https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsrun

# If the Autobuild fails above, remove it and uncomment the following three lines.
# modify them (or add more) to build your code if your project, please refer to the EXAMPLE below for guidance.

# - run: |
# echo "Run, Build Application using script"
# ./location_of_script_within_repo/buildscript.sh

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v2
40 changes: 24 additions & 16 deletions converters.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,25 +35,12 @@ func Int(in any) (int, error) { return ToInt(in) }

// MustInt convert string to int
func MustInt(in any) int {
val, _ := ToInt(in)
return val
return mathutil.MustInt(in)
}

// ToInt convert string to int
func ToInt(in any) (int, error) { return mathutil.ToInt(in) }

// Uint convert string to uint
func Uint(in any) (uint64, error) { return ToUint(in) }

// MustUint convert string to uint
func MustUint(in any) uint64 {
val, _ := ToUint(in)
return val
}

// ToUint convert string to uint
func ToUint(in any) (uint64, error) { return mathutil.ToUint(in) }

// Int64 convert value to int64
func Int64(in any) (int64, error) { return ToInt64(in) }

Expand All @@ -62,8 +49,29 @@ func ToInt64(val any) (int64, error) { return mathutil.ToInt64(val) }

// MustInt64 convert value to int64
func MustInt64(in any) int64 {
i64, _ := ToInt64(in)
return i64
return mathutil.MustInt64(in)
}

// Uint convert string to uint
func Uint(in any) (uint, error) { return ToUint(in) }

// ToUint convert string to uint
func ToUint(in any) (uint, error) { return mathutil.ToUint(in) }

// MustUint convert string to uint
func MustUint(in any) uint {
return mathutil.MustUint(in)
}

// Uint64 convert string to uint64
func Uint64(in any) (uint64, error) { return ToUint64(in) }

// ToUint64 convert string to uint64
func ToUint64(in any) (uint64, error) { return mathutil.ToUint64(in) }

// MustUint64 convert string to uint64
func MustUint64(in any) uint64 {
return mathutil.MustUint64(in)
}

// Float convert string to float
Expand Down
16 changes: 14 additions & 2 deletions helper.go → filter.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
// Package filter provide data filter, sanitize, convert process.
//
// Source code and other details for the project are available at GitHub:
//
// https://github.com/gookit/filter
//
// More usage please see README and tests
package filter

import (
Expand Down Expand Up @@ -43,8 +50,13 @@ func Apply(name string, val any, args []string) (any, error) {
return val, err
}

str, isString := val.(string)
if !isString {
// check val is string
var str string

// up: support filter pointer string value
if poStr, ok := val.(*string); ok {
str = *poStr
} else if str, ok = val.(string); ok {
return nil, fmt.Errorf("filter: '%s' only use for string type value", name)
}

Expand Down
21 changes: 21 additions & 0 deletions filter_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package filter_test

import (
"testing"

"github.com/gookit/filter"
"github.com/gookit/goutil/testutil/assert"
)

func TestApply(t *testing.T) {
str := " abc "
ret, err := filter.Apply("trim", str, nil)
assert.NoErr(t, err)
assert.Equal(t, "abc", ret)

// test pointer string
ps := &str
ret, err = filter.Apply("trim", ps, nil)
assert.NoErr(t, err)
assert.Equal(t, "abc", ret)
}
8 changes: 0 additions & 8 deletions filters.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
// Package filter provide data filter, sanitize, convert
//
// Source code and other details for the project are available at GitHub:
//
// https://github.com/gookit/filter
//
// More usage please see README and test
package filter

import (
Expand Down Expand Up @@ -87,7 +80,6 @@ func Trim(s string, cutSet ...string) string {
if len(cutSet) > 0 && cutSet[0] != "" {
return strings.Trim(s, cutSet[0])
}

return strings.TrimSpace(s)
}

Expand Down

0 comments on commit 9772411

Please sign in to comment.