Skip to content

Commit

Permalink
central config parser
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelemusiani committed Jun 3, 2024
1 parent 36861c5 commit cf312eb
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 148 deletions.
9 changes: 5 additions & 4 deletions bot/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
tgbotapi "github.com/samuelemusiani/telegram-bot-api"
"golang.org/x/exp/slices"

"github.com/csunibo/config-parser-go"
"github.com/csunibo/informabot/model"
"github.com/csunibo/informabot/utils"
)
Expand Down Expand Up @@ -124,7 +125,7 @@ func buildEmails(emails []string) string {
return strings.Join(emails, DOMAIN) + DOMAIN
}

func teachingToString(teaching model.Teaching) string {
func teachingToString(teaching cparser.Teaching) string {
var b strings.Builder
if teaching.Name != "" {
b.WriteString(fmt.Sprintf("<b>%s</b>\n", teaching.Name))
Expand Down Expand Up @@ -159,15 +160,15 @@ func handleTeaching(bot *tgbotapi.BotAPI, update *tgbotapi.Update, teachingName
return true
}

func degreeToTeaching(degree model.Degree) model.Teaching {
return model.Teaching{
func degreeToTeaching(degree cparser.Degree) cparser.Teaching {
return cparser.Teaching{
Name: degree.Name,
Url: degree.Id,
Chat: degree.Chat,
}
}

func degreeToString(degree model.Degree) string {
func degreeToString(degree cparser.Degree) string {
if len(degree.Years) == 0 {
return teachingToString(degreeToTeaching(degree))
}
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
module github.com/csunibo/informabot

go 1.21
go 1.22.3

require (
github.com/csunibo/config-parser-go v0.0.2
github.com/csunibo/unibo-go v0.0.10
github.com/mitchellh/mapstructure v1.5.0
github.com/samuelemusiani/telegram-bot-api v0.0.6
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/csunibo/config-parser-go v0.0.2 h1:BNujualQX9mZdCzKsEmWlzvTinLqDC0T3dkgdZ/mCTY=
github.com/csunibo/config-parser-go v0.0.2/go.mod h1:guoYwHikybA1kw6MFmCr+HCMZEFxGaO0cr+ACxD0dvY=
github.com/csunibo/unibo-go v0.0.10 h1:UwFIvTpXMKZCOVoX564RmqQttXTTs5Xz5hCAjun8NFk=
github.com/csunibo/unibo-go v0.0.10/go.mod h1:h2+xnccHa7x48RNB6d07bpHQ01ozw4oihgDOlvVrJ9U=
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
Expand Down
11 changes: 6 additions & 5 deletions model/globals.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,21 @@
package model

import (
"github.com/csunibo/config-parser-go"
"log"
)

var (
Autoreplies []AutoReply
Actions []Action
Degrees map[string]Degree
Degrees map[string]cparser.Degree
MemeList []Meme
Settings SettingsStruct
Teachings map[string]Teaching
Teachings map[string]cparser.Teaching
Groups GroupsStruct
Timetables map[string]Timetable
Maintainers []Maintainer
Representatives map[string]Representative
Timetables map[string]cparser.Timetable
Maintainers []cparser.Maintainer
Representatives map[string]cparser.Representative
)

func InitGlobals() {
Expand Down
53 changes: 0 additions & 53 deletions model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,59 +71,6 @@ type Action struct {
Data DataInterface `json:"data"`
}

type Maintainer struct {
Id int64 `json:"id"`
Username string `json:"username"`
}

// config/teachings.json

type Teaching struct {
Name string `json:"name"`
Url string `json:"url"`
Chat string `json:"chat"`
Website string `json:"website"`
Professors []string `json:"professors"`
}

// config/degrees.json

type YearStudyDiagram struct {
Mandatory []string `json:"mandatory"`
Electives []string `json:"electives"`
}

type Year struct {
Year int64 `json:"year"`
Chat string `json:"chat"`
Teachings YearStudyDiagram `json:"teachings"`
}

type Degree struct {
Id string `json:"id"`
Name string `json:"name"`
Icon string `json:"icon"`
Years []Year `json:"years"`
Chat string `json:"chat"`
}

// timetables.json

type Curriculum struct {
Name string `json:"name"`
Callback string `json:"callback"`
}

// Recognized by a callback string
type Timetable struct {
Course string `json:"course"` // Course title
Name string `json:"name"` // Course name
Type string `json:"type"` // Type (laurea|magistrale|2cycle)
Curriculum string `json:"curricula"` // Curriculum
Title string `json:"title"`
FallbackText string `json:"fallbackText"`
}

// SECTION ACTION STRUCTS DATA
type MessageData struct {
Text string `json:"text"`
Expand Down
103 changes: 19 additions & 84 deletions model/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ import (
"fmt"
"io"
"os"
"path"
"path/filepath"
"strings"

"github.com/mitchellh/mapstructure"
"golang.org/x/exp/slices"

"github.com/csunibo/config-parser-go"
"github.com/csunibo/informabot/utils"
)

Expand Down Expand Up @@ -50,11 +52,11 @@ func commandNameFromString(s string) string {
return s
}

func commandNameFromTeaching(t Teaching) string {
func commandNameFromTeaching(t cparser.Teaching) string {
return commandNameFromString(t.Url)
}

func commandNameFromDegree(d Degree) string {
func commandNameFromDegree(d cparser.Degree) string {
return commandNameFromString(d.Id)
}

Expand All @@ -64,37 +66,23 @@ func commandNamesFromStrings(strings []string) {
}
}

func ParseTeachings() (teachings map[string]Teaching, err error) {
filepath := filepath.Join(jsonPath, configSubpath, teachingsFile)
file, err := os.Open(filepath)
defer file.Close()
if err != nil {
return nil, fmt.Errorf("error reading %s file: %w", filepath, err)
}
func ParseTeachings() (teachings map[string]cparser.Teaching, err error) {

var teachingsArray []Teaching
err = json.NewDecoder(file).Decode(&teachingsArray)
teachingsArray, err := cparser.ParseTeachings(filepath.Join(jsonPath, configSubpath))
if err != nil {
return nil, fmt.Errorf("error parsing %s file: %w", filepath, err)
return nil, fmt.Errorf("error parsing Teachings: %w", err)
}
teachings = make(map[string]Teaching, len(teachingsArray))
teachings = make(map[string]cparser.Teaching, len(teachingsArray))
for _, t := range teachingsArray {
teachings[commandNameFromTeaching(t)] = t
}
return
}

func ParseDegrees() (degrees map[string]Degree, err error) {
filepath := filepath.Join(jsonPath, configSubpath, degreesFile)
file, err := os.Open(filepath)
defer file.Close()
func ParseDegrees() (degrees map[string]cparser.Degree, err error) {
degreesArray, err := cparser.ParseDegrees(filepath.Join(jsonPath, configSubpath))
if err != nil {
return nil, fmt.Errorf("error reading %s file: %w", degreesFile, err)
}
var degreesArray []Degree
err = json.NewDecoder(file).Decode(&degreesArray)
if err != nil {
return nil, fmt.Errorf("error parsing %s file: %w", degreesFile, err)
return nil, fmt.Errorf("error parsing Degrees: %w", err)
}
for _, d := range degreesArray {
for _, y := range d.Years {
Expand All @@ -103,7 +91,7 @@ func ParseDegrees() (degrees map[string]Degree, err error) {
commandNamesFromStrings(t.Electives)
}
}
degrees = make(map[string]Degree, len(degreesArray))
degrees = make(map[string]cparser.Degree, len(degreesArray))
for _, d := range degreesArray {
degrees[commandNameFromDegree(d)] = d
}
Expand Down Expand Up @@ -213,72 +201,19 @@ func SaveGroups(groups GroupsStruct) error {
return utils.WriteJSONFile(filepath, groups)
}

func ParseTimetables() (timetables map[string]Timetable, err error) {
filepath := filepath.Join(jsonPath, configSubpath, timetablesFile)
file, err := os.Open(filepath)
defer file.Close()
if err != nil {
return nil, fmt.Errorf("error reading %s file: %w", timetablesFile, err)
}

var mapData map[string]Timetable
func ParseTimetables() (timetables map[string]cparser.Timetable, err error) {
timetables, err = cparser.ParseTimetables(filepath.Join(jsonPath, configSubpath))

err = json.NewDecoder(file).Decode(&mapData)
if err != nil {
return nil, fmt.Errorf("error parsing %s file: %w", filepath, err)
return nil, fmt.Errorf("error parsing Timetables: %w", err)
}

timetables = mapData
return
}

func ParseMaintainers() (maintainer []Maintainer, err error) {
filepath := filepath.Join(jsonPath, configSubpath, maintainersFile)
file, err := os.ReadFile(filepath)
if errors.Is(err, os.ErrNotExist) {
return maintainer, fmt.Errorf("%s does not exist", maintainersFile)
} else if err != nil {
return nil, fmt.Errorf("error reading %s file: %w", maintainersFile, err)
}

var projects []struct {
Name string `json:"project"`
Maintainers []Maintainer `json:"maintainers"`
}

err = json.Unmarshal(file, &projects)
if err != nil {
return nil, fmt.Errorf("error parsing %s file: %w", maintainersFile, err)
}

for _, p := range projects {
if p.Name == "informabot" {
return p.Maintainers, nil
}
}

return nil, fmt.Errorf("couldn't found informabot projects after parsing %s", maintainersFile)
func ParseMaintainers() (maintainer []cparser.Maintainer, err error) {
return cparser.ParseMaintainers(path.Join(jsonPath, configSubpath))
}

func ParseRepresentatives() (map[string]Representative, error) {
representatives := make(map[string]Representative)

filepath := filepath.Join(jsonPath, configSubpath, representativesFile)
byteValue, err := os.ReadFile(filepath)
if errors.Is(err, os.ErrNotExist) {
return representatives, nil
} else if err != nil {
return nil, fmt.Errorf("error reading %s file: %w", filepath, err)
}

err = json.Unmarshal(byteValue, &representatives)
if err != nil {
return nil, fmt.Errorf("error parsing %s file: %w", filepath, err)
}

if representatives == nil {
representatives = make(map[string]Representative)
}

return representatives, nil
func ParseRepresentatives() (map[string]cparser.Representative, error) {
return cparser.ParseRepresentatives(path.Join(jsonPath, configSubpath))
}
3 changes: 2 additions & 1 deletion model/timetables.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ import (
"strings"
"time"

"github.com/csunibo/config-parser-go"
tgbotapi "github.com/samuelemusiani/telegram-bot-api"
)

type InlineKeyboardRows [][]tgbotapi.InlineKeyboardButton

// Returns a group of button rows for a selected groups on `timetables`
func GetTimetableCoursesRows(timetables *map[string]Timetable) InlineKeyboardRows {
func GetTimetableCoursesRows(timetables *map[string]cparser.Timetable) InlineKeyboardRows {
rows := make([][]tgbotapi.InlineKeyboardButton, len(*timetables))

keys := make([]string, 0)
Expand Down

0 comments on commit cf312eb

Please sign in to comment.