Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generated data are not Error checked #6

Open
brockelmore opened this issue Feb 16, 2019 · 1 comment
Open

Generated data are not Error checked #6

brockelmore opened this issue Feb 16, 2019 · 1 comment

Comments

@brockelmore
Copy link

brockelmore commented Feb 16, 2019

Dps specifically needs to be checked otherwise *math.Inf or *math.NaN values can mess with the json encoding resulting in a fatal crash as the encoding library does not handle these values for you.

data_def.go changes:

import (
	"errors"
	"fmt"
	"log"
	"reflect"
	"math"
)
// -- snipped

func generateData(fin *financialReport, name string) float64 {
	log.Println("Generating data: ", name)
	switch name {
	case "GrossMargin":
		//Do this only when the parsing is complete for required fields
		if isCollectedDataSet(fin.Ops, "Revenue") && isCollectedDataSet(fin.Ops, "CostOfSales") {
			log.Println("Generating Gross Margin")
			if !math.IsInf(fin.Ops.Revenue - fin.Ops.CostOfSales, 0) && !math.IsNaN(fin.Ops.Revenue - fin.Ops.CostOfSales){
				return fin.Ops.Revenue - fin.Ops.CostOfSales
			}
		}

	case "Dps":
		if isCollectedDataSet(fin.Cf, "Dividends") {
			if isCollectedDataSet(fin.Ops, "WAShares") {
				if !math.IsInf(round(fin.Cf.Dividends * -1 / fin.Ops.WAShares), 0) && !math.IsNaN(round(fin.Cf.Dividends * -1 / fin.Ops.WAShares)){
					return round(fin.Cf.Dividends * -1 / fin.Ops.WAShares)
				}
			} else if isCollectedDataSet(fin.Entity, "ShareCount") {
				if !math.IsInf(round(fin.Cf.Dividends * -1 / fin.Entity.ShareCount), 0) && !math.IsNaN(round(fin.Cf.Dividends * -1 / fin.Entity.ShareCount)){
					return round(fin.Cf.Dividends * -1 / fin.Entity.ShareCount)
				}
			}
		}
	case "OpExpense":
		if isCollectedDataSet(fin.Ops, "Revenue") &&
			isCollectedDataSet(fin.Ops, "CostOfSales") &&
			isCollectedDataSet(fin.Ops, "OpIncome") {
				if !math.IsInf(round(fin.Ops.Revenue - fin.Ops.CostOfSales - fin.Ops.OpIncome),0) && !math.IsNaN(round(fin.Ops.Revenue - fin.Ops.CostOfSales - fin.Ops.OpIncome)) {
					return round(fin.Ops.Revenue - fin.Ops.CostOfSales - fin.Ops.OpIncome)
				}
		}
	}
	return 0
}
@palafrank
Copy link
Owner

The reason for NaN should have either been a parse error with getting the field, in which case the CollectedData should have been false, or if the WAS or Shares is 0. Which again would have been a parsing error. What is the scenario in which you run into this case and need this check. The check in itself is harmless, but I am trying to understand why to get there

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants