How, to export and import Vue3 composition COMPOSABLE CLASS with static variable? - class

How to export a class with static variables - using as an ENUM
class State{
static Default = 0;
static Hyper=1;
}
and import it into components to be used like const state = ref(State.Hyper)

Related

#DynamicPropertySource not being invoked (Kotlin, Spring Boot and TestContainers)

I'm trying to define a #TestConfiguration class that is executed once before all integration tests to run a MongoDB TestContainer in Kotlin in a Spring Boot project.
Here is the code:
import org.springframework.boot.test.context.TestConfiguration
import org.springframework.test.context.DynamicPropertyRegistry
import org.springframework.test.context.DynamicPropertySource
import org.testcontainers.containers.MongoDBContainer
import org.testcontainers.utility.DockerImageName
#TestConfiguration
class TestContainerMongoConfig {
companion object {
#JvmStatic
private val MONGO_CONTAINER: MongoDBContainer = MongoDBContainer(DockerImageName.parse("mongo").withTag("latest")).withReuse(true)
#JvmStatic
#DynamicPropertySource
private fun emulatorProperties(registry: DynamicPropertyRegistry) {
registry.add("spring.data.mongodb.uri", MONGO_CONTAINER::getReplicaSetUrl)
}
init { MONGO_CONTAINER.start() }
}
}
The issue seems to be that emulatorProperties method is not being called.
The regular flow should be that the container is started and then the properties are set.
The first step happens, the second does not.
I know there is an alternative for which I can do this configuration in each functional test class but I don't like it as it adds not needed noise to the test class.
For example, with a Java project that uses Postgres I managed to make it work with the following code:
import javax.sql.DataSource;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.context.annotation.Bean;
import org.testcontainers.containers.PostgreSQLContainer;
import org.testcontainers.utility.DockerImageName;
#TestConfiguration
public class PostgresqlTestContainersConfig {
static final PostgreSQLContainer POSTGRES_CONTAINER;
private final static DockerImageName IMAGE = DockerImageName.parse("postgres").withTag("latest");
static {
POSTGRES_CONTAINER = new PostgreSQLContainer(IMAGE);
POSTGRES_CONTAINER.start();
}
#Bean
DataSource dataSource() {
return DataSourceBuilder.create()
.username(POSTGRES_CONTAINER.getUsername())
.password(POSTGRES_CONTAINER.getPassword())
.driverClassName(POSTGRES_CONTAINER.getDriverClassName())
.url(POSTGRES_CONTAINER.getJdbcUrl())
.build();
}
}
I'm trying to achieve the same thing but in Kotlin and using MongoDB.
Any idea on what may be the issue causing the #DynamicPropertySource not being called?
#DynamicPropertySource is part of the Spring-Boot context lifecycle. Since you want to replicate the Java setup in a way, it is not required to use #DynamicPropertySource. Instead you can follow the Singleton Container Pattern, and replicate it in Kotlin as well.
Instead of setting the config on the registry, you can set them as a System property and Spring Autoconfig will pick it up:
init {
MONGO_CONTAINER.start()
System.setProperty("spring.data.mongodb.uri", MONGO_CONTAINER.getReplicaSetUrl());
}
I was able to resolve similar problem in Groovy by:
Having static method annotated with #DynamicPropetySource directly in the test class (probably it would also work in superclass.
But I didn't want to copy the code into every test class that needs MongoDB.
I resolved the issue by using ApplicationContexInitializer
The example is written in groovy
class MongoTestContainer implements ApplicationContextInitializer<ConfigurableApplicationContext>{
static final MongoDBContainer mongoDBContainer = new MongoDBContainer(DockerImageName.parse("mongo:6.0.2"))
#Override
void initialize(ConfigurableApplicationContext applicationContext) {
mongoDBContainer.start()
def testValues = TestPropertyValues.of("spring.data.mongodb.uri="+ mongoDBContainer.getReplicaSetUrl())
testValues.applyTo(applicationContext.getEnvironment())
}
}
To make it complete, in the test class, you just need to add #ContextConfiguration(initializers = MongoTestContainer) to activate context initializer for the test.
For this you could also create custom annotation which would combine #DataMongoTest with previous annotation.
This solution works for me.
Method with #DynamicPropertySource is inside companion object(also added #JvmStatic) and added org.testcontainers.junit.jupiter.Testcontainers on the test class
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.jdbc.DataSourceBuilder
import org.springframework.boot.test.context.TestConfiguration
import org.springframework.context.annotation.Bean
import org.springframework.test.context.ContextConfiguration
import org.springframework.test.context.DynamicPropertyRegistry
import org.springframework.test.context.DynamicPropertySource
import org.springframework.test.context.junit.jupiter.SpringExtension
import org.testcontainers.containers.PostgreSQLContainer
import org.testcontainers.junit.jupiter.Container
import org.testcontainers.junit.jupiter.Testcontainers
import javax.sql.DataSource
#ExtendWith(SpringExtension::class)
#Testcontainers
#TestConfiguration
#ContextConfiguration(classes = [PostgresqlTestContainersConfig::class])
class PostgresqlTestContainersConfig {
#Autowired
var dataSource: DataSource? = null
#Test
internal fun name() {
dataSource!!.connection.close()
}
#Bean
fun dataSource(): DataSource? {
return DataSourceBuilder.create()
.username(POSTGRES_CONTAINER.getUsername())
.password(POSTGRES_CONTAINER.getPassword())
.driverClassName(POSTGRES_CONTAINER.getDriverClassName())
.url(POSTGRES_CONTAINER.getJdbcUrl())
.build()
}
companion object {
#JvmStatic
#Container
private val POSTGRES_CONTAINER: PostgreSQLContainer<*> = PostgreSQLContainer("postgres:9.6.12")
.withDatabaseName("integration-tests-db")
.withUsername("sa")
.withPassword("sa")
#JvmStatic
#DynamicPropertySource
fun postgreSQLProperties(registry: DynamicPropertyRegistry) {
registry.add("db.url") { POSTGRES_CONTAINER.jdbcUrl }
registry.add("db.user") { POSTGRES_CONTAINER.username }
registry.add("db.password") { POSTGRES_CONTAINER.password }
}
}
}

