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

Long Config Value Locked at Lower Value #44

Open
Swedz opened this issue Feb 17, 2024 · 3 comments
Open

Long Config Value Locked at Lower Value #44

Swedz opened this issue Feb 17, 2024 · 3 comments

Comments

@Swedz
Copy link

Swedz commented Feb 17, 2024

Very weird problem I'm encountering. I'm using the Gson extension for dazzleconf. I have the following value in my config interface (HexCrawlerConfig):

@ConfKey("channel")
@ConfDefault.DefaultLong(0)
long channel();

In my .json config file it is defined like so:

"channel": 1184252858728726529

Whenever my config is loaded, it changes the value to be 1 less:

"channel": 1184252858728726528

If I leave it at this new value it chose for it, it stays like this every time I load up. When I set it back to the original value, it overrides it back to this value... Something I figured I'd try was just set the value to be 1 greater than what I need it to be, but it still locks the value to the one with the 8 at the end.

Here is how I am loading the config itself:

public final class JsonDazzleConfHandler<C> extends ConfigurationHelper<C>
{
	private volatile C configData;
	
	private JsonDazzleConfHandler(Path configFolder, String fileName, ConfigurationFactory<C> factory)
	{
		super(configFolder, fileName, factory);
	}
	
	public static <C> JsonDazzleConfHandler<C> create(Path configFolder, String fileName, Class<C> configClass)
	{
		GsonOptions jsonOptions = new GsonOptions.Builder()
				.build();
		return new JsonDazzleConfHandler<>(
				configFolder, fileName,
				GsonConfigurationFactory.create(
						configClass,
						new ConfigurationOptions.Builder().sorter(new AnnotationBasedSorter()).build(),
						jsonOptions
				)
		);
	}
	
	public void reload()
	{
		try
		{
			configData = reloadConfigData();
		}
		catch (IOException ex)
		{
			throw new UncheckedIOException(ex);
		}
		catch (ConfigFormatSyntaxException ex)
		{
			configData = getFactory().loadDefaults();
			System.err.println("Uh-oh! The syntax of your configuration are invalid.");
			ex.printStackTrace();
			
		}
		catch (InvalidConfigException ex)
		{
			configData = getFactory().loadDefaults();
			System.err.println("Uh-oh! The values in your configuration are invalid. Check to make sure you have specified the right data types.");
			ex.printStackTrace();
		}
	}
	
	public C get()
	{
		if(configData == null)
		{
			throw new IllegalStateException("Configuration has not been loaded yet");
		}
		return configData;
	}
}
JsonDazzleConfHandler<HexCrawlerConfig> configHandler = JsonDazzleConfHandler.create(
		Path.of("file path here"), "config.json",
		HexCrawlerConfig.class
);
configHandler.reload();
config = configHandler.get();
System.out.println("test: " + config.channel());
@Swedz
Copy link
Author

Swedz commented Feb 17, 2024

I'm trying some other values to see what happens. Lower values seem to stay the same as expected, but larger values get shifted around somewhat.

118425285872872652 gets changed into 118425285872872656, for example.

@Swedz
Copy link
Author

Swedz commented Feb 17, 2024

A working solution is to write the long as a string in the json. Dazzleconf reads it as a long just fine. Although when the config is loaded, it converts it back to a long and gets rid of the quotation marks around the channel value. This can be worked around using the following code:

default long channel()
{
	return Long.parseLong(this.channelString());
}

@ConfKey("channel")
@ConfDefault.DefaultString("0")
String channelString();

@A248
Copy link
Owner

A248 commented Feb 17, 2024

This is very odd. May I ask you to try the following things?

  1. Run this generic number-parsing code, and see if the result is the correct long:
String text = "1184252858728726529";
long value = NumberFormat.getInstance().parse(text);
System.out.println(value);
  1. Use Gson directly and serialize/deserialize. Deserialize to an Object and check the type of the deserialized value, and, of course, the value itself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants