Skip to content

Commit 9ff7905

Browse files
authored
add examples,documentation and code-coverage gh-action. (#2)
* add examples,documentation and code-coverage gh-action. * fix gofumpt err. * checking code coverage. * fix tests.yml * fix quotes * fix name err. * reorder artifact dl and add license. * revert code-cov branch to master.
1 parent d1ad19f commit 9ff7905

15 files changed

+549
-58
lines changed

.github/workflows/tests.yml

+27-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Testing
1+
name: unit-tests
22

33
on: [ push ]
44

@@ -20,12 +20,36 @@ jobs:
2020
run: |
2121
echo "Don't cancel old workflow"
2222
tests:
23-
name: Unit testing
23+
name: unit-tests
2424
runs-on: ubuntu-latest
2525
steps:
2626
- name: Install Go
2727
uses: actions/setup-go@v2
2828
- name: Checkout code
2929
uses: actions/checkout@v2
3030
- name: Unit test
31-
run: go test -v ./...
31+
run: go test -v ./... -coverprofile=coverage.txt
32+
- name: Archive code coverage results
33+
if: contains(github.ref, 'master')
34+
uses: actions/upload-artifact@v2
35+
with:
36+
name: code-coverage-report
37+
path: ./coverage.txt
38+
code-coverage:
39+
name: Upload code coverage
40+
runs-on: ubuntu-latest
41+
needs: tests
42+
if: contains(github.ref, 'master')
43+
steps:
44+
- uses: actions/checkout@master
45+
- name: Download coverage report
46+
uses: actions/download-artifact@v2
47+
with:
48+
name: code-coverage-report
49+
- uses: codecov/codecov-action@v1
50+
with:
51+
file: ./coverage.txt
52+
flags: unittests
53+
name: go-financial
54+
fail_ci_if_error: true
55+
verbose: true

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2020 Razorpay
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@ This package is a go native port of the numpy-financial package with some additi
44
functions.
55

66
The functions in this package are a scalar version of their vectorised counterparts in
7-
the numpy-financial library.
7+
the [numpy-financial](https://github.com/numpy/numpy-financial) library.
8+
9+
[![unit-tests status](https://github.com/razorpay/go-financial/workflows/unit-tests/badge.svg?branch=master "unit-tests")]("https://github.com/razorpay/go-financial/workflows/unit-tests/badge.svg?branch=master")
10+
[![Go Report Card](https://goreportcard.com/badge/github.com/razorpay/go-financial)](https://goreportcard.com/report/github.com/razorpay/go-financial)
11+
[![codecov](https://codecov.io/gh/razorpay/go-financial/branch/master/graph/badge.svg)](https://codecov.io/gh/razorpay/go-financial)
12+
[![GoDoc](https://godoc.org/github.com/razorpay/go-financial?status.svg)](https://godoc.org/github.com/razorpay/go-financial) [![Release](https://img.shields.io/github/release/razorpay/go-financial.svg?style=flat-square)](https://github.com/razorpay/go-financial/releases)
813

914
Currently, only some functions are ported,
1015
which are as follows:

amortization.go

+15-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package calculator
1+
package gofinancial
22

33
import (
44
"encoding/json"
@@ -13,14 +13,16 @@ import (
1313
"github.com/razorpay/go-financial/enums/interesttype"
1414
)
1515

16-
type amortization struct {
16+
// Amortization struct holds the configuration and financial details.
17+
type Amortization struct {
1718
Config *Config
1819
Financial Financial
1920
}
2021

21-
func NewAmortization(c *Config) (*amortization, error) {
22-
a := amortization{Config: c}
23-
if err := a.Config.SetPeriodsAndDates(); err != nil {
22+
// NewAmortization return a new amortisation object with config and financial fields initialised.
23+
func NewAmortization(c *Config) (*Amortization, error) {
24+
a := Amortization{Config: c}
25+
if err := a.Config.setPeriodsAndDates(); err != nil {
2426
return nil, err
2527
}
2628

@@ -33,6 +35,7 @@ func NewAmortization(c *Config) (*amortization, error) {
3335
return &a, nil
3436
}
3537

38+
// Row represents a single row in an amortization schedule.
3639
type Row struct {
3740
Period int64
3841
StartDate time.Time
@@ -42,7 +45,8 @@ type Row struct {
4245
Principal float64
4346
}
4447

45-
func (a amortization) GenerateTable() ([]Row, error) {
48+
// GenerateTable constructs the amortization table based on the configuration.
49+
func (a Amortization) GenerateTable() ([]Row, error) {
4650
var result []Row
4751
for i := int64(1); i <= a.Config.periods; i++ {
4852
var row Row
@@ -74,6 +78,8 @@ func (a amortization) GenerateTable() ([]Row, error) {
7478
return result, nil
7579
}
7680

81+
// PerformErrorCorrectionDueToRounding takes care of errors in principal and payment amount due to rounding.
82+
// Only the final row is adjusted for rounding errors.
7783
func PerformErrorCorrectionDueToRounding(finalRow *Row, rows []Row, principal int64, round bool) {
7884
principalCollected := finalRow.Principal
7985
for _, row := range rows {
@@ -101,11 +107,13 @@ func PerformErrorCorrectionDueToRounding(finalRow *Row, rows []Row, principal in
101107
}
102108
}
103109

110+
// PrintRows outputs a formatted json for given rows as input.
104111
func PrintRows(rows []Row) {
105-
bytes, _ := json.MarshalIndent(rows, "", " ")
112+
bytes, _ := json.MarshalIndent(rows, "", "\t")
106113
fmt.Printf("%s", bytes)
107114
}
108115

116+
// PlotRows uses the go-echarts package to generate an interactive plot from the Rows array.
109117
func PlotRows(rows []Row, fileName string) error {
110118
bar := charts.NewBar()
111119
bar.SetGlobalOptions(charts.WithTitleOpts(opts.Title{

amortization_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package calculator
1+
package gofinancial
22

33
import (
44
"fmt"

config.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package calculator
1+
package gofinancial
22

33
import (
44
"errors"
@@ -11,6 +11,7 @@ import (
1111
"github.com/razorpay/go-financial/enums/frequency"
1212
)
1313

14+
// Config is used to store details used in generation of amortization table.
1415
type Config struct {
1516
StartDate time.Time
1617
EndDate time.Time
@@ -25,7 +26,7 @@ type Config struct {
2526
endDates []time.Time // derived
2627
}
2728

28-
func (c *Config) SetPeriodsAndDates() error {
29+
func (c *Config) setPeriodsAndDates() error {
2930
sy, sm, sd := c.StartDate.Date()
3031
startDate := time.Date(sy, sm, sd, 0, 0, 0, 0, c.StartDate.Location())
3132

config_test.go

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package calculator
1+
package gofinancial
22

33
import (
44
"fmt"
@@ -19,9 +19,6 @@ func TestConfig_SetPeriodsAndDates(t *testing.T) {
1919
EndDate time.Time
2020
Frequency Frequency.Type
2121
}
22-
// start inclusive.
23-
// end not inclusive
24-
2522
tests := []struct {
2623
name string
2724
fields fields
@@ -207,7 +204,7 @@ func TestConfig_SetPeriodsAndDates(t *testing.T) {
207204
EndDate: tt.fields.EndDate,
208205
Frequency: tt.fields.Frequency,
209206
}
210-
if err := c.SetPeriodsAndDates(); (err != nil) != tt.wantErr {
207+
if err := c.setPeriodsAndDates(); (err != nil) != tt.wantErr {
211208
t.Fatalf("SetPeriodsAndDates() error = %v, wantErr %v", err, tt.wantErr)
212209
}
213210
if c.periods != tt.wantPeriods {

error_codes.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package calculator
1+
package gofinancial
22

33
import "errors"
44

example_amortization_test.go

+176
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
package gofinancial_test
2+
3+
import (
4+
"time"
5+
6+
gofinancial "github.com/razorpay/go-financial"
7+
"github.com/razorpay/go-financial/enums/frequency"
8+
"github.com/razorpay/go-financial/enums/interesttype"
9+
"github.com/razorpay/go-financial/enums/paymentperiod"
10+
)
11+
12+
// This example generates amortization table for a loan of 20 lakhs over 15years at 12% per annum.
13+
func ExampleAmortization_GenerateTable() {
14+
loc, err := time.LoadLocation("Asia/Kolkata")
15+
if err != nil {
16+
panic("location loading error")
17+
}
18+
currentDate := time.Date(2009, 11, 11, 0o4, 30, 0o0, 0, loc)
19+
config := gofinancial.Config{
20+
21+
// start date is inclusive
22+
StartDate: currentDate,
23+
24+
// end date is inclusive.
25+
EndDate: currentDate.AddDate(15, 0, 0).AddDate(0, 0, -1),
26+
Frequency: frequency.ANNUALLY,
27+
28+
// AmountBorrowed is in paisa
29+
AmountBorrowed: 200000000,
30+
31+
// InterestType can be flat or reducing
32+
InterestType: interesttype.REDUCING,
33+
34+
// interest is in basis points
35+
Interest: 1200,
36+
37+
// amount is paid at the end of the period
38+
PaymentPeriod: paymentperiod.ENDING,
39+
40+
// all values will be rounded
41+
Round: true,
42+
}
43+
amortization, err := gofinancial.NewAmortization(&config)
44+
if err != nil {
45+
panic(err)
46+
}
47+
48+
rows, err := amortization.GenerateTable()
49+
if err != nil {
50+
panic(err)
51+
}
52+
gofinancial.PrintRows(rows)
53+
// Output:
54+
// [
55+
// {
56+
// "Period": 1,
57+
// "StartDate": "2009-11-11T04:30:00+05:30",
58+
// "EndDate": "2010-11-10T23:59:59+05:30",
59+
// "Payment": -29364848,
60+
// "Interest": -24000000,
61+
// "Principal": -5364848
62+
// },
63+
// {
64+
// "Period": 2,
65+
// "StartDate": "2010-11-11T00:00:00+05:30",
66+
// "EndDate": "2011-11-10T23:59:59+05:30",
67+
// "Payment": -29364848,
68+
// "Interest": -23356218,
69+
// "Principal": -6008630
70+
// },
71+
// {
72+
// "Period": 3,
73+
// "StartDate": "2011-11-11T00:00:00+05:30",
74+
// "EndDate": "2012-11-10T23:59:59+05:30",
75+
// "Payment": -29364848,
76+
// "Interest": -22635183,
77+
// "Principal": -6729665
78+
// },
79+
// {
80+
// "Period": 4,
81+
// "StartDate": "2012-11-11T00:00:00+05:30",
82+
// "EndDate": "2013-11-10T23:59:59+05:30",
83+
// "Payment": -29364848,
84+
// "Interest": -21827623,
85+
// "Principal": -7537225
86+
// },
87+
// {
88+
// "Period": 5,
89+
// "StartDate": "2013-11-11T00:00:00+05:30",
90+
// "EndDate": "2014-11-10T23:59:59+05:30",
91+
// "Payment": -29364848,
92+
// "Interest": -20923156,
93+
// "Principal": -8441692
94+
// },
95+
// {
96+
// "Period": 6,
97+
// "StartDate": "2014-11-11T00:00:00+05:30",
98+
// "EndDate": "2015-11-10T23:59:59+05:30",
99+
// "Payment": -29364848,
100+
// "Interest": -19910153,
101+
// "Principal": -9454695
102+
// },
103+
// {
104+
// "Period": 7,
105+
// "StartDate": "2015-11-11T00:00:00+05:30",
106+
// "EndDate": "2016-11-10T23:59:59+05:30",
107+
// "Payment": -29364848,
108+
// "Interest": -18775589,
109+
// "Principal": -10589259
110+
// },
111+
// {
112+
// "Period": 8,
113+
// "StartDate": "2016-11-11T00:00:00+05:30",
114+
// "EndDate": "2017-11-10T23:59:59+05:30",
115+
// "Payment": -29364848,
116+
// "Interest": -17504878,
117+
// "Principal": -11859970
118+
// },
119+
// {
120+
// "Period": 9,
121+
// "StartDate": "2017-11-11T00:00:00+05:30",
122+
// "EndDate": "2018-11-10T23:59:59+05:30",
123+
// "Payment": -29364848,
124+
// "Interest": -16081682,
125+
// "Principal": -13283166
126+
// },
127+
// {
128+
// "Period": 10,
129+
// "StartDate": "2018-11-11T00:00:00+05:30",
130+
// "EndDate": "2019-11-10T23:59:59+05:30",
131+
// "Payment": -29364848,
132+
// "Interest": -14487702,
133+
// "Principal": -14877146
134+
// },
135+
// {
136+
// "Period": 11,
137+
// "StartDate": "2019-11-11T00:00:00+05:30",
138+
// "EndDate": "2020-11-10T23:59:59+05:30",
139+
// "Payment": -29364848,
140+
// "Interest": -12702445,
141+
// "Principal": -16662403
142+
// },
143+
// {
144+
// "Period": 12,
145+
// "StartDate": "2020-11-11T00:00:00+05:30",
146+
// "EndDate": "2021-11-10T23:59:59+05:30",
147+
// "Payment": -29364848,
148+
// "Interest": -10702956,
149+
// "Principal": -18661892
150+
// },
151+
// {
152+
// "Period": 13,
153+
// "StartDate": "2021-11-11T00:00:00+05:30",
154+
// "EndDate": "2022-11-10T23:59:59+05:30",
155+
// "Payment": -29364848,
156+
// "Interest": -8463529,
157+
// "Principal": -20901319
158+
// },
159+
// {
160+
// "Period": 14,
161+
// "StartDate": "2022-11-11T00:00:00+05:30",
162+
// "EndDate": "2023-11-10T23:59:59+05:30",
163+
// "Payment": -29364848,
164+
// "Interest": -5955371,
165+
// "Principal": -23409477
166+
// },
167+
// {
168+
// "Period": 15,
169+
// "StartDate": "2023-11-11T00:00:00+05:30",
170+
// "EndDate": "2024-11-10T23:59:59+05:30",
171+
// "Payment": -29364849,
172+
// "Interest": -3146234,
173+
// "Principal": -26218615
174+
// }
175+
//]
176+
}

0 commit comments

Comments
 (0)