Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
9531425
🔨 Add Makefile
akm Sep 21, 2025
ea953e3
⬆️ Bump golang version from 1.13 to 1.24.5
akm Sep 21, 2025
ee4ea4c
🤖 [calendatext] $ go mod tidy
akm Sep 21, 2025
b4689d8
🔧 Add golangci.yml for lint
akm Sep 21, 2025
6d08f2e
🩹 Save files in VS Code
akm Sep 21, 2025
6fa144c
✅ Add t.Parallell() suggested by lint
akm Sep 21, 2025
44f47d9
🩹 Fix lint errors
akm Sep 21, 2025
98f1341
🩹 Extract constant weekdays
akm Sep 21, 2025
c2de2e3
🩹 nolint for magic numbers for date
akm Sep 21, 2025
a5cb18d
🔨 Remove test-% target from Makefile
akm Sep 21, 2025
9141d56
🩹 Use pointer receiver for Period
akm Sep 21, 2025
625395c
🩹 Fix lint errors
akm Sep 21, 2025
eb78ca7
🩹 Use type assertion instead of comment
akm Sep 21, 2025
1ddffdc
🩹 Fix lint errors
akm Sep 21, 2025
948a31a
🔨 Remove unnecessary processing for not-existing directories
akm Sep 21, 2025
b53c6a7
:octocat: Add coverages to .gitignore
akm Sep 21, 2025
44fabda
🔨 Add ci.yml
akm Sep 21, 2025
074a4eb
🤖 [calendatext] $ go mod tidy
akm Sep 21, 2025
0246ca7
⬆️ Update golanci-lint to v2
akm Sep 21, 2025
c98964f
🤖 [calendatext] $ golangci-lint migrate --skip-validation
akm Sep 21, 2025
cce3199
🩹 Fix lint errors
akm Sep 21, 2025
5e11d0c
🔨 lint の実行を test のあとに移動
akm Sep 21, 2025
08ea62b
🔨 lint の実行を test のあとに移動
akm Sep 21, 2025
af23570
🔨 Add slug to codecov/codecov-action@v5
akm Sep 21, 2025
251d78b
🔨 Run make test before make test-with-coverage
akm Sep 21, 2025
e3aba99
:octocat: go.sum をコミット対象外から削除
akm Sep 21, 2025
6794535
🔧 Add go.sum
akm Sep 21, 2025
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
42 changes: 42 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
name: CI

on:
push:
branches:
- "**"

jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
go: ["1.24", "1.25"]
name: Test on Go ${{ matrix.go }}
steps:
# https://github.com/actions/checkout
- uses: actions/checkout@v4
with:
submodules: recursive

# https://github.com/actions/setup-go
- uses: actions/setup-go@v5
with:
go-version: ${{ matrix.go }}

- name: Check golang version/env
run: |
set -x
go version
go env

- run: make build
- run: make test
- run: make lint

- run: make test-with-coverage
- run: make test-coverage-profile
- name: Upload coverage reports to Codecov
uses: codecov/codecov-action@v5
with:
token: ${{ secrets.CODECOV_TOKEN }}
slug: tecowl/calendatext
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1 @@
/go.sum
coverages/
28 changes: 28 additions & 0 deletions .golangci.bck.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
linters:
enable-all: true
disable:
- gosmopolitan
- nlreturn
- nolintlint
- tenv # The linter 'tenv' is deprecated (since v1.64.0) due to: Duplicate feature in another linter. Replaced by usetesting.
- testpackage
- varnamelen
- wrapcheck
- wsl

issues:
# exclude-files:
# - example_test.go
exclude-rules:
- path: _test\.go
linters:
- depguard
- dupword
- errcheck
- forcetypeassert
- funlen
linters-settings:
# cyclop:
# skip-tests: true
# lll:
# line-length: 250
44 changes: 44 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
version: "2"
linters:
default: all
disable:
- gosmopolitan
- noinlineerr
- nlreturn
- nolintlint
- testpackage
- varnamelen
- wrapcheck
- wsl
- wsl_v5
exclusions:
generated: lax
presets:
- comments
- common-false-positives
- legacy
- std-error-handling
rules:
- linters:
- depguard
- dupword
- errcheck
- forcetypeassert
- funlen
path: _test\.go
paths:
- third_party$
- builtin$
- examples$
formatters:
enable:
- gci
- gofmt
- gofumpt
- goimports
exclusions:
generated: lax
paths:
- third_party$
- builtin$
- examples$
21 changes: 21 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
.PHONY: default
default: build lint test

GOBIN=$(shell go env GOBIN)

include ./Makefiles/build.mk
include ./Makefiles/lint.mk
include ./Makefiles/godoc.mk
include ./Makefiles/test.mk
include ./Makefiles/cov-unit.mk
include ./Makefiles/cov-integration.mk
include ./Makefiles/metadata.mk

.PHONY: test
test: test-unit

