diff --git a/commonutils/logwrapper/logger.go b/commonutils/logwrapper/logger.go index 30c83157..d9929e58 100644 --- a/commonutils/logwrapper/logger.go +++ b/commonutils/logwrapper/logger.go @@ -5,6 +5,7 @@ package logger import "github.com/sirupsen/logrus" +// Supported log levels. type LogLevel int const ( @@ -24,22 +25,27 @@ func init() { log.SetLevel(logrus.ErrorLevel) } +// Internal func Debug(args ...interface{}) { log.Debug(args...) } +// Internal func Info(args ...interface{}) { log.Info(args...) } +// Internal func Warn(args ...interface{}) { log.Warn(args...) } +// Internal func Error(args ...interface{}) { log.Error(args...) } +// Sets the logging level for the application. Defaults to `ERROR`. func SetLogLevel(level LogLevel) { switch level { case INFO: diff --git a/processMarkdown.go b/processMarkdown.go new file mode 100644 index 00000000..7787e62e --- /dev/null +++ b/processMarkdown.go @@ -0,0 +1,144 @@ +package main + +import ( + "fmt" + "io/ioutil" + "regexp" + "strings" +) + +func main() { + // Read the markdown file + files := []string{"client", "common", "logger", "util", "vaultapi"} + + for _, filename := range files { + file := "docs/" + filename + ".md" + data, err := ioutil.ReadFile(file) + if err != nil { + fmt.Println("Error reading file:", err) + return + } + // fmt.Println(data) + // Convert byte data to string + markdown := string(data) + + markdown = "{% env enable=\"goSdkRef\" %}\n\n" + markdown + + // Remove import statement + markdown = removeImportStatement(markdown) + + // Remove copyright statement + markdown = removeCopyrightStatement(markdown) + + // Remove functions with Deprecated description + markdown = removeDeprecatedOrInternal(markdown) + + // Represent type as h2 + markdown = changeTypeHeading(markdown) + + // Represent functions as h3 + markdown = changeFuncHeading(markdown) + + // Remove multiple empty lines + markdown = removeMultipleEmptyLines(markdown) + + markdown = markdown + "\n{% /env %}" + + // Write the modified markdown back to the file + err = ioutil.WriteFile(file, []byte(markdown), 0644) + if err != nil { + fmt.Println("Error writing file:", err) + return + } + } + + fmt.Println("Markdown files updated successfully.") + +} + +func removeImportStatement(markdown string) string { + // Use regex to match and remove import statement + importRegex := regexp.MustCompile(`--\n\s*import\s+"."\n`) + markdown = importRegex.ReplaceAllString(markdown, "") + + return markdown +} + +func removeCopyrightStatement(markdown string) string { + // Use regex to match and remove copyright statement + copyrightRegex := regexp.MustCompile(`\s*Copyright.*`) + markdown = copyrightRegex.ReplaceAllString(markdown, "") + + return markdown +} + +// func removeDeprecatedFunctions(markdown string) string { +// // Regular expression pattern to match the deprecated functions +// pattern := regexp.MustCompile(`(?s)(?m)#### func\s*((?!#### func).)*?Deprecated.*?\n\n`) +// return pattern.ReplaceAllString(markdown, "") +// } + +func sum(arr []int) int { + sum := 0 + for _, valueInt := range arr { + sum += valueInt + } + return sum +} + +func removeDeprecatedOrInternal(markdown string) string { + lines := strings.Split(markdown, "\n") + var result []string + var temp []int + startIndex := 0 + endIndex := 0 + for i, line := range lines { + if strings.HasPrefix(line, "#### func") { + startIndex = i + } else if strings.HasPrefix(line, "#### type") { + startIndex = i + } else if strings.HasPrefix(line, "Deprecated") { + endIndex = i + } else if strings.HasPrefix(line, "Internal") { + endIndex = i + } + + result = append(result, line) + if startIndex > 0 && endIndex > 0 { + result = append(result[:startIndex-(sum(temp)+1)]) + temp = append(temp, (endIndex-startIndex)+1) + startIndex = 0 + endIndex = 0 + } + } + + return strings.Join(result, "\n") +} + +func changeTypeHeading(markdown string) string { + // Regular expression pattern to match type headings (h4) + pattern := regexp.MustCompile(`#### type`) + + // Replace type heading (h4) with h2 + return pattern.ReplaceAllStringFunc(markdown, func(match string) string { + return strings.Replace(match, "####", "##", 1) + }) +} + +func changeFuncHeading(markdown string) string { + // Regular expression pattern to match func headings (h4) + pattern := regexp.MustCompile(`#### func`) + + // Replace func heading (h4) with h3 + return pattern.ReplaceAllStringFunc(markdown, func(match string) string { + return strings.Replace(match, "####", "###", 1) + }) +} + +func removeMultipleEmptyLines(markdown string) string { + // Regular expression pattern to match multiple empty lines + pattern := regexp.MustCompile(`\n{2,}`) + + // Replace multiple empty lines with a single empty line + return pattern.ReplaceAllString(markdown, "\n\n") +} \ No newline at end of file diff --git a/serviceaccount/util/token.go b/serviceaccount/util/token.go index 673be968..4a827387 100644 --- a/serviceaccount/util/token.go +++ b/serviceaccount/util/token.go @@ -22,6 +22,7 @@ import ( "github.com/skyflowapi/skyflow-go/skyflow/common" ) +// Represents the structure for a response containing an access token and its type. type ResponseToken struct { AccessToken string `json:"accessToken"` TokenType string `json:tokenType` @@ -35,7 +36,7 @@ func GenerateToken(filePath string) (*ResponseToken, *errors.SkyflowError) { return GenerateBearerToken(filePath) } -// GenerateBearerToken - Generates a Service Account Token from the given Service Account Credential file with a default timeout of 60minutes. +// Generates a service account token from the given service account credential file with a default timeout of 60 minutes. func GenerateBearerToken(filePath string) (*ResponseToken, *errors.SkyflowError) { var key map[string]interface{} @@ -66,6 +67,7 @@ func GenerateBearerToken(filePath string) (*ResponseToken, *errors.SkyflowError) return token, nil } +// Generates a service account token from the given service account credential JSON string. func GenerateBearerTokenFromCreds(credentials string) (*ResponseToken, *errors.SkyflowError) { credsMap := make(map[string]interface{}) @@ -83,7 +85,7 @@ func GenerateBearerTokenFromCreds(credentials string) (*ResponseToken, *errors.S return token, nil } -// getSATokenFromCredsFile gets bearer token from service account endpoint +// Gets bearer token from service account credentials file. func getSATokenFromCredsFile(key map[string]interface{}) (*ResponseToken, *errors.SkyflowError) { privateKey := key["privateKey"] @@ -234,6 +236,7 @@ func IsValid(tokenString string) bool { return !IsExpired(tokenString) } +// Checks if the provided service account token has expired. func IsExpired(tokenString string) bool { if tokenString == "" { logger.Info(fmt.Sprintf(messages.EMPTY_BEARER_TOKEN, "ServiceAccountUtil")) diff --git a/skyflow/client/client.go b/skyflow/client/client.go index 3bab27fc..ea5c6e05 100644 --- a/skyflow/client/client.go +++ b/skyflow/client/client.go @@ -14,6 +14,7 @@ import ( vaultapi "github.com/skyflowapi/skyflow-go/skyflow/vaultapi" ) +// Represents a client instance that interacts with Skyflow's APIs. type Client struct { configuration common.Configuration } @@ -22,6 +23,7 @@ var clientTag = "Client" var tokenUtils TokenUtils +// Inserts data into the vault. func (client *Client) Insert(records map[string]interface{}, options ...common.InsertOptions) (common.InsertRecords, *errors.SkyflowError) { var tempOptions common.InsertOptions if len(options) == 0 { @@ -54,6 +56,7 @@ func (client *Client) Insert(records map[string]interface{}, options ...common.I return response, nil } +// Returns values that correspond to the specified tokens. func (client *Client) Detokenize(records map[string]interface{}) (common.DetokenizeRecords, *errors.SkyflowError) { if client.configuration.TokenProvider == nil { @@ -81,6 +84,7 @@ func (client *Client) Detokenize(records map[string]interface{}) (common.Detoken return response, nil } +// Reveals records by Skyflow ID. func (client *Client) GetById(records map[string]interface{}) (common.GetByIdRecords, *errors.SkyflowError) { if client.configuration.TokenProvider == nil { @@ -108,6 +112,7 @@ func (client *Client) GetById(records map[string]interface{}) (common.GetByIdRec return response, nil } +// Invokes a connection to an external service using the provided connection configuration. func (client *Client) InvokeConnection(connectionConfig common.ConnectionConfig) (common.ResponseBody, *errors.SkyflowError) { if client.configuration.TokenProvider == nil { diff --git a/skyflow/client/init.go b/skyflow/client/init.go index 8ffefe58..20c197dd 100644 --- a/skyflow/client/init.go +++ b/skyflow/client/init.go @@ -11,6 +11,7 @@ import ( "github.com/skyflowapi/skyflow-go/skyflow/common" ) +// Initializes the Skyflow client. func Init(configuration common.Configuration) Client { logger.Info(fmt.Sprintf(messages.INITIALIZING_SKYFLOW_CLIENT, clientTag)) return Client{configuration} diff --git a/skyflow/client/token_utils.go b/skyflow/client/token_utils.go index ea5d7005..912a519a 100644 --- a/skyflow/client/token_utils.go +++ b/skyflow/client/token_utils.go @@ -14,6 +14,7 @@ import ( "github.com/skyflowapi/skyflow-go/skyflow/common" ) +// Represents a utility structure for handling bearer tokens. type TokenUtils struct { Token string } diff --git a/skyflow/common/common.go b/skyflow/common/common.go index b9d71b4b..5e3e4afd 100644 --- a/skyflow/common/common.go +++ b/skyflow/common/common.go @@ -3,9 +3,13 @@ Copyright (c) 2022 Skyflow, Inc. */ package common +// Internal type ResponseBody map[string]interface{} + +// Helper function that retrieves a Skyflow bearer token from your backend. type TokenProvider func() (string, error) +// Supported request methods. type RequestMethod int const ( @@ -16,10 +20,12 @@ const ( DELETE ) +// Returns the string representation of the RequestMethod. func (requestMethod RequestMethod) String() string { return [...]string{"GET", "POST", "PUT", "PATCH", "DELETE"}[requestMethod] } +// Supported redaction types. type RedactionType string const ( @@ -29,6 +35,7 @@ const ( REDACTED RedactionType = "REDACTED" ) +// Supported connection configurations. type ConnectionConfig struct { ConnectionURL string MethodName RequestMethod @@ -38,83 +45,102 @@ type ConnectionConfig struct { RequestHeader map[string]string } +// Wrapper for parameters required by insert options. type InsertOptions struct { Tokens bool Upsert []UpsertOptions } + +// Wrapper for parameters required by upsert options. type UpsertOptions struct { Table string Column string } +// Contains the parameters required for Skyflow client initialisation. type Configuration struct { VaultID string VaultURL string TokenProvider TokenProvider } +// Internal type InsertRecords struct { Records []InsertRecord } +// Internal type InsertRecord struct { Table string Fields map[string]interface{} } +// Internal type DetokenizeInput struct { Records []RevealRecord } +// Internal type RevealRecord struct { Token string Redaction string } +// Internal type DetokenizeRecords struct { Records []DetokenizeRecord Errors []DetokenizeError } +// Internal type DetokenizeRecord struct { Token string Value string } +// Internal type DetokenizeError struct { Token string Error ResponseError } +// Internal type ResponseError struct { Code string Description string } +// Internal type GetByIdInput struct { Records []SkyflowIdRecord } +// Internal type GetByIdRecords struct { Records []GetByIdRecord Errors []GetByIdError } +// Internal type GetByIdRecord struct { Fields map[string]interface{} Table string } +// Internal type GetByIdError struct { Ids []string Error ResponseError } + +// Internal type SkyflowIdRecord struct { Ids []string Redaction RedactionType Table string } +// Supported content types. type ContentType string const ( diff --git a/skyflow/common/helpers.go b/skyflow/common/helpers.go index 686480fd..40cfb35a 100644 --- a/skyflow/common/helpers.go +++ b/skyflow/common/helpers.go @@ -10,6 +10,7 @@ import ( logger "github.com/skyflowapi/skyflow-go/commonutils/logwrapper" ) +// Internal func AppendRequestId(message string, requestId string) string { if requestId == "" { return message @@ -18,6 +19,7 @@ func AppendRequestId(message string, requestId string) string { return message + " - requestId : " + requestId } +// Internal func CreateJsonMetadata() string { // Create a map to hold the key-value pairs data := map[string]string{ diff --git a/skyflow/vaultapi/detokenize.go b/skyflow/vaultapi/detokenize.go index 29346483..c81f2228 100644 --- a/skyflow/vaultapi/detokenize.go +++ b/skyflow/vaultapi/detokenize.go @@ -16,6 +16,7 @@ import ( "github.com/skyflowapi/skyflow-go/skyflow/common" ) +// Represents the configuration and data required for performing detokenization. type DetokenizeApi struct { Configuration common.Configuration Records map[string]interface{} @@ -24,6 +25,7 @@ type DetokenizeApi struct { var detokenizeTag = "Detokenize" +// Reveals detokenized data using the Detokenize API. func (detokenize *DetokenizeApi) Get() (map[string]interface{}, *errors.SkyflowError) { err := detokenize.doValidations() diff --git a/skyflow/vaultapi/get_by_id.go b/skyflow/vaultapi/get_by_id.go index 742f5cd8..7628b4d3 100644 --- a/skyflow/vaultapi/get_by_id.go +++ b/skyflow/vaultapi/get_by_id.go @@ -17,6 +17,7 @@ import ( "github.com/skyflowapi/skyflow-go/skyflow/common" ) +// Retrieves records using SkyflowIDs. type GetByIdApi struct { Configuration common.Configuration Records map[string]interface{} @@ -25,6 +26,7 @@ type GetByIdApi struct { var getByIdTag = "GetById" +// Retrieves records by ID from the Skyflow vault. func (g *GetByIdApi) Get() (map[string]interface{}, *errors.SkyflowError) { err := g.doValidations() diff --git a/skyflow/vaultapi/insert.go b/skyflow/vaultapi/insert.go index 843fe820..7f936a2a 100644 --- a/skyflow/vaultapi/insert.go +++ b/skyflow/vaultapi/insert.go @@ -18,6 +18,7 @@ import ( "github.com/skyflowapi/skyflow-go/skyflow/common" ) +// Inserts data into the vault. type InsertApi struct { Configuration common.Configuration Records map[string]interface{} @@ -26,6 +27,7 @@ type InsertApi struct { var insertTag = "Insert" +// Interface for performing HTTP requests. type HTTPClient interface { Do(req *http.Request) (*http.Response, error) } @@ -105,6 +107,7 @@ func (insertApi *InsertApi) doValidations() *errors.SkyflowError { return nil } +// Internal func (insertApi *InsertApi) Post(token string) (common.ResponseBody, *errors.SkyflowError) { err := insertApi.doValidations() if err != nil { diff --git a/skyflow/vaultapi/invoke_connection.go b/skyflow/vaultapi/invoke_connection.go index f25828da..51846117 100644 --- a/skyflow/vaultapi/invoke_connection.go +++ b/skyflow/vaultapi/invoke_connection.go @@ -20,6 +20,7 @@ import ( "github.com/skyflowapi/skyflow-go/skyflow/common" ) +// Wrapper for the parameters that are required to invoke connections. type InvokeConnectionApi struct { ConnectionConfig common.ConnectionConfig Token string @@ -41,6 +42,7 @@ func (InvokeConnectionApi *InvokeConnectionApi) doValidations() *errors.SkyflowE return nil } +// Internal func (InvokeConnectionApi *InvokeConnectionApi) Post() (map[string]interface{}, *errors.SkyflowError) { validationError := InvokeConnectionApi.doValidations()