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

Struttura alternativa del JSON actions #72

Closed
Flecart opened this issue Mar 12, 2023 · 2 comments · Fixed by #76
Closed

Struttura alternativa del JSON actions #72

Flecart opened this issue Mar 12, 2023 · 2 comments · Fixed by #76
Assignees
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@Flecart
Copy link
Member

Flecart commented Mar 12, 2023

Introduzione

La struttura actions all'interno di Informabot è la struttura principale da cui il bot va a ritorvare le informazioni necessarie per rispondere in modo appropriato. Attualmente possiede una struttura molto piatta:

  1. Definizione nome-comando
  2. definizione del tipo di comando
  3. Dati variabili, allo stesso livello del tipo di comando.

Richiesta:

La questione che vorrei sollevare è mettere tutti i campi dati, che sono variabili e differenti a seconda della tipologia di comando, in un suo livello di indentazione. Questo faciliterebbe molto la sezione di parsing del JSON (andrebbe a guardare il tipo, e poi parserebbe a seconda del tipo), e renderebbe anche molto più chiara l'interfaccia di act.

Esempi

Attualmente abbiamo cose come

  "start": {
    "type": "message",
    "text": "..."
  },
  "cercogruppo": {
    "type": "lookingFor",
    "description": "Cerca un gruppo di progetto",
    "singularText": "Una persona cerca un gruppo in <b>\"{0}\"</b>:\n",
    "pluralText": "<b>{1}</b> persone cercano gruppi in <b>\"{0}\"</b>:\n",
    "chatError": "Questo comando è riservato alle chat dei gruppi degli insegnamenti."
  },

Secondo me sarebbe più chiaro avere una forma di questo tipo

  "start": {
    "type": "message",
    "data": {
        "text": "...."
     }
  },
  "cercogruppo": {
    "type": "lookingFor",
    "data": {
        "description": "Cerca un gruppo di progetto",
        "singularText": "Una persona cerca un gruppo in <b>\"{0}\"</b>:\n",
        "pluralText": "<b>{1}</b> persone cercano gruppi in <b>\"{0}\"</b>:\n",
        "chatError": "Questo comando è riservato alle chat dei gruppi degli insegnamenti."
    }
  },
@Flecart
Copy link
Member Author

Flecart commented Mar 12, 2023

Script

Uno script in python per eseguire l'operazione sopra descritta:

import json

with open('json/actions.json') as f:
    data = json.load(f)
  
result_json = dict()

for keyword in data:
    command = data[keyword]

    new_data_field = dict()

    to_delete = []
    for field in command:
        if field != 'type':
            new_data_field[field] = command[field]
            to_delete.append(field)
    
    for field in to_delete:
        del command[field]
    
    command['data'] = new_data_field
    result_json[keyword] = command

with open("result.json", "w") as f:
    json.dump(result_json, f, indent=2)

@Flecart Flecart added enhancement New feature or request help wanted Extra attention is needed labels Mar 12, 2023
@foxyseta
Copy link
Member

Al momento @crestaa sta riscrivendo in Go, in cui il parsing viene effettuando facendo marshaling direttamente nel tipo di struct desiderato. Quindi il modo non-piatto in cui strutturare i json sarà lo stesso in cui lui definisce i tipi di struct in Go.
Si potrebbero usare i generici, dove Data è il tipo della struct innestata che contiene i campi variabili.

Flecart added a commit that referenced this issue Mar 12, 2023
this is still not completed, actions should use #72 format
@Flecart Flecart linked a pull request Mar 12, 2023 that will close this issue
@Flecart Flecart closed this as completed Mar 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
No open projects
Status: Done
Development

Successfully merging a pull request may close this issue.

3 participants