Skip to content

Commit

Permalink
actions: create types and begin to parse
Browse files Browse the repository at this point in the history
this is still not completed, actions should use #72 format
  • Loading branch information
Flecart committed Mar 12, 2023
1 parent f72ddb4 commit 30161b8
Show file tree
Hide file tree
Showing 2 changed files with 166 additions and 0 deletions.
98 changes: 98 additions & 0 deletions actions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package main

// TODO: capire come implementare funzioni di questo tipo
func GetActionFromType(commandType string) Action[T] {
switch commandType {
case "message":
return Action[MessageData]()
case "help":
return Action[HelpData]()
case "aggiorna":
return Action[AggiornaData]()
case "lookingFor":
return Action[LookingForData]()
case "notLookingFor":
return Action[NotLookingForData]()
case "yearly":
return Action[YearlyData]()
case "todayLecture":
return Action[TodayLectureData]()
case "tomorrowLecture":
return Action[TomorrowLectureData]()
case "scelta":
return Action[SceltaData]()
case "course":
return Action[CourseData]()
default:
return Action[MessageData]()
}
}

type Action[T any] struct {
Name string
Type string `json:"type"`
Data T
}

type MessageData struct {
Text string `json:"text"`
}

type HelpData struct {
Description string `json:"description"`
}

type AggiornaData struct {
Description string `json:"description"`
NoYear string `json:"noYear"`
NoMod string `json:"noMod"`
Started string `json:"started"`
Ended string `json:"ended"`
Failed string `json:"failed"`
}

type LookingForData struct {
Description string `json:"description"`
SingularText string `json:"singularText"`
PluralText string `json:"pluralText"`
ChatError string `json:"chatError"`
}

type NotLookingForData struct {
Description string `json:"description"`
Text string `json:"text"`
ChatError string `json:"chatError"`
NotFoundError string `json:"notFoundError"`
}

type YearlyData struct {
Description string `json:"description"`
Command string `json:"command"`
NoYear string `json:"noYear"`
}

type TodayLectureData struct {
Description string `json:"description"`
Url string `json:"url"`
Title string `json:"title"`
FallbackText string `json:"fallbackText"`
}

type TomorrowLectureData TodayLectureData

type SceltaData struct {
Description string `json:"description"`
Header string `json:"header"`
Template string `json:"template"`
Items [][]string `json:"items"`
}

type CourseData struct {
Name string `json:"name"`
Description string `json:"description"`
Virtuale string `json:"virtuale"`
Teams string `json:"teams"`
Website string `json:"website"`
Professors []string `json:"professors"`
TelegramLink string `json:"telegram"`
}
68 changes: 68 additions & 0 deletions jsonparse.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,71 @@ func ParseAutoReplies() (AutoReply, error) {

return autoreplies, nil
}

func ParseActions() error {
jsonFile, err := os.Open("./json/actions.json")
if err != nil {
fmt.Println(err)
return err
}
defer jsonFile.Close()

byteValue, _ := ioutil.ReadAll(jsonFile)

var JSON map[string]interface{}
json.Unmarshal(byteValue, &JSON)
// TODO: finire il parsing delle azioni
// fmt.Println(JSON)

for key, value := range JSON {
fmt.Println(key)
fmt.Println(value)
fmt.Printf("%T", value)
break
}
// neededOutput := jsonToMap(JSON)
// fmt.Println(neededOutput)

return nil
}

// JSON TO MAP
// From https://stackoverflow.com/questions/70937676/extract-all-json-keys-and-values-as-map
func jsonToMap(data map[string]interface{}) map[string][]string {
// final output
out := make(map[string][]string)

// check all keys in data
for key, value := range data {
// check if key not exist in out variable, add it
if _, ok := out[key]; !ok {
out[key] = []string{}
}

if valueA, ok := value.(map[string]interface{}); ok { // if value is map
out[key] = append(out[key], "")
for keyB, valueB := range jsonToMap(valueA) {
if _, ok := out[keyB]; !ok {
out[keyB] = []string{}
}
out[keyB] = append(out[keyB], valueB...)
}
} else if valueA, ok := value.([]interface{}); ok { // if value is array
for _, valueB := range valueA {
if valueC, ok := valueB.(map[string]interface{}); ok {
for keyD, valueD := range jsonToMap(valueC) {
if _, ok := out[keyD]; !ok {
out[keyD] = []string{}
}
out[keyD] = append(out[keyD], valueD...)
}
} else {
out[key] = append(out[key], fmt.Sprintf("%v", valueB))
}
}
} else { // if string and numbers and other ...
out[key] = append(out[key], fmt.Sprintf("%v", value))
}
}
return out
}

0 comments on commit 30161b8

Please sign in to comment.