Re: vibed how do I convert form values into a struct automatically?

2021-11-10 Thread Chris Bare via Digitalmars-d-learn

On Wednesday, 10 November 2021 at 19:23:44 UTC, Chris Bare wrote:

What am I missing?


I figured out part of it. In the html input element the name has 
to be employeeform.first_name.
but now I get a different error: Error handling field 
'employeeform.id': Missing form field.


I tried declaring the id as @optional, but that did not make a 
difference.

```d
struct Employee
{
@optional long id;
string first_name;
string last_name;
}
```
Is there another way to indicate that a field does not have to be 
in the form?


vibed how do I convert form values into a struct automatically?

2021-11-10 Thread Chris Bare via Digitalmars-d-learn
I know I can do this manually by looking at HTTPServerRequest 
req, but the documentation for registerWebInterface sounds like 
there's an automatic way to do this.

```

required/>
required/>


```
```d
struct Employee
{
long id;
string first_name;
@string last_name;
}
```
in my web interface service I tried:
```d
void postEmployeeform (Employee employeeform)
```
but that won't compile. The following compiles and works:
```d
void postEmployeeform (string first_name, string last_name)

```
What am I missing?


Re: in a template, how can I get the parameter of a user defined attribute

2021-11-06 Thread Chris Bare via Digitalmars-d-learn

On Saturday, 6 November 2021 at 19:51:09 UTC, Adam Ruppe wrote:

On Saturday, 6 November 2021 at 19:45:49 UTC, Chris Bare wrote:

dbForeignKey!(Position)



static if(is(T == dbForeignKey!Arg, Arg)) {
   // use Arg here
}


Thanks, that did it.


in a template, how can I get the parameter of a user defined attribute

2021-11-06 Thread Chris Bare via Digitalmars-d-learn
In the example below, one of the attributes is 
@dbForeignKey!Position. pragma (msg, attr) gives me: 
dbForeignKey!(Position). I want to be able to extract the Postion 
as a type so I can call another template like: save!Position ();


Is this possible?

```d
import std.stdio;
import std.traits;
import std.format;

struct dbForeignKey(T)
{
}

struct Employee
{
long id;
string first_name;
string last_name;
@dbForeignKey!Position long default_position_id;
}

struct Position
{
long id;
string name;
}

void main()
{
Employee e;

structForm!Employee (e);

}

void structForm (T)(T data)
{
string form = fullyQualifiedName!T;
writeln ("form name = %s".format(form));
string field_label;
bool required;
 foreach(field_name; FieldNameTuple!T)
{
writeln ("field name = %s".format(field_name));
auto field_value = __traits(getMember, data, 
field_name);
writeln ("field value = %s".format(field_value));
	foreach (attr; __traits(getAttributes, __traits(getMember, 
data, field_name)))

{
pragma (msg, attr);
writeln ("attr = %s".format (attr.stringof));
}
writeln ("---");
}
}
```
The compiler prints this for the pragma is:
```
dbForeignKey!(Position)
```

the program's output is:
```
form name = test.Employee
field name = id
field value = 0
---
field name = first_name
field value =
---
field name = last_name
field value =
---
field name = default_position_id
field value = 0
attr = dbForeignKey!(Position)
---
```


Re: vibe.d json deserializeJson!Foo and ddbc.update!Foo

2021-02-13 Thread Chris Bare via Digitalmars-d-learn
On Saturday, 13 February 2021 at 01:21:56 UTC, Steven 
Schveighoffer wrote:

On 2/12/21 6:22 PM, Chris Bare wrote:

[...]


Does @ignore work? That's a UDA that tells vibe to ignore the 
field. Though I don't know if it means it leaves it alone 
completely.


https://vibed.org/api/vibe.data.serialization/ignore

-Steve


@ignore just prevents it from throwing an error if the field is 
not in the json. The field still gets overwritten with it's 
default value in the struct.




vibe.d json deserializeJson!Foo and ddbc.update!Foo

2021-02-12 Thread Chris Bare via Digitalmars-d-learn
I'm working on a project with vibe.d and ddbc to access my 
database.
Both have powerful functions that operate on Struct types to 
avoid a lot of boilerplate code.

I have a web site that sends a json message to update a record.
deserializeJson creates a new Foo record containing the json data.
However, there are some fields in the record which are not in the 
json, and these get default values.

I have another Foo that has the original values.

What would be the best way to merge the updated values into the 
original?
Obviously I can copy from one Foo to the other, but that's pretty 
inelegant.
If I could pass an existing Foo to deserializeJson and have only 
some fields filled in, that would be perfect.

I tried this:

deserializeJson!Foo(foo, req.json);

but it completely overwrites all the fields in foo.

Any suggestions? Is there a trick I'm just not seeing?

--
Chris


Associative Array Question

2020-12-31 Thread Chris Bare via Digitalmars-d-learn
I'm missing something about the way associative arrays are 
allocated.
In the example below, case 1 and case 2 seem to be the same, a 
type indexed by a long.

Case 2 fails with a Range violation.

Can someone explain the difference?
I found a work around (or the correct way) in case 3.

struct Project
{
string  date;
string  name;
}

// case 1
string[long] a1;
a1[10] = "testing a1";

foreach (f; a1)
writeln ("name ", f);

// case 2
Project[long] a2;
a2[10].name = "testing a2";

foreach (f; a2)
writeln ("name ", f.name);

// case 3
Project*[long] a3;
a3[10] = new Project;
a3[10].name = "testing a3";

foreach (f; a3)
writeln ("name ", f.name);


How can I walk the list in a RegexMatch backwards?

2019-02-03 Thread Chris Bare via Digitalmars-d-learn

auto matches = matchAll(str, searchRegex);

foreach (m; matches) // this walks the list forward

I tried:
foreach_reverse (m; matches)
foreach (m; reverse (matches))
foreach (m; retro (matches))

and they all failed to compile.
I also tried to index matches (matches[i]) but that does not work 
either.


Re: problem extracting data from GtkSourceView using Gtkd

2019-01-17 Thread Chris Bare via Digitalmars-d-learn

I think I finally figured it out.
I think the GTKapplication shutdown signal is called after the 
window has been destroyed.
If I attach a handler to the window's destroy signal, then I am 
able to get the data from the sourceView.





Re: problem extracting data from GtkSourceView using Gtkd

2019-01-16 Thread Chris Bare via Digitalmars-d-learn
Weird, the code does work in my program during startup, but when 
I call the same function from Application.onShutdown it gets the 
0 results.


Are the widgets destroyed before onShutdown?

Here's a stripped down version my Application subclass:


int
main (string[] args)
{
auto application = new SpiralApp();
application.run(args);
return(0);
}

class SpiralApp: Application
{
MainWin win;

this ()
{
super("com.bareflix.spiral", ApplicationFlags.NON_UNIQUE);

addOnActivate();
addOnStartup();
addOnShutdown();
}

void onActivate(GioApplication application)
{
win = new MainWin();
this.addWindow (win.spiral);
win.spiral.showAll();

notepadList = new NotepadList (dir);
activePad = notepadList.front ();
		activePad.activate ();	// this sets the text in the 
GtkSourceView

win.getMainTxt ();  // this retrieves the text correctly
}

void onShutdown(GioApplication application)
{
		// this returns a blank string. I don't do anything in the UI, 
just

// start it up and then exit with the window manager
win.getMainTxt ();
notepadList.write ();
activePad.save ();
info ("on shutdown here");
}
}

Any ideas?


problem extracting data from GtkSourceView using Gtkd

2019-01-14 Thread Chris Bare via Digitalmars-d-learn
I would have posted this in the Gtkd forum, but it has been down 
for a while.


I'm porting a GTK2/C program to Gtkd. I'm trying to read the data 
from a GtkSourceView, but when I try to get the bounds, it's 
always zero.


Here's the c version that works:
GtkSourceBuffer *bf;
GtkTextIter start, end;
int len;

bf = gtk_text_view_get_buffer (GTK_TEXT_VIEW (w.mainTxt));
gtk_text_buffer_get_bounds (GTK_TEXT_BUFFER(bf), , );
	data = gtk_text_buffer_get_text (GTK_TEXT_BUFFER(bf), , 
, FALSE);


Here's what I tried in D:
SourceBuffer bf;
auto start = new TextIter();
auto end = new TextIter();

bf = this.mainTxt.getBuffer ();
bf.getBounds (start, end);
string str = bf.getText (start, end, false);

getBounds is defined as: getBounds(out TextIter start, out 
TextIter end)


Is there something I'm doing wrong?
Any suggestions on what else to try?




Reading XDR data from a file

2018-11-24 Thread Chris Bare via Digitalmars-d-learn
I have a file in xdr format and I'm trying to use the xdr module 
from dub.

After a lot of failures, I got this to work:

ubyte[] b2 = cast (ubyte[]) read(fname);
string n = cast (string) b2.get!string();
tracef("<%s>", n);

I feel like there should be a way for the xdr get to read 
directly from the file rather than reading it all into a buffer 
first, but no combination of File f = File(fname, "r"); and get  
that I tried would compile.


Also is using cast like this good practice?

--
Chris


passing subclass to superclass where parameter is a delegate for the superclass

2018-11-14 Thread Chris Bare via Digitalmars-d-learn

If I have:
class base
{
void delegate(base) stored_dg;

void
add_function (void delegate (base) dlg)
{
stored_dg = dlg;
}
}

class A : base
{
this ()
{
super ();
add_function ();
}

void foo (A a)
{
log ("i got here");
}
}

it fails because foo(A) does not match the parameter type of 
delegate(base)


If I change foo (A a) to foo (base a)

It works, but this could be awkward in a deeper class hierarchy 
where you

might not know the class name to use.

Is there a way to do this where foo's parameter type does not to 
match the

class that implements add_function?

Thanks,
Chris