Hive: generate adapters for classes in multiple .dart files

I have classes defined in individual files. I want to store all of the classes in a single Hive box. I cannot see how to register the adapters for the separate files and have them generated into a single .part file. Do I need to create a method and explicitly call the method to register the adapters? I've tried registering them in the Main() method of main file, but that doesn't work. After running build_runner, the additional adapters are not in the .part file.
CLASS A
import 'package:hive/hive.dart';
import 'package:my_project/class_b.dart';
import 'package:my_project/class_c.dart';
...
#HiveType(typeId: 0)
class ClassA {
#HiveField(0)
String string ="";
...
void main(List<String> arguments) async {
var path = "assets/";
Hive
..registerAdapter(ClassAAdapter());
..registerAdapter(ClassBAdapter()); //???
...
}
CLASS B
import 'package:hive/hive.dart';
...
#HiveType(typeId: 1)
class ClassB {
#HiveField(0)
String string ="";
...
void initAdapters() { //???
var path = "assets/";
Hive // ??????
..registerAdapter(ClassBAdapter()); //???
}

flutter how to create an dart:ffi struct reference

I created a struct with dart:ffi.
import 'dart:ffi';
import 'package:ffi/ffi.dart';
class TestStruct extends Struct{
external Pointer<Utf8> strText;
#Int32()
external int nNum;
#Bool()
external bool bIsTrue;
//contstruct
TestStruct(String str, int number, bool state){
strText = str as Pointer<Utf8>;
nNum = number as int;
bIsTrue = state as bool;
}
}
I want to create a reference of TestStruct and use it. So I wrote the code.
TestStruct test = TestStruct("Text", 10, true);
but this is an error
Subclasses of 'Struct' and 'Union' are backed by native memory, and can't be instantiated by a generative constructor.
Try allocating it via allocation, or load from a 'Pointer'.
I tried searching with the api documentation, but I didn't understand. Do you know how to create a struct as a reference?? thank you.
Example:
class InAddr extends Struct {
factory InAddr.allocate(int sAddr) =>
calloc<InAddr>().ref
..sAddr = sAddr;
#Uint32()
external int sAddr;
}
You can allocate this with calloc function
final Pointer<InAddr> inAddress = calloc<InAddr>();
And free the pointer with
calloc.free(inAddress);
While I am late to the party, writing a complete example so that it may be helpful for others new to the FFI scene.
If this is the dart representative struct (from C) -
import 'dart:ffi';
import 'package:ffi/ffi.dart';
class TestStruct extends Struct{
#Int32()
external int nNum;
#Bool()
external bool bIsTrue;
// IMPORTANT: DO NOT DEFINE A CONSTRUCTOR since struct/union are natively allocated
}
You can instantiate from dart using ffi.Pointer like below.
// my_native_helper.dart
final dynamicLibrary = ... ; // load .so/process
void some_function() {
final Pointer<TestStruct > inAddress = calloc<TestStruct >();
inAddress.ref.nNum= 1234;
inAddress.ref.bIsTrue = false;
// ffigen generated method replicating C extern function
// To auto generate this, define the C signature in a header file
dynamicLibrary.receiveFromDart(inAddress);
calloc.free(inAddress);
}
The C method will look like below.
extern "C" void recieveFromDart(TestStruct* structPtr) {
cout << structPtr->nNum << "/" << structPtr->bIsTrue << endl;
}
According to the doc you can't create instances of Struct classes:
https://api.flutter.dev/flutter/dart-ffi/Struct-class.html
However, you typically would need pointers. So you can come up with something like that:
static Pointer<InAddr> allocate(int sAddr) {
final pointer = calloc<InAddr>();
pointer.ref.sAddr = sAddr;
return pointer;
}

How to set up an interface with an abstract member variable in Dart

I'm building a Flutter application and wanted to define a common scheme for all my pages. All pages should have a static id variable that I use for routing with named routes. (instead of having to instanciate MainMenuPage().id for Navigator.pushNamed(..., MainMenuPage ().id), I prefer to use the MainMenu.id.
In the MainMenuPage page file, this currently looks like:
class MainMenuPage extends StatelessWidget {
const MainMenu();
static String id = 'main-menu-id'; // added by me, but not "enforced" through dart logic
}
I am aware that the flutter-way of Widget-Stacking is composition over inheritance. Still, I was wondering why I cannot create a superclass of all my pages to force them to implement this static variable.
In my imagination this would be looking like this:
abstract class MyAppPage() {
static String id;
}
class NotAbstractPage() extends StatelessWidget implements MyAppPage {
static String id = 'foobar'; // this line is enforced through the quasi-interface
}
Unfortunately, this approach currently does not work in Dart. How could one make this work with some interfacy-ish mechaning?
You can force your pages to implement the variable in your superclass but it shouldn't be a static variable like this :
abstract class MyAppPage {
late String id ;
}
if you Implement this class without overriding this variable it will give you an error :
after overriding it:

Getter on flutter error when different files

I'm trying to make all strings in 1 file, but when I try it it produces an error Only static members can be accessed in initializers
I want to put all strings in 1 file and can be called with the getter method for all classes, be it an ordinary class, a class with StatefullWidget, and a class with StatelessWidget
error Only static members can be accessed in initializers leading to configStrings in the ApiService class
My Code in config_string.dart
class ConfigStrings {
String _blogUrl = "https://blogspot-to-app.blogspot.com/";
String get blogUrl => _blogUrl;
}
My Code in api_service.dart
import 'package:blogspot/config/config_strings.dart';
import 'package:http/http.dart';
class ApiServices {
var configStrings = ConfigStrings();
final String baseUrl = configStrings.blogUrl;
Client client = Client();
}
You need to access configStrings inside a method or the constructor:
import 'package:blogspot/config/config_strings.dart';
import 'package:http/http.dart';
class ApiServices {
var configStrings = ConfigStrings();
ApiServices(){
final String baseUrl = configStrings.blogUrl;
}
Client client = Client();
}