# COVERAGE_GO_PACKAGES_CSV is used in Makefiles/cov-unit.mk
COVERAGE_GO_PACKAGES_CSV=$(shell find . -type d | grep -v '.git' grep -v Makefiles | grep -v coverages | tr '\n' ',' | sed 's/,$$//')
.PHONY: coverage-go-packages
coverage-go-packages:
@echo $(COVERAGE_GO_PACKAGES_CSV)
3 changes: 3 additions & 0 deletions Makefiles/build.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.PHONY: build
build:
go build ./...
28 changes: 28 additions & 0 deletions Makefiles/cov-integration.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
COVERAGE_INTEGRATED_DIR=$(COVERAGES_DIR)/integrated
$(COVERAGE_INTEGRATED_DIR):
mkdir -p $(COVERAGE_INTEGRATED_DIR)

.PHONY: test-with-coverage
test-with-coverage: test-unit-with-coverage

COVERAGE_DIRS_CSV=$(UNIT_COVERAGE_DIR)

COVERAGE_PROFILE?=$(COVERAGES_DIR)/coverage.txt
$(COVERAGE_PROFILE): $(COVERAGE_INTEGRATED_DIR)
$(MAKE) test-coverage-profile

.PHONY: test-coverage-profile
test-coverage-profile: $(COVERAGE_INTEGRATED_DIR)
go tool covdata merge \
-i $(COVERAGE_DIRS_CSV) \
-o $(COVERAGE_INTEGRATED_DIR)
go tool covdata percent -i=$(COVERAGE_INTEGRATED_DIR) -o $(COVERAGE_PROFILE)

COVERAGE_HTML?=$(COVERAGES_DIR)/coverage.html
$(COVERAGE_HTML): $(COVERAGE_PROFILE)
go tool covdata html -i=$(COVERAGE_PROFILE) -o $(COVERAGE_HTML)

.PHONY: test-coverage
test-coverage: test-coverage-profile
go tool cover -html=$(COVERAGE_PROFILE) -o $(COVERAGE_HTML)
@command -v open && open $(COVERAGE_HTML) || echo "open $(COVERAGE_HTML)"
19 changes: 19 additions & 0 deletions Makefiles/cov-unit.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Directory path from COVERAGES_DIR is used in test in sub directory. So COVERAGE_DIR must be an absolute path.
COVERAGES_DIR=$(CURDIR)/coverages
$(COVERAGES_DIR):
mkdir -p $(COVERAGES_DIR)

UNIT_COVERAGE_DIR=$(COVERAGES_DIR)/unit
$(UNIT_COVERAGE_DIR):
mkdir -p $(UNIT_COVERAGE_DIR)

.PHONY: clean-unit-coverage
clean-unit-coverage:
rm -rf $(UNIT_COVERAGE_DIR)

# COVERAGE_GO_PACKAGES_CSV is defined in Makefile

# See https://app.codecov.io/github/akm/go-requestid/new
.PHONY: test-unit-with-coverage
test-unit-with-coverage: clean-unit-coverage $(UNIT_COVERAGE_DIR)
go test -cover -coverpkg=$(COVERAGE_GO_PACKAGES_CSV) ./... -args -test.gocoverdir="$(UNIT_COVERAGE_DIR)"
12 changes: 12 additions & 0 deletions Makefiles/godoc.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
GODOC_CLI_VERSION=latest
GODOC_CLI_MODULE=golang.org/x/tools/cmd/godoc
GODOC_CLI=$(GOBIN)/godoc
$(GODOC_CLI):
$(MAKE) godoc-cli-install
godoc-cli-install:
go install $(GODOC_CLI_MODULE)@$(GODOC_CLI_VERSION)

.PHONY: godoc
godoc: $(GODOC_CLI)
@echo "Open http://localhost:6060/pkg/github.com/tecowl/querybm"
godoc -http=:6060
15 changes: 15 additions & 0 deletions Makefiles/lint.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
GOLANGCI_LINT_CLI_VERSION?=latest
GOLANGCI_LINT_CLI_MODULE=github.com/golangci/golangci-lint/v2/cmd/golangci-lint
GOLANGCI_LINT_CLI=$(GOBIN)/golangci-lint
$(GOLANGCI_LINT_CLI):
$(MAKE) golangci-lint-cli-install
golangci-lint-cli-install:
go install $(GOLANGCI_LINT_CLI_MODULE)@$(GOLANGCI_LINT_CLI_VERSION)

.PHONY: lint
lint: $(GOLANGCI_LINT_CLI)
golangci-lint run

.PHONY: linters-enabled
linters-enabled: $(GOLANGCI_LINT_CLI)
@golangci-lint linters | awk '/^Enabled by your configuration linters:$$/{flag=1;next}/^Disabled by your configuration linters:$$/{flag=0}flag{print}'
7 changes: 7 additions & 0 deletions Makefiles/metadata.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
METADATA_YAML=.project.yaml
$(METADATA_YAML): metadata-gen

