Skip to content

Generated data are not Error checked #6

Description

@brockelmore

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
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions