Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
name: ci.yml

on:
pull_request:
push:
branches:
- main
- master

permissions:
contents: read

env:
GO111MODULE: on

jobs:
golangci:
name: lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v5

- uses: actions/setup-go@v6
with:
go-version: stable

- name: golangci-lint
uses: golangci/golangci-lint-action@v8
with:
version: v2.1

build:
name: Build and Test
runs-on: ubuntu-latest
strategy:
matrix:
go-version: [ '1.23', '1.x' ]
fail-fast: false
steps:
- uses: actions/checkout@v5

- uses: actions/setup-go@v6
with:
go-version: ${{ matrix.go-version }}

- name: go vet
run: go vet -x ./...

- name: Check Formatting
run: test -z "$(gofmt -s -l -w . | tee /dev/stderr)"

- name: Run Tests
run: go test -v ./...

- name: Coverage (profile.cov)
run: go test -covermode=count -coverprofile=profile.cov

- name: Coveralls
uses: coverallsapp/github-action@v2
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
file: profile.cov
format: golang
21 changes: 0 additions & 21 deletions .travis.yml

This file was deleted.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# go-linq [![GoDoc](https://godoc.org/github.com/ahmetb/go-linq?status.svg)](https://godoc.org/github.com/ahmetb/go-linq) [![Build Status](https://travis-ci.org/ahmetb/go-linq.svg?branch=master)](https://travis-ci.org/ahmetb/go-linq) [![Coverage Status](https://coveralls.io/repos/github/ahmetb/go-linq/badge.svg?branch=master)](https://coveralls.io/github/ahmetb/go-linq?branch=master) [![Go Report Card](https://goreportcard.com/badge/github.com/ahmetb/go-linq)](https://goreportcard.com/report/github.com/ahmetb/go-linq)
# go-linq [![GoDoc](https://godoc.org/github.com/ahmetb/go-linq?status.svg)](https://godoc.org/github.com/ahmetb/go-linq) [![Build Status](https://github.com/ahmetb/go-linq/actions/workflows/ci.yml/badge.svg?branch=master)](https://github.com/ahmetb/go-linq/actions/workflows/ci.yml) [![Coverage Status](https://coveralls.io/repos/github/ahmetb/go-linq/badge.svg?branch=master)](https://coveralls.io/github/ahmetb/go-linq?branch=master) [![Go Report Card](https://goreportcard.com/badge/github.com/ahmetb/go-linq)](https://goreportcard.com/report/github.com/ahmetb/go-linq)

A powerful language integrated query (LINQ) library for Go.

Expand Down
19 changes: 10 additions & 9 deletions compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,18 @@ type comparer func(interface{}, interface{}) int
// elements in order to work with linq.
//
// Example:
// func (f foo) CompareTo(c Comparable) int {
// a, b := f.f1, c.(foo).f1
//
// if a < b {
// return -1
// } else if a > b {
// return 1
// }
// func (f foo) CompareTo(c Comparable) int {
// a, b := f.f1, c.(foo).f1
//
// return 0
// }
// if a < b {
// return -1
// } else if a > b {
// return 1
// }
//
// return 0
// }
type Comparable interface {
CompareTo(Comparable) int
}
Expand Down
39 changes: 20 additions & 19 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,8 +326,8 @@ func ExampleQuery_Append() {
// 5
}