METADATA_LINTERS=$(strip $(shell $(MAKE) linters-enabled --no-print-directory 2>/dev/null | grep . | wc -l))
.PHONY: metadata-gen
metadata-gen:
@echo "linters: $(METADATA_LINTERS)" > $(METADATA_YAML)
5 changes: 5 additions & 0 deletions Makefiles/test.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
GO_TEST_OPTIONS?=

.PHONY: test-unit
test-unit:
go test $(GO_TEST_OPTIONS) ./...
2 changes: 1 addition & 1 deletion calendar.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ type Calendar struct {
}

func NewCalendar(start, end Date, baseEnabled bool) *Calendar {
return &Calendar{Period: NewPeriod(start, end), BaseEnabled: baseEnabled}
return &Calendar{Period: NewPeriod(start, end), BaseEnabled: baseEnabled, Patterns: Patterns{}}
}

func (c *Calendar) Dates() Dates {
Expand Down
14 changes: 8 additions & 6 deletions calendar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestCalendarDays(t *testing.T) {
t.Parallel()

// ----- 2020-08 ------
// S M T W T F S
Expand All @@ -18,6 +20,7 @@ func TestCalendarDays(t *testing.T) {
// 30 31

t.Run("single date", func(t *testing.T) {
t.Parallel()
c := &Calendar{
Period: NewPeriod(*NewDate(2020, 8, 1), *NewDate(2020, 8, 31)),
BaseEnabled: false,
Expand Down Expand Up @@ -55,6 +58,8 @@ func TestCalendarDays(t *testing.T) {
}

func TestCalendarParse(t *testing.T) {
t.Parallel()

texts := map[string]string{
"full-date": `
+ 平日 : 通常営業日
Expand Down Expand Up @@ -84,15 +89,12 @@ func TestCalendarParse(t *testing.T) {

for name, text := range texts {
t.Run(name, func(t *testing.T) {
t.Parallel()
c := NewCalendar(*NewDate(2020, 8, 1), *NewDate(2020, 8, 31), false)
err := c.ParseText(text)
if !assert.NoError(t, err) {
return
}
require.NoError(t, err)
require.Len(t, c.Patterns, 4)

if !assert.Equal(t, 4, len(c.Patterns)) {
return
}
if assert.IsType(t, (*Date)(nil), c.Patterns[0].DateMatcher) {
i := c.Patterns[0]
m := i.DateMatcher.(*Date)
Expand Down
26 changes: 15 additions & 11 deletions contextual_date_parser.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package calendatext

import (
"errors"
"fmt"
"regexp"
"strconv"
"strings"
"time"

"github.com/pkg/errors"
)

type ContextualDateParser struct {
Expand All @@ -18,48 +18,49 @@ func NewContextualDateParser(delimiterPattern string, d *Date) *ContextualDatePa
if d == nil {
d = Today()
}

return &ContextualDateParser{
delimiter: regexp.MustCompile("[" + delimiterPattern + "]"),
current: d,
}
}

func (cp *ContextualDateParser) Parse(s string) (*Date, error) {
parts := cp.delimiter.Split(strings.TrimSpace(s), 3)
func (cp *ContextualDateParser) Parse(s string) (*Date, error) { // nolint:cyclop,funlen
parts := cp.delimiter.Split(strings.TrimSpace(s), 3) // nolint:mnd
var y, d int
var m time.Month
var err error
switch len(parts) {
case 1:
case 1: // nolint:mnd
curr := cp.current
d, err = strconv.Atoi(parts[0])
if err != nil {
return nil, err
return nil, err // nolint:wrapcheck
}
if d < curr.Day() {
m = curr.Month() + 1
} else {
m = curr.Month()
}
y = curr.Year()
case 2:
case 2: // nolint:mnd
curr := cp.current
v, err := strconv.Atoi(parts[0])
if err != nil {
return nil, err
return nil, err // nolint:wrapcheck
}
m = time.Month(v)
d, err = strconv.Atoi(parts[1])
if err != nil {
return nil, err
return nil, err // nolint:wrapcheck
}
tmpD := NewDate(curr.Year(), m, d)
if curr.After(tmpD) {
y = curr.Year() + 1
} else {
y = curr.Year()
}
case 3:
case 3: // nolint:mnd
y, err = strconv.Atoi(parts[0])
if err != nil {
return nil, err
Expand All @@ -74,10 +75,13 @@ func (cp *ContextualDateParser) Parse(s string) (*Date, error) {
return nil, err
}
default:
return nil, errors.Errorf("Something wrong to parse %v (len: %d) as Date", parts, len(parts))
return nil, fmt.Errorf("%w: %v (len: %d)", ErrDateParse, parts, len(parts))
}

r := NewDate(y, m, d)
cp.current = r

return r, nil
}

var ErrDateParse = errors.New("something wrong to parse date")
Loading