From 66c0b13553afbf4d2482e274f542120c75a00339 Mon Sep 17 00:00:00 2001 From: Colin Cross Date: Wed, 25 Sep 2019 11:25:04 -0700 Subject: [PATCH] Add proptools.Int and proptools.IntDefault Add proptools.Int and proptools.IntDefault that behave analogously to proptools.String and proptools.StringDefault. Change-Id: I41fd3417c973c9ff4a5aa6680546b4b893784745 --- go.mod | 2 ++ proptools/proptools.go | 15 +++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/go.mod b/go.mod index 933cd12..fe96d45 100644 --- a/go.mod +++ b/go.mod @@ -1 +1,3 @@ module github.com/google/blueprint + +go 1.13 diff --git a/proptools/proptools.go b/proptools/proptools.go index e6e3ae7..6881828 100644 --- a/proptools/proptools.go +++ b/proptools/proptools.go @@ -82,3 +82,18 @@ func StringDefault(s *string, def string) string { func String(s *string) string { return StringDefault(s, "") } + +// IntDefault takes a pointer to an int64 and returns the value pointed to by the pointer cast to int +// if it is non-nil, or def if the pointer is nil. +func IntDefault(i *int64, def int) int { + if i != nil { + return int(*i) + } + return def +} + +// Int takes a pointer to an int64 and returns the value pointed to by the pointer cast to int +// if it is non-nil, or 0 if the pointer is nil. +func Int(i *int64) int { + return IntDefault(i, 0) +}