//The following code example demonstrates how to use Average
//to calculate the average of a slice of values.
// The following code example demonstrates how to use Average
// to calculate the average of a slice of values.
func ExampleQuery_Average() {
grades := []int{78, 92, 100, 37, 81}
average := From(grades).Average()
Expand Down Expand Up @@ -360,8 +360,8 @@ func ExampleQuery_Contains() {
// Does the slice contains 5? true
}

//The following code example demonstrates how to use CountWith
//to count the even numbers in an array.
// The following code example demonstrates how to use CountWith
// to count the even numbers in an array.
func ExampleQuery_CountWith() {
slice := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

Expand Down Expand Up @@ -445,8 +445,8 @@ func ExampleQuery_DefaultIfEmpty() {

}

//The following code example demonstrates how to use Distinct
//to return distinct elements from a slice of integers.
// The following code example demonstrates how to use Distinct
// to return distinct elements from a slice of integers.
func ExampleQuery_Distinct() {
ages := []int{21, 46, 46, 55, 17, 21, 55, 55}

Expand Down Expand Up @@ -567,7 +567,7 @@ func ExampleQuery_First() {

}

//The following code example demonstrates how to use FirstWith
// The following code example demonstrates how to use FirstWith
// to return the first element of an array that satisfies a condition.
func ExampleQuery_FirstWith() {
numbers := []int{9, 34, 65, 92, 87, 435, 3, 54, 83, 23, 87, 435, 67, 12, 19}
Expand All @@ -583,8 +583,8 @@ func ExampleQuery_FirstWith() {

}

//The following code example demonstrates how to use Intersect
//to return the elements that appear in each of two slices of integers.
// The following code example demonstrates how to use Intersect
// to return the elements that appear in each of two slices of integers.
func ExampleQuery_Intersect() {
id1 := []int{44, 26, 92, 30, 71, 38}
id2 := []int{39, 59, 83, 47, 26, 4, 30}
Expand All @@ -603,8 +603,8 @@ func ExampleQuery_Intersect() {

}

//The following code example demonstrates how to use IntersectBy
//to return the elements that appear in each of two slices of products with same Code.
// The following code example demonstrates how to use IntersectBy
// to return the elements that appear in each of two slices of products with same Code.
func ExampleQuery_IntersectBy() {
type Product struct {
Name string
Expand Down Expand Up @@ -716,10 +716,10 @@ func ExampleQuery_OrderByDescending() {
// The following code example demonstrates how to use ThenByDescending to perform
// a secondary ordering of the elements in a slice in descending order.
func ExampleOrderedQuery_ThenByDescending() {
fruits := []string{"apPLe", "baNanA", "apple", "APple", "orange", "BAnana", "ORANGE", "apPLE"}
fruits := []string{"apPLe", "baNanA", "apple", "APple", "orange", "BAnana", "ORANGE"}

// Sort the strings first ascending by their length and
// then descending using a custom case insensitive comparer.
// then descending using a custom case-insensitive comparer.
var query []string
From(fruits).
OrderBy(
Expand All @@ -728,21 +728,22 @@ func ExampleOrderedQuery_ThenByDescending() {
ThenByDescending(
func(fruit interface{}) interface{} { return fruit.(string)[0] },
).
ThenByDescending(
func(fruit interface{}) interface{} { return fruit.(string)[3] },
).
ToSlice(&query)

for _, fruit := range query {
fmt.Println(fruit)
}
// Output:
// apPLe
// apPLE
// apple
// apPLe
// APple
// orange
// baNanA
// ORANGE
// BAnana

}

// The following code example demonstrates how to use Concat
Expand Down Expand Up @@ -1281,7 +1282,7 @@ func ExampleQuery_SumUInts() {
}

// The following code example demonstrates how to use Take
// to return elements from the start of a slice.
// to return elements from the start of a slice.
func ExampleQuery_Take() {
grades := []int{59, 82, 70, 56, 92, 98, 85}

Expand Down Expand Up @@ -1910,7 +1911,7 @@ func ExampleQuery_GroupByT() {
}

// The following code example demonstrates how to use GroupJoinT
// to perform a grouped join on two slices.
// to perform a grouped join on two slices.
func ExampleQuery_GroupJoinT() {

type Person struct {
Expand Down Expand Up @@ -2405,7 +2406,7 @@ func ExampleQuery_SelectManyByIndexedT() {

}

//The following code example demonstrates how to use SingleWithT
// The following code example demonstrates how to use SingleWithT
// to select the only element of a slice that satisfies a condition.
func ExampleQuery_SingleWithT() {
fruits := []string{"apple", "banana", "mango", "orange", "passionfruit", "grape"}
Expand Down
4 changes: 2 additions & 2 deletions genericfunc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func TestNewGenericFunc(t *testing.T) {

for _, test := range tests {
_, err := newGenericFunc(test.methodName, test.paramName, test.function, test.validationFunc)
if !(err == test.exception || err.Error() == test.exception.Error()) {
if err != test.exception && err.Error() != test.exception.Error() {
t.Errorf("Validate expect error: %s, actual: %s", test.exception, err)
}
}
Expand Down Expand Up @@ -112,7 +112,7 @@ func TestCall(t *testing.T) {
func() {
defer func() {
r := recover()
if !(r == test.exception || r == test.exception.Error()) {
if r != test.exception && r != test.exception.Error() {
t.Errorf("expect error: nil, actual: %s", r)
}
}()
Expand Down
4 changes: 4 additions & 0 deletions orderby.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ func (q Query) OrderByDescending(selector func(interface{}) interface{}) Ordered

// OrderByDescendingT is the typed version of OrderByDescending.
// - selectorFn is of type "func(TSource) TKey"
//
// NOTE: OrderByDescending has better performance than OrderByDescendingT.
func (q Query) OrderByDescendingT(selectorFn interface{}) OrderedQuery {
selectorGenericFunc, err := newGenericFunc(
Expand Down Expand Up @@ -138,6 +139,7 @@ func (oq OrderedQuery) ThenBy(

// ThenByT is the typed version of ThenBy.
// - selectorFn is of type "func(TSource) TKey"
//
// NOTE: ThenBy has better performance than ThenByT.
func (oq OrderedQuery) ThenByT(selectorFn interface{}) OrderedQuery {
selectorGenericFunc, err := newGenericFunc(
Expand Down Expand Up @@ -184,6 +186,7 @@ func (oq OrderedQuery) ThenByDescending(selector func(interface{}) interface{})

// ThenByDescendingT is the typed version of ThenByDescending.
// - selectorFn is of type "func(TSource) TKey"
//
// NOTE: ThenByDescending has better performance than ThenByDescendingT.
func (oq OrderedQuery) ThenByDescendingT(selectorFn interface{}) OrderedQuery {
selectorFunc, ok := selectorFn.(func(interface{}) interface{})
Expand Down Expand Up @@ -230,6 +233,7 @@ func (q Query) Sort(less func(i, j interface{}) bool) Query {

// SortT is the typed version of Sort.
// - lessFn is of type "func(TSource,TSource) bool"
//
// NOTE: Sort has better performance than SortT.
func (q Query) SortT(lessFn interface{}) Query {
lessGenericFunc, err := newGenericFunc(
Expand Down
4 changes: 2 additions & 2 deletions result_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ func TestForEach(t *testing.T) {

func TestForEachT_PanicWhenActionFnIsInvalid(t *testing.T) {
mustPanicWithError(t, "ForEachT: parameter [actionFn] has a invalid function signature. Expected: 'func(T)', actual: 'func(int,int)'", func() {
From([]int{1, 1, 1, 2, 1, 2, 3, 4, 2}).ForEachT(func(item, idx int) { item = item + 2 })
From([]int{1, 1, 1, 2, 1, 2, 3, 4, 2}).ForEachT(func(item, idx int) {})
})
}

Expand All @@ -247,7 +247,7 @@ func TestForEachIndexed(t *testing.T) {

func TestForEachIndexedT_PanicWhenActionFnIsInvalid(t *testing.T) {
mustPanicWithError(t, "ForEachIndexedT: parameter [actionFn] has a invalid function signature. Expected: 'func(int,T)', actual: 'func(int)'", func() {
From([]int{1, 1, 1, 2, 1, 2, 3, 4, 2}).ForEachIndexedT(func(item int) { item = item + 2 })
From([]int{1, 1, 1, 2, 1, 2, 3, 4, 2}).ForEachIndexedT(func(item int) {})
})
}

Expand Down
2 changes: 2 additions & 0 deletions select.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func (q Query) Select(selector func(interface{}) interface{}) Query {

// SelectT is the typed version of Select.
// - selectorFn is of type "func(TSource)TResult"
//
// NOTE: Select has better performance than SelectT.
func (q Query) SelectT(selectorFn interface{}) Query {

Expand Down Expand Up @@ -91,6 +92,7 @@ func (q Query) SelectIndexed(selector func(int, interface{}) interface{}) Query

// SelectIndexedT is the typed version of SelectIndexed.
// - selectorFn is of type "func(int,TSource)TResult"
//
// NOTE: SelectIndexed has better performance than SelectIndexedT.
func (q Query) SelectIndexedT(selectorFn interface{}) Query {
selectGenericFunc, err := newGenericFunc(
Expand Down
2 changes: 1 addition & 1 deletion setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func validateQuery(q Query, output []interface{}) bool {

_, ok := next()
_, ok2 := next()
return !(ok || ok2)
return !ok && !ok2
}

func mustPanicWithError(t *testing.T, expectedErr string, f func()) {
Expand Down
Loading