github.com/futurehomeno/fimpgo@v1.14.0/edgeapp/manifest.go (about)

     1  package edgeapp
     2  
     3  import (
     4  	"encoding/json"
     5  	"github.com/futurehomeno/fimpgo/fimptype"
     6  	log "github.com/sirupsen/logrus"
     7  	"io/ioutil"
     8  )
     9  
    10  type Manifest struct {
    11  	Configs     []AppConfig  `json:"configs"`
    12  	UIBlocks    []AppUBLock  `json:"ui_blocks"`
    13  	UIButtons   []UIButton   `json:"ui_buttons"`
    14  	Auth        AppAuth      `json:"auth"`
    15  	InitFlow    []string     `json:"init_flow"`
    16  	Services    []AppService `json:"services"`
    17  	AppState    AppStates    `json:"app_state"`
    18  	ConfigState interface{}  `json:"config_state"`
    19  }
    20  
    21  type AppConfig struct {
    22  	ID          string            `json:"id"`
    23  	Label       MultilingualLabel `json:"label"`
    24  	ValT        string            `json:"val_t"`
    25  	UI          AppConfigUI       `json:"ui"`
    26  	Val         Value             `json:"val"`
    27  	IsRequired  bool              `json:"is_required"`
    28  	ConfigPoint string            `json:"config_point"`
    29  	Hidden      bool              `json:"hidden"` //
    30  }
    31  
    32  func (b *AppConfig) Hide() {
    33  	b.Hidden = true
    34  }
    35  
    36  func (b *AppConfig) Show() {
    37  	b.Hidden = true
    38  }
    39  
    40  type MultilingualLabel map[string]string
    41  
    42  type AppAuth struct {
    43  	Type                  string `json:"type"`
    44  	CodeGrantLoginPageUrl string `json:"code_grant_login_page_url"`
    45  	RedirectURL           string `json:"redirect_url"`
    46  	ClientID              string `json:"client_id"`
    47  	Secret                string `json:"secret"`
    48  	PartnerID             string `json:"partner_id"`
    49  	AuthEndpoint          string `json:"auth_endpoint"`
    50  }
    51  
    52  type AppService struct {
    53  	Name       string               `json:"name"`
    54  	Alias      string               `json:"alias"`
    55  	Address    string               `json:"address"`
    56  	Interfaces []fimptype.Interface `json:"interfaces"`
    57  }
    58  
    59  type Value struct {
    60  	Default interface{} `json:"default"`
    61  }
    62  
    63  type AppConfigUI struct {
    64  	Type   string      `json:"type"`
    65  	Select interface{} `json:"select"`
    66  }
    67  
    68  type UIButton struct {
    69  	ID    string            `json:"id"`
    70  	Label MultilingualLabel `json:"label"`
    71  	Req   struct {
    72  		Serv  string `json:"serv"`
    73  		IntfT string `json:"intf_t"`
    74  		Val   string `json:"val"`
    75  	} `json:"req"`
    76  	Hidden       bool `json:"hidden"`
    77  }
    78  
    79  func (b *UIButton) Hide() {
    80  	b.Hidden = true
    81  }
    82  
    83  func (b *UIButton) Show() {
    84  	b.Hidden = true
    85  }
    86  
    87  type ButtonActionResponse struct {
    88  	Operation       string `json:"op"`
    89  	OperationStatus string `json:"op_status"`
    90  	Next            string `json:"next"`
    91  	ErrorCode       string `json:"error_code"`
    92  	ErrorText       string `json:"error_text"`
    93  }
    94  
    95  type AppUBLock struct {
    96  	ID      string            `json:"id"`
    97  	Header  MultilingualLabel `json:"header"`
    98  	Text    MultilingualLabel `json:"text"`
    99  	Configs []string          `json:"configs"`
   100  	Buttons []string          `json:"buttons"`
   101  	Footer  MultilingualLabel `json:"footer"`
   102  	Hidden  bool              `json:"hidden"`
   103  }
   104  
   105  func (b *AppUBLock) Hide() {
   106  	b.Hidden = true
   107  }
   108  
   109  func (b *AppUBLock) Show() {
   110  	b.Hidden = true
   111  }
   112  
   113  
   114  func NewManifest() *Manifest {
   115  	return &Manifest{}
   116  }
   117  
   118  func (m *Manifest) LoadFromFile(filePath string) error {
   119  	log.Debug("<manifest> Loading flow from file : ", filePath)
   120  	file, err := ioutil.ReadFile(filePath)
   121  	if err != nil {
   122  		log.Error("<manifest> Can't open manifest file.")
   123  		return err
   124  	}
   125  	err = json.Unmarshal(file, m)
   126  	if err != nil {
   127  		log.Error("<FlMan> Can't unmarshal manifest file.")
   128  		return err
   129  	}
   130  	return nil
   131  }
   132  
   133  func (m *Manifest) SaveToFile(filePath string) error {
   134  	flowMetaByte, err := json.Marshal(m)
   135  	if err != nil {
   136  		log.Error("<manifest> Can't marshal imported file ")
   137  		return err
   138  	}
   139  	log.Debugf("<manifest> Saving manifest to file %s :", filePath)
   140  	err = ioutil.WriteFile(filePath, flowMetaByte, 0644)
   141  	if err != nil {
   142  		log.Error("<manifest>Can't save flow to file . Error : ", err)
   143  		return err
   144  	}
   145  	return nil
   146  }
   147  
   148  func (m *Manifest) GetUIBlock(id string)*AppUBLock {
   149  	for i:=range m.UIBlocks {
   150  		if m.UIBlocks[i].ID == id {
   151  			return &m.UIBlocks[i]
   152  		}
   153  	}
   154  	return nil
   155  }
   156  
   157  func (m *Manifest) GetButton(id string)*UIButton {
   158  	for i:=range m.UIButtons {
   159  		if m.UIButtons[i].ID == id {
   160  			return &m.UIButtons[i]
   161  		}
   162  	}
   163  	return nil
   164  }
   165  
   166  func (m *Manifest) GetAppConfig(id string)*AppConfig {
   167  	for i:=range m.Configs {
   168  		if m.Configs[i].ID == id {
   169  			return &m.Configs[i]
   170  		}
   171  	}
   172  	return nil
   173  }
   174  
   175  type AuthResponse struct {
   176  	Status string `json:"status"`
   177  	ErrorText string `json:"error_text"`
   178  	ErrorCode string `json:"error_code"`
   179  }