util

package
v0.0.0-...-03c68b9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 3, 2019 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Overview

* Copyright Project - Stockbinator, Author - quoeamaster, (C) 2019 * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License.

* Copyright Project - Stockbinator, Author - quoeamaster, (C) 2019 * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License.

* Copyright Project - Stockbinator, Author - quoeamaster, (C) 2019 * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License.

* Copyright Project - Stockbinator, Author - quoeamaster, (C) 2019 * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License.

* Copyright Project - Stockbinator, Author - quoeamaster, (C) 2019 * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License.

Index

Constants

View Source
const CommonDateFormat = "2006-01-02T15:04:05-07:00"

common date format for parsing => yyyy-MM-ddThh:mm:ss-07:00

View Source
const DefaultConfigValString = "NIL"
View Source
const ParseErrKeyPathNotFound = "Key path not found"

Variables

This section is empty.

Functions

func ConcatenateFilePath

func ConcatenateFilePath(paths ...string) (finalPath string)

way to concatenate strings together to form a final-path

func CreateTodayTargetTimeByHourMinTimezone

func CreateTodayTargetTimeByHourMinTimezone(hour, min int, timezone string) (todayTargetTime string)

2019-05-28T07:30:00+0000

func GetConfigFolderPath

func GetConfigFolderPath() (repoPath string, err error)

common method to get the config-folder's path: 1. check if env variable SB_CONFIG_PATH is set or not, if so, use it as repo path OR 2. get the current executable path and append "/config" to this path

PS. there is no guarantee that the path decided is available or not

func GetContentFromUrl

func GetContentFromUrl(url string) (content string, err error)

method to get contents from a URL

func GetCurrentYearHolidays

func GetCurrentYearHolidays(pHolidayConfig *config.Config) (holidaySlice []string, err error)

func GetExecutablePath

func GetExecutablePath(excludeExecutable bool) (ePath string, err error)

return the executable's fullpath; in "excludeExecutable" is true then the return path would exclude the executable's name

func GetLastPathElement

func GetLastPathElement(finalPath string) (string, error)

extract the final sub-path of the given path; the separator is based on the OS's path separator

func GetLoggerFolderPath

func GetLoggerFolderPath(initialPath string) (repoPath string, err error)

similar to GetConfigFolderPath(), this method parses the env variable OR the hard-coded path provided when hard-coded path is provided; first check if the path is empty string or not; if empty => get the executable's path append "log" to it as final path OR just use the given hard-coded path if non empty

PS. there is no guarantee on the path is available or not

func GetPathSeparator

func GetPathSeparator() string

return the path separator based on current OS

func GetRequestBodyInBytes

func GetRequestBodyInBytes(pBody *io.ReadCloser) (bContent []byte, err error)

method to parse and return the request body contents in []byte

func GetTimeTruncatedDate

func GetTimeTruncatedDate(givenDate *time.Time) (date time.Time, err error)

func IsEmptyString

func IsEmptyString(value string) bool

method to check if the supplied string is empty or not

func IsFileExists

func IsFileExists(filePath string) (exists bool, err error)

check if the given filepath exists and valid or not

func IsFolderExists

func IsFolderExists(filePath string) (exists bool, err error)

func IsHoliday

func IsHoliday(date *time.Time, dateInString *string, holidays []string) (isHoliday bool, err error)

check if the given date is one of the holidays given

func IsValidTimePart

func IsValidTimePart(value int, partName string) (valid bool)

validates if the given time part is valid (e.g. hour24 = 0~23, min = 0~59, sec = 0~59)

func IsValidTimezone

func IsValidTimezone(value string) (valid bool)

method to check if the given string is a valid timezone format => 1) +08:00 OR 2) +8:00 OR 3) -07:00

func IsWeekend

func IsWeekend(date time.Time) (isWeekend bool)

check if the given date is on weekend (day-of-week 0 = sunday, 6 = saturday)

