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

Data parser [DNM] #47

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions pkg/data_parser/data_parser.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you 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.

// This is a reimplementation of the amazing work done by Mathew Topol.
// Thanks to his work, reading parquet files was made incredibly easy.

package parseador

import (
"bufio"
"encoding/json"
"os"
"path/filepath"
"strings"

"github.com/sirupsen/logrus"
)

// ParseLines reads the contents of a file and returns the lines that contain a certain element
func ParseLines(file string, element string) ([]string, error) {
var lines []string

// Open the file
f, err := os.Open(filepath.Clean(file))
if err != nil {
return lines, err
}
defer func() {
if err := f.Close(); err != nil {
logrus.Printf("Error closing file: %s\n", err)
}
}()

// Read the file line by line
scanner := bufio.NewScanner(f)
for scanner.Scan() {
line := scanner.Text()

// Check if the line contains the element
if strings.Contains(line, element) {
if !strings.HasPrefix(line, "#") {
lines = append(lines, line)
}
}
}

if err := scanner.Err(); err != nil {
return lines, err
}

return lines, nil
}

func OutputData(file string, data []string) error {
// Marshal the data into JSON
bytes, err := json.Marshal(data)
if err != nil {
return err
}

// Write the JSON to the file
err = os.WriteFile(file, bytes, 0600)
if err != nil {
return err
}

return nil
}
64 changes: 64 additions & 0 deletions pkg/data_parser/parseador.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package parseador

import (
"encoding/json"
"fmt"
"log"
"os"
"path/filepath"
"strings"

"github.com/sirupsen/logrus"
)

type Config struct {
Connection string `json:"connection"`
Enabled_Backends []string `json:"enabled_backends"`
NumberOfBackends string `json:"numberofbackends"`
}

func Parseador(file string) {
// Read the contents of the configuration file into a byte slice
data, err := os.ReadFile(filepath.Clean(file))
if err != nil {
logrus.Fatal("Can't read file", err)
}

// Split the byte slice into a slice of strings, with each string representing a line in the file
lines := strings.Split(string(data), "\n")
logrus.Info("Lines split in slice of strings")
logrus.Info("Number of lines: ", len(lines))

// Initialize an empty Config struct
var config Config

// Iterate through the lines in the file
for _, line := range lines {
//logrus.Info("Untreated lines: ", lines)

// Skip empty lines
if strings.HasPrefix(line, "#") || !strings.Contains(line, "=") {
continue
}
// Split the line into a key-value pair
parts := strings.Split(line, "=")
logrus.Info(line, " --- ", "output: ", parts)
key := strings.TrimSpace(parts[0])
value := strings.TrimSpace(parts[1])

// Set the appropriate field in the Config struct based on the key
var x int
switch key {
case "enabled_backends":
config.Enabled_Backends = append(config.Enabled_Backends, value)
config.NumberOfBackends = fmt.Sprint(x + 1)
}
}

// Marshal the Config struct into a JSON byte slice
configJSON, err := json.MarshalIndent(config, "", " ")
if err != nil {
log.Fatal(err)
}
fmt.Println(string(configJSON))
}