Skip to content

Commit

Permalink
add examples,documentation and code-coverage gh-action. (#2)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
gyanesh-m committed Dec 27, 2020
1 parent d1ad19f commit 9ff7905
Show file tree
Hide file tree
Showing 15 changed files with 549 additions and 58 deletions.
30 changes: 27 additions & 3 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Testing
name: unit-tests

on: [ push ]

Expand All @@ -20,12 +20,36 @@ jobs:
run: |
echo "Don't cancel old workflow"
tests:
name: Unit testing
name: unit-tests
runs-on: ubuntu-latest
steps:
- name: Install Go
uses: actions/setup-go@v2
- name: Checkout code
uses: actions/checkout@v2
- name: Unit test
run: go test -v ./...
run: go test -v ./... -coverprofile=coverage.txt
- name: Archive code coverage results
if: contains(github.ref, 'master')
uses: actions/upload-artifact@v2
with:
name: code-coverage-report
path: ./coverage.txt
code-coverage:
name: Upload code coverage
runs-on: ubuntu-latest
needs: tests
if: contains(github.ref, 'master')
steps:
- uses: actions/checkout@master
- name: Download coverage report
uses: actions/download-artifact@v2
with:
name: code-coverage-report
- uses: codecov/codecov-action@v1
with:
file: ./coverage.txt
flags: unittests
name: go-financial
fail_ci_if_error: true
verbose: true
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2020 Razorpay

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ This package is a go native port of the numpy-financial package with some additi
functions.

The functions in this package are a scalar version of their vectorised counterparts in
the numpy-financial library.
the [numpy-financial](https://github.com/numpy/numpy-financial) library.

[![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")
[![Go Report Card](https://goreportcard.com/badge/github.com/razorpay/go-financial)](https://goreportcard.com/report/github.com/razorpay/go-financial)
[![codecov](https://codecov.io/gh/razorpay/go-financial/branch/master/graph/badge.svg)](https://codecov.io/gh/razorpay/go-financial)
[![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)

Currently, only some functions are ported,
which are as follows:
Expand Down
22 changes: 15 additions & 7 deletions amortization.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package calculator
package gofinancial

import (
"encoding/json"
Expand All @@ -13,14 +13,16 @@ import (
"github.com/razorpay/go-financial/enums/interesttype"
)

type amortization struct {
// Amortization struct holds the configuration and financial details.
type Amortization struct {
Config *Config
Financial Financial
}

func NewAmortization(c *Config) (*amortization, error) {
a := amortization{Config: c}
if err := a.Config.SetPeriodsAndDates(); err != nil {
// NewAmortization return a new amortisation object with config and financial fields initialised.
func NewAmortization(c *Config) (*Amortization, error) {
a := Amortization{Config: c}
if err := a.Config.setPeriodsAndDates(); err != nil {
return nil, err
}

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

// Row represents a single row in an amortization schedule.
type Row struct {
Period int64
StartDate time.Time
Expand All @@ -42,7 +45,8 @@ type Row struct {
Principal float64
}

func (a amortization) GenerateTable() ([]Row, error) {
// GenerateTable constructs the amortization table based on the configuration.
func (a Amortization) GenerateTable() ([]Row, error) {
var result []Row
for i := int64(1); i <= a.Config.periods; i++ {
var row Row
Expand Down Expand Up @@ -74,6 +78,8 @@ func (a amortization) GenerateTable() ([]Row, error) {
return result, nil
}

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

// PrintRows outputs a formatted json for given rows as input.
func PrintRows(rows []Row) {
bytes, _ := json.MarshalIndent(rows, "", " ")
bytes, _ := json.MarshalIndent(rows, "", "\t")
fmt.Printf("%s", bytes)
}

// PlotRows uses the go-echarts package to generate an interactive plot from the Rows array.
func PlotRows(rows []Row, fileName string) error {
bar := charts.NewBar()
bar.SetGlobalOptions(charts.WithTitleOpts(opts.Title{
Expand Down
2 changes: 1 addition & 1 deletion amortization_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package calculator
package gofinancial

import (
"fmt"
Expand Down
5 changes: 3 additions & 2 deletions config.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package calculator
package gofinancial

import (
"errors"
Expand All @@ -11,6 +11,7 @@ import (
"github.com/razorpay/go-financial/enums/frequency"
)

// Config is used to store details used in generation of amortization table.
type Config struct {
StartDate time.Time
EndDate time.Time
Expand All @@ -25,7 +26,7 @@ type Config struct {
endDates []time.Time // derived
}

func (c *Config) SetPeriodsAndDates() error {
func (c *Config) setPeriodsAndDates() error {
sy, sm, sd := c.StartDate.Date()
startDate := time.Date(sy, sm, sd, 0, 0, 0, 0, c.StartDate.Location())

Expand Down
7 changes: 2 additions & 5 deletions config_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package calculator
package gofinancial

import (
"fmt"
Expand All @@ -19,9 +19,6 @@ func TestConfig_SetPeriodsAndDates(t *testing.T) {
EndDate time.Time
Frequency Frequency.Type
}
// start inclusive.
// end not inclusive

tests := []struct {
name string
fields fields
Expand Down Expand Up @@ -207,7 +204,7 @@ func TestConfig_SetPeriodsAndDates(t *testing.T) {
EndDate: tt.fields.EndDate,
Frequency: tt.fields.Frequency,
}
if err := c.SetPeriodsAndDates(); (err != nil) != tt.wantErr {
if err := c.setPeriodsAndDates(); (err != nil) != tt.wantErr {
t.Fatalf("SetPeriodsAndDates() error = %v, wantErr %v", err, tt.wantErr)
}
if c.periods != tt.wantPeriods {
Expand Down
2 changes: 1 addition & 1 deletion error_codes.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package calculator
package gofinancial

import "errors"

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

import (
"time"

gofinancial "github.com/razorpay/go-financial"
"github.com/razorpay/go-financial/enums/frequency"
"github.com/razorpay/go-financial/enums/interesttype"
"github.com/razorpay/go-financial/enums/paymentperiod"
)

// This example generates amortization table for a loan of 20 lakhs over 15years at 12% per annum.
func ExampleAmortization_GenerateTable() {
loc, err := time.LoadLocation("Asia/Kolkata")
if err != nil {
panic("location loading error")
}
currentDate := time.Date(2009, 11, 11, 0o4, 30, 0o0, 0, loc)
config := gofinancial.Config{

// start date is inclusive
StartDate: currentDate,

// end date is inclusive.
EndDate: currentDate.AddDate(15, 0, 0).AddDate(0, 0, -1),
Frequency: frequency.ANNUALLY,

// AmountBorrowed is in paisa
AmountBorrowed: 200000000,

// InterestType can be flat or reducing
InterestType: interesttype.REDUCING,

// interest is in basis points
Interest: 1200,

// amount is paid at the end of the period
PaymentPeriod: paymentperiod.ENDING,

// all values will be rounded
Round: true,
}
amortization, err := gofinancial.NewAmortization(&config)
if err != nil {
panic(err)
}

rows, err := amortization.GenerateTable()
if err != nil {
panic(err)
}
gofinancial.PrintRows(rows)
// Output:
// [
// {
// "Period": 1,
// "StartDate": "2009-11-11T04:30:00+05:30",
// "EndDate": "2010-11-10T23:59:59+05:30",
// "Payment": -29364848,
// "Interest": -24000000,
// "Principal": -5364848
// },
// {
// "Period": 2,
// "StartDate": "2010-11-11T00:00:00+05:30",
// "EndDate": "2011-11-10T23:59:59+05:30",
// "Payment": -29364848,
// "Interest": -23356218,
// "Principal": -6008630
// },
// {
// "Period": 3,
// "StartDate": "2011-11-11T00:00:00+05:30",
// "EndDate": "2012-11-10T23:59:59+05:30",
// "Payment": -29364848,
// "Interest": -22635183,
// "Principal": -6729665
// },
// {
// "Period": 4,
// "StartDate": "2012-11-11T00:00:00+05:30",
// "EndDate": "2013-11-10T23:59:59+05:30",
// "Payment": -29364848,
// "Interest": -21827623,
// "Principal": -7537225
// },
// {
// "Period": 5,
// "StartDate": "2013-11-11T00:00:00+05:30",
// "EndDate": "2014-11-10T23:59:59+05:30",
// "Payment": -29364848,
// "Interest": -20923156,
// "Principal": -8441692
// },
// {
// "Period": 6,
// "StartDate": "2014-11-11T00:00:00+05:30",
// "EndDate": "2015-11-10T23:59:59+05:30",
// "Payment": -29364848,
// "Interest": -19910153,
// "Principal": -9454695
// },
// {
// "Period": 7,
// "StartDate": "2015-11-11T00:00:00+05:30",
// "EndDate": "2016-11-10T23:59:59+05:30",
// "Payment": -29364848,
// "Interest": -18775589,
// "Principal": -10589259
// },
// {
// "Period": 8,
// "StartDate": "2016-11-11T00:00:00+05:30",
// "EndDate": "2017-11-10T23:59:59+05:30",
// "Payment": -29364848,
// "Interest": -17504878,
// "Principal": -11859970
// },
// {
// "Period": 9,
// "StartDate": "2017-11-11T00:00:00+05:30",
// "EndDate": "2018-11-10T23:59:59+05:30",
// "Payment": -29364848,
// "Interest": -16081682,
// "Principal": -13283166
// },
// {
// "Period": 10,
// "StartDate": "2018-11-11T00:00:00+05:30",
// "EndDate": "2019-11-10T23:59:59+05:30",
// "Payment": -29364848,
// "Interest": -14487702,
// "Principal": -14877146
// },
// {
// "Period": 11,
// "StartDate": "2019-11-11T00:00:00+05:30",
// "EndDate": "2020-11-10T23:59:59+05:30",
// "Payment": -29364848,
// "Interest": -12702445,
// "Principal": -16662403
// },
// {
// "Period": 12,
// "StartDate": "2020-11-11T00:00:00+05:30",
// "EndDate": "2021-11-10T23:59:59+05:30",
// "Payment": -29364848,
// "Interest": -10702956,
// "Principal": -18661892
// },
// {
// "Period": 13,
// "StartDate": "2021-11-11T00:00:00+05:30",
// "EndDate": "2022-11-10T23:59:59+05:30",
// "Payment": -29364848,
// "Interest": -8463529,
// "Principal": -20901319
// },
// {
// "Period": 14,
// "StartDate": "2022-11-11T00:00:00+05:30",
// "EndDate": "2023-11-10T23:59:59+05:30",
// "Payment": -29364848,
// "Interest": -5955371,
// "Principal": -23409477
// },
// {
// "Period": 15,
// "StartDate": "2023-11-11T00:00:00+05:30",
// "EndDate": "2024-11-10T23:59:59+05:30",
// "Payment": -29364849,
// "Interest": -3146234,
// "Principal": -26218615
// }
//]
}

0 comments on commit 9ff7905

Please sign in to comment.