func ListMatchingFoldersUnderPath

func ListMatchingFoldersUnderPath(repoPath string, folderPrefix string) (matchedFolderPaths []string, err error)

get back only matched folders under a given path; matching criteria is the folder starts with the given folder-prefix

func LoadConfig

func LoadConfig(filePath string) (configObject config.Config, err error)

method to load a given file into a Config object

func ParseEnvVar

func ParseEnvVar(key string) (matches []string, matchesIndex [][]int, err error)

parse and find out the env variable keys (e.g. {ENV_VAR_KEY} ) plus the indices involved per match

func ParseStringDateToTodayUTC

func ParseStringDateToTodayUTC(hh24, mm int, timezone string) (pDate time.Time, err error)

helper method to create the date-time based on today's date associated with the hours plus minutes plus timezone provided

func PrepareTimebasedPatternWithGivenTime

func PrepareTimebasedPatternWithGivenTime(pattern string, givenTime time.Time) (v string)

helper method to replace the time pattern string with the given time's values

Types

type StructCommonResponse

type StructCommonResponse struct {
	// http response code (only 200 or 201 is a Successful operation)
	ResponseCode int
	// optional string message to describe the operation
	Message string
}

func NewStructCommonResponse

func NewStructCommonResponse(responseCode int, message string) *StructCommonResponse

create method for the common-response object

type StructJsonParser

type StructJsonParser struct {
	// contains filtered or unexported fields
}

important concept about usage of StructJsonParser. scenario 1: do not set ignore-error list

  • basically, if there were any error occurred during the Get(), all kinds of error would exposed; however there are error(s) that you might want to ignore -> "key path not found" especially when certain parameters might be optional. In this case the developer would need to handle all error checking and ignore error(s) that might do no harm to your biz logic.

scenario 2: set ignore-error list + using DefaultValue()

  • if we would like to ignore "key path not found" error; then how could we know if the value returned is valid or just the default value for casting failure? We can deal with this by setting the DefaultValue() and perform a bool check against the value returned by a key versus the provided DefaultValue; if both values are identical that means the casting failed (probably because of "key path not found" in this scenario)

code snippet:

c.pJsonParser.ResetIgnoreError().IgnoreError(util.ParseErrKeyPathNotFound)

paramVal, err := c.pJsonParser.Get(bArr, "hour24")
if err != nil { // really is an unexpected error
  fmt.Println(err)
} else {
  if paramVal.Default(-1).IntValue() == -1 { // means key path not found...
    fmt.Println("key path not found??? hour24")
  } else {
    fmt.Printf("%v = %v~\n", "hour24", paramVal.IntValue())
  }
}

func NewStructJsonParser

func NewStructJsonParser() (pParser *StructJsonParser)

creator method for the JsonParser struct

func (*StructJsonParser) Get

func (j *StructJsonParser) Get(byteContent []byte, keys ...string) (value StructJsonValue, err error)

func (*StructJsonParser) IgnoreError

func (j *StructJsonParser) IgnoreError(description ...string)

updates the ignoreList with the given description(s); only new description(s) would be appended to the ignore-list

func (*StructJsonParser) ResetIgnoreError

func (j *StructJsonParser) ResetIgnoreError() *StructJsonParser

reset the ignore-error descriptions

type StructJsonValue

type StructJsonValue struct {
	// contains filtered or unexported fields
}

func (*StructJsonValue) Default

func (v *StructJsonValue) Default(value interface{}) *StructJsonValue

set a default value for the underneath value property

func (*StructJsonValue) IntValue

func (v *StructJsonValue) IntValue() (iValue int)

cast the original (string) to an int, error would be encapsulate inside the StructJsonValue.error property instead

func (*StructJsonValue) StringValue

func (v *StructJsonValue) StringValue() (sValue string)

super easy implementation since, the underneath value attribute is already a string. Simply return this value

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL