Pass error to svelte using a struct
...a very cursed struct
This commit is contained in:
		
							
								
								
									
										8
									
								
								app.go
									
									
									
									
									
								
							
							
						
						
									
										8
									
								
								app.go
									
									
									
									
									
								
							@@ -20,6 +20,10 @@ func (a *App) startup(ctx context.Context) {
 | 
				
			|||||||
	a.ctx = ctx
 | 
						a.ctx = ctx
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (a *App) GetFood() ([]Food, error) {
 | 
					func (a *App) GetFood() WailsFood {
 | 
				
			||||||
	return foodService.GetRecent()
 | 
						data, err := foodService.GetRecent()
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return WailsFood{Success: false, Error: err.Error()}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return WailsFood{Data: data, Success: true}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@@ -1,9 +1,6 @@
 | 
				
			|||||||
<script lang="ts">
 | 
					<script lang="ts">
 | 
				
			||||||
	import Header from "$lib/components/Header.svelte";
 | 
						import Header from "$lib/components/Header.svelte";
 | 
				
			||||||
	import Router from "$lib/router/Router.svelte";
 | 
						import Router from "$lib/router/Router.svelte";
 | 
				
			||||||
	import { GetFood } from "../wailsjs/go/main/App.js";
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	GetFood().then((result) => console.log(result));
 | 
					 | 
				
			||||||
</script>
 | 
					</script>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
<template>
 | 
					<template>
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -0,0 +1,20 @@
 | 
				
			|||||||
 | 
					<script lang="ts">
 | 
				
			||||||
 | 
						import { GetFood } from "$wails/main/App.js";
 | 
				
			||||||
 | 
						import { main } from '$wails/models'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						let food: main.Food[] = [];
 | 
				
			||||||
 | 
						GetFood().then((result) => {
 | 
				
			||||||
 | 
							if (!result.success) {
 | 
				
			||||||
 | 
								console.error(result.error);
 | 
				
			||||||
 | 
								return;
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							food = result.data
 | 
				
			||||||
 | 
							console.log(food);
 | 
				
			||||||
 | 
						});
 | 
				
			||||||
 | 
					</script>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					<template>
 | 
				
			||||||
 | 
						{#each food as f}
 | 
				
			||||||
 | 
							<div>{f.food}</div>
 | 
				
			||||||
 | 
						{/each}
 | 
				
			||||||
 | 
					</template>
 | 
				
			||||||
							
								
								
									
										2
									
								
								frontend/wailsjs/go/main/App.d.ts
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										2
									
								
								frontend/wailsjs/go/main/App.d.ts
									
									
									
									
										vendored
									
									
								
							@@ -2,4 +2,4 @@
 | 
				
			|||||||
// This file is automatically generated. DO NOT EDIT
 | 
					// This file is automatically generated. DO NOT EDIT
 | 
				
			||||||
import {main} from '../models';
 | 
					import {main} from '../models';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export function GetFood():Promise<Array<main.Food>>;
 | 
					export function GetFood():Promise<main.WailsFood>;
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,7 +1,13 @@
 | 
				
			|||||||
export namespace main {
 | 
					export namespace main {
 | 
				
			||||||
	
 | 
						
 | 
				
			||||||
	export class Food {
 | 
						export class Food {
 | 
				
			||||||
	
 | 
						    rowid: number;
 | 
				
			||||||
 | 
						    date: string;
 | 
				
			||||||
 | 
						    food: string;
 | 
				
			||||||
 | 
						    description: string;
 | 
				
			||||||
 | 
						    amount: number;
 | 
				
			||||||
 | 
						    per100: number;
 | 
				
			||||||
 | 
						    energy: number;
 | 
				
			||||||
	
 | 
						
 | 
				
			||||||
	    static createFrom(source: any = {}) {
 | 
						    static createFrom(source: any = {}) {
 | 
				
			||||||
	        return new Food(source);
 | 
						        return new Food(source);
 | 
				
			||||||
@@ -9,9 +15,49 @@ export namespace main {
 | 
				
			|||||||
	
 | 
						
 | 
				
			||||||
	    constructor(source: any = {}) {
 | 
						    constructor(source: any = {}) {
 | 
				
			||||||
	        if ('string' === typeof source) source = JSON.parse(source);
 | 
						        if ('string' === typeof source) source = JSON.parse(source);
 | 
				
			||||||
	
 | 
						        this.rowid = source["rowid"];
 | 
				
			||||||
 | 
						        this.date = source["date"];
 | 
				
			||||||
 | 
						        this.food = source["food"];
 | 
				
			||||||
 | 
						        this.description = source["description"];
 | 
				
			||||||
 | 
						        this.amount = source["amount"];
 | 
				
			||||||
 | 
						        this.per100 = source["per100"];
 | 
				
			||||||
 | 
						        this.energy = source["energy"];
 | 
				
			||||||
	    }
 | 
						    }
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
						export class WailsFood {
 | 
				
			||||||
 | 
						    data: Food[];
 | 
				
			||||||
 | 
						    success: boolean;
 | 
				
			||||||
 | 
						    error?: string;
 | 
				
			||||||
 | 
						
 | 
				
			||||||
 | 
						    static createFrom(source: any = {}) {
 | 
				
			||||||
 | 
						        return new WailsFood(source);
 | 
				
			||||||
 | 
						    }
 | 
				
			||||||
 | 
						
 | 
				
			||||||
 | 
						    constructor(source: any = {}) {
 | 
				
			||||||
 | 
						        if ('string' === typeof source) source = JSON.parse(source);
 | 
				
			||||||
 | 
						        this.data = this.convertValues(source["data"], Food);
 | 
				
			||||||
 | 
						        this.success = source["success"];
 | 
				
			||||||
 | 
						        this.error = source["error"];
 | 
				
			||||||
 | 
						    }
 | 
				
			||||||
 | 
						
 | 
				
			||||||
 | 
							convertValues(a: any, classs: any, asMap: boolean = false): any {
 | 
				
			||||||
 | 
							    if (!a) {
 | 
				
			||||||
 | 
							        return a;
 | 
				
			||||||
 | 
							    }
 | 
				
			||||||
 | 
							    if (a.slice && a.map) {
 | 
				
			||||||
 | 
							        return (a as any[]).map(elem => this.convertValues(elem, classs));
 | 
				
			||||||
 | 
							    } else if ("object" === typeof a) {
 | 
				
			||||||
 | 
							        if (asMap) {
 | 
				
			||||||
 | 
							            for (const key of Object.keys(a)) {
 | 
				
			||||||
 | 
							                a[key] = new classs(a[key]);
 | 
				
			||||||
 | 
							            }
 | 
				
			||||||
 | 
							            return a;
 | 
				
			||||||
 | 
							        }
 | 
				
			||||||
 | 
							        return new classs(a);
 | 
				
			||||||
 | 
							    }
 | 
				
			||||||
 | 
							    return a;
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										16
									
								
								types.go
									
									
									
									
									
								
							
							
						
						
									
										16
									
								
								types.go
									
									
									
									
									
								
							@@ -1,16 +0,0 @@
 | 
				
			|||||||
package main
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
import "time"
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
type (
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	Weight struct {
 | 
					 | 
				
			||||||
		rowid  int
 | 
					 | 
				
			||||||
		date   time.Time
 | 
					 | 
				
			||||||
		weight float32
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	AggregatedWeight struct {
 | 
					 | 
				
			||||||
		period string
 | 
					 | 
				
			||||||
		amount float32
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
)
 | 
					 | 
				
			||||||
							
								
								
									
										33
									
								
								wailstypes.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										33
									
								
								wailstypes.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,33 @@
 | 
				
			|||||||
 | 
					package main
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import "time"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type (
 | 
				
			||||||
 | 
						// Wails doesn't rlike generics... Fuck...
 | 
				
			||||||
 | 
						// WailsReturn[T interface{}] struct {
 | 
				
			||||||
 | 
						// 	Data    T      `json:'data'`
 | 
				
			||||||
 | 
						// 	Success bool   `json:'success'`
 | 
				
			||||||
 | 
						// 	Error   string `json:'error'`
 | 
				
			||||||
 | 
						// }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						WailsFood struct {
 | 
				
			||||||
 | 
							Data    []Food `json:"data"`
 | 
				
			||||||
 | 
							Success bool   `json:"success"`
 | 
				
			||||||
 | 
							Error   string `json:"error,omitempty"`
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						WailsAggregateFood struct {
 | 
				
			||||||
 | 
							Data    []AggregatedFood `json:"data"`
 | 
				
			||||||
 | 
							Success bool             `json:"success"`
 | 
				
			||||||
 | 
							Error   string           `json:"error,omitempty"`
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						Weight struct {
 | 
				
			||||||
 | 
							rowid  int
 | 
				
			||||||
 | 
							date   time.Time
 | 
				
			||||||
 | 
							weight float32
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						AggregatedWeight struct {
 | 
				
			||||||
 | 
							period string
 | 
				
			||||||
 | 
							amount float32
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
		Reference in New Issue
	
	